Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check Swift reserved names when generating function names #1213

Merged
merged 4 commits into from
Jul 8, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 50 additions & 5 deletions Sources/protoc-gen-grpc-swift/Generator-Names.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,58 @@ extension Generator {
return nameForPackageServiceMethod(file, service, method) + "Call"
}

internal let quotableFieldNames: Set<String> = {
() -> Set<String> in
ialimz marked this conversation as resolved.
Show resolved Hide resolved
var names: Set<String> = []

names = names.union(swiftKeywordsUsedInDeclarations)
names = names.union(swiftKeywordsUsedInStatements)
names = names.union(swiftKeywordsUsedInExpressionsAndTypes)
return names
}()

internal let swiftKeywordsUsedInDeclarations: Set<String> = [
"associatedtype", "class", "deinit", "enum", "extension",
"fileprivate", "func", "import", "init", "inout", "internal",
"let", "open", "operator", "private", "protocol", "public",
"static", "struct", "subscript", "typealias", "var"
]

internal let swiftKeywordsUsedInStatements: Set<String> = [ "break", "case",
"continue", "default", "defer", "do", "else", "fallthrough",
"for", "guard", "if", "in", "repeat", "return", "switch", "where",
"while"
]

internal let swiftKeywordsUsedInExpressionsAndTypes: Set<String> = [ "as",
"Any", "catch", "false", "is", "nil", "rethrows", "super", "self",
"Self", "throw", "throws", "true", "try"
]

internal let quotableFieldNames: Set<String> = {
() -> Set<String> in
ialimz marked this conversation as resolved.
Show resolved Hide resolved
var names: Set<String> = []

names = names.union(swiftKeywordsUsedInDeclarations)
names = names.union(swiftKeywordsUsedInStatements)
names = names.union(swiftKeywordsUsedInExpressionsAndTypes)
return names
ialimz marked this conversation as resolved.
Show resolved Hide resolved
}()

internal var methodFunctionName: String {
let name = method.name
if self.options.keepMethodCasing {
return name
} else {
return name.prefix(1).lowercased() + name.dropFirst()
var name = method.name
if !self.options.keepMethodCasing {
name = name.prefix(1).lowercased() + name.dropFirst()
}

return sanitize(fieldName: name)
}

internal func sanitize(fieldName string: String) -> String {
if quotableFieldNames.contains(string) {
return "`\(string)`"
}
return string
}

internal var methodInputName: String {
Expand Down