Skip to content

Commit dea448c

Browse files
authored
Merge pull request apple#980 from tbkka/tbkka-moduleName
Don't hardcode the `SwiftProtobuf` module name everywhere
2 parents 92e58f4 + 023d5dc commit dea448c

File tree

9 files changed

+43
-40
lines changed

9 files changed

+43
-40
lines changed

Sources/protoc-gen-swift/Descriptor+Extensions.swift

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ extension FieldDescriptor {
214214
switch type {
215215
case .bool: return "false"
216216
case .string: return "String()"
217-
case .bytes: return "SwiftProtobuf.Internal.emptyData"
217+
case .bytes: return "\(Version.moduleName).Internal.emptyData"
218218
case .group, .message:
219219
return namer.fullName(message: messageType) + "()"
220220
case .enum:
@@ -235,31 +235,31 @@ extension FieldDescriptor {
235235
let valueTraits = valueField.traitsType(namer: namer)
236236
switch valueField.type {
237237
case .message: // Map's can't have a group as the value
238-
return "SwiftProtobuf._ProtobufMessageMap<\(keyTraits),\(valueTraits)>"
238+
return "\(Version.moduleName)._ProtobufMessageMap<\(keyTraits),\(valueTraits)>"
239239
case .enum:
240-
return "SwiftProtobuf._ProtobufEnumMap<\(keyTraits),\(valueTraits)>"
240+
return "\(Version.moduleName)._ProtobufEnumMap<\(keyTraits),\(valueTraits)>"
241241
default:
242-
return "SwiftProtobuf._ProtobufMap<\(keyTraits),\(valueTraits)>"
242+
return "\(Version.moduleName)._ProtobufMap<\(keyTraits),\(valueTraits)>"
243243
}
244244
}
245245
switch type {
246-
case .double: return "SwiftProtobuf.ProtobufDouble"
247-
case .float: return "SwiftProtobuf.ProtobufFloat"
248-
case .int64: return "SwiftProtobuf.ProtobufInt64"
249-
case .uint64: return "SwiftProtobuf.ProtobufUInt64"
250-
case .int32: return "SwiftProtobuf.ProtobufInt32"
251-
case .fixed64: return "SwiftProtobuf.ProtobufFixed64"
252-
case .fixed32: return "SwiftProtobuf.ProtobufFixed32"
253-
case .bool: return "SwiftProtobuf.ProtobufBool"
254-
case .string: return "SwiftProtobuf.ProtobufString"
246+
case .double: return "\(Version.moduleName).ProtobufDouble"
247+
case .float: return "\(Version.moduleName).ProtobufFloat"
248+
case .int64: return "\(Version.moduleName).ProtobufInt64"
249+
case .uint64: return "\(Version.moduleName).ProtobufUInt64"
250+
case .int32: return "\(Version.moduleName).ProtobufInt32"
251+
case .fixed64: return "\(Version.moduleName).ProtobufFixed64"
252+
case .fixed32: return "\(Version.moduleName).ProtobufFixed32"
253+
case .bool: return "\(Version.moduleName).ProtobufBool"
254+
case .string: return "\(Version.moduleName).ProtobufString"
255255
case .group, .message: return namer.fullName(message: messageType)
256-
case .bytes: return "SwiftProtobuf.ProtobufBytes"
257-
case .uint32: return "SwiftProtobuf.ProtobufUInt32"
256+
case .bytes: return "\(Version.moduleName).ProtobufBytes"
257+
case .uint32: return "\(Version.moduleName).ProtobufUInt32"
258258
case .enum: return namer.fullName(enum: enumType)
259-
case .sfixed32: return "SwiftProtobuf.ProtobufSFixed32"
260-
case .sfixed64: return "SwiftProtobuf.ProtobufSFixed64"
261-
case .sint32: return "SwiftProtobuf.ProtobufSInt32"
262-
case .sint64: return "SwiftProtobuf.ProtobufSInt64"
259+
case .sfixed32: return "\(Version.moduleName).ProtobufSFixed32"
260+
case .sfixed64: return "\(Version.moduleName).ProtobufSFixed64"
261+
case .sint32: return "\(Version.moduleName).ProtobufSInt32"
262+
case .sint64: return "\(Version.moduleName).ProtobufSInt64"
263263
}
264264
}
265265
}

Sources/protoc-gen-swift/EnumGenerator.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class EnumGenerator {
5858

5959
p.print("\n")
6060
p.print(enumDescriptor.protoSourceComments())
61-
p.print("\(visibility)enum \(swiftRelativeName): SwiftProtobuf.Enum {\n")
61+
p.print("\(visibility)enum \(swiftRelativeName): \(Version.moduleName).Enum {\n")
6262
p.indent()
6363
p.print("\(visibility)typealias RawValue = Int\n")
6464

@@ -120,7 +120,7 @@ class EnumGenerator {
120120

121121
func generateRuntimeSupport(printer p: inout CodePrinter) {
122122
p.print("\n")
123-
p.print("extension \(swiftFullName): SwiftProtobuf._ProtoNameProviding {\n")
123+
p.print("extension \(swiftFullName): \(Version.moduleName)._ProtoNameProviding {\n")
124124
p.indent()
125125
generateProtoNameProviding(printer: &p)
126126
p.outdent()
@@ -156,7 +156,7 @@ class EnumGenerator {
156156
private func generateProtoNameProviding(printer p: inout CodePrinter) {
157157
let visibility = generatorOptions.visibilitySourceSnippet
158158

159-
p.print("\(visibility)static let _protobuf_nameMap: SwiftProtobuf._NameMap = [\n")
159+
p.print("\(visibility)static let _protobuf_nameMap: \(Version.moduleName)._NameMap = [\n")
160160
p.indent()
161161
for v in mainEnumValueDescriptorsSorted {
162162
if v.aliases.isEmpty {

Sources/protoc-gen-swift/ExtensionSetGenerator.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class ExtensionSetGenerator {
4747
default: modifier = ""
4848
}
4949

50-
return "SwiftProtobuf.\(label)\(modifier)ExtensionField"
50+
return "\(Version.moduleName).\(label)\(modifier)ExtensionField"
5151
}
5252

5353
init(descriptor: FieldDescriptor, generatorOptions: GeneratorOptions, namer: SwiftProtobufNamer) {
@@ -81,7 +81,7 @@ class ExtensionSetGenerator {
8181

8282
p.print(
8383
comments,
84-
"\(visibility)\(scope)let \(swiftRelativeExtensionName) = SwiftProtobuf.MessageExtension<\(extensionFieldType)<\(traitsType)>, \(containingTypeSwiftFullName)>(\n")
84+
"\(visibility)\(scope)let \(swiftRelativeExtensionName) = \(Version.moduleName).MessageExtension<\(extensionFieldType)<\(traitsType)>, \(containingTypeSwiftFullName)>(\n")
8585
p.indent()
8686
p.print(
8787
"_protobuf_fieldNumber: \(fieldDescriptor.number),\n",
@@ -223,7 +223,7 @@ class ExtensionSetGenerator {
223223
"/// this .proto file. It can be used any place an `SwiftProtobuf.ExtensionMap` is needed\n",
224224
"/// in parsing, or it can be combined with other `SwiftProtobuf.SimpleExtensionMap`s to create\n",
225225
"/// a larger `SwiftProtobuf.SimpleExtensionMap`.\n",
226-
"\(generatorOptions.visibilitySourceSnippet)let \(filePrefix)\(filenameAsIdentifer)_Extensions: SwiftProtobuf.SimpleExtensionMap = [\n")
226+
"\(generatorOptions.visibilitySourceSnippet)let \(filePrefix)\(filenameAsIdentifer)_Extensions: \(Version.moduleName).SimpleExtensionMap = [\n")
227227
p.indent()
228228
var separator = ""
229229
for e in extensions {

Sources/protoc-gen-swift/FileGenerator.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class FileGenerator {
9393
if !fileDescriptor.isBundledProto {
9494
// The well known types ship with the runtime, everything else needs
9595
// to import the runtime.
96-
p.print("import SwiftProtobuf\n")
96+
p.print("import \(Version.moduleName)\n")
9797
}
9898
if let neededImports = generatorOptions.protoToModuleMappings.neededModules(forFile: fileDescriptor) {
9999
p.print("\n")
@@ -190,10 +190,10 @@ class FileGenerator {
190190
"// incompatible with the version of SwiftProtobuf to which you are linking.\n",
191191
"// Please ensure that you are building against the same version of the API\n",
192192
"// that was used to generate this file.\n",
193-
"fileprivate struct _GeneratedWithProtocGenSwiftVersion: SwiftProtobuf.ProtobufAPIVersionCheck {\n")
193+
"fileprivate struct _GeneratedWithProtocGenSwiftVersion: \(Version.moduleName).ProtobufAPIVersionCheck {\n")
194194
p.indent()
195195
p.print(
196-
"struct _\(v): SwiftProtobuf.ProtobufAPIVersion_\(v) {}\n",
196+
"struct _\(v): \(Version.moduleName).ProtobufAPIVersion_\(v) {}\n",
197197
"typealias Version = _\(v)\n")
198198
p.outdent()
199199
p.print("}\n")

Sources/protoc-gen-swift/MessageFieldGenerator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ class MessageFieldGenerator: FieldGeneratorBase, FieldGenerator {
161161
guard isGroupOrMessage && fieldDescriptor.messageType.hasRequiredFields() else { return }
162162

163163
if isRepeated { // Map or Array
164-
p.print("if !SwiftProtobuf.Internal.areAllInitialized(\(storedProperty)) {return false}\n")
164+
p.print("if !\(Version.moduleName).Internal.areAllInitialized(\(storedProperty)) {return false}\n")
165165
} else {
166166
p.print("if let v = \(storedProperty), !v.isInitialized {return false}\n")
167167
}

Sources/protoc-gen-swift/MessageGenerator.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class MessageGenerator {
118118

119119
let conformances: String
120120
if isExtensible {
121-
conformances = ": SwiftProtobuf.ExtensibleMessage"
121+
conformances = ": \(Version.moduleName).ExtensibleMessage"
122122
} else {
123123
conformances = ""
124124
}
@@ -127,7 +127,7 @@ class MessageGenerator {
127127
descriptor.protoSourceComments(),
128128
"\(visibility)struct \(swiftRelativeName)\(conformances) {\n")
129129
p.indent()
130-
p.print("// SwiftProtobuf.Message conformance is added in an extension below. See the\n",
130+
p.print("// \(Version.moduleName).Message conformance is added in an extension below. See the\n",
131131
"// `Message` and `Message+*Additions` files in the SwiftProtobuf library for\n",
132132
"// methods supported on all messages.\n")
133133

@@ -137,7 +137,7 @@ class MessageGenerator {
137137

138138
p.print(
139139
"\n",
140-
"\(visibility)var unknownFields = SwiftProtobuf.UnknownStorage()\n")
140+
"\(visibility)var unknownFields = \(Version.moduleName).UnknownStorage()\n")
141141

142142
for o in oneofs {
143143
o.generateMainEnum(printer: &p)
@@ -164,7 +164,7 @@ class MessageGenerator {
164164
if isExtensible {
165165
p.print(
166166
"\n",
167-
"\(visibility)var _protobuf_extensionFieldValues = SwiftProtobuf.ExtensionFieldValueSet()\n")
167+
"\(visibility)var _protobuf_extensionFieldValues = \(Version.moduleName).ExtensionFieldValueSet()\n")
168168
}
169169
if let storage = storage {
170170
if !isExtensible {
@@ -200,7 +200,7 @@ class MessageGenerator {
200200
func generateRuntimeSupport(printer p: inout CodePrinter, file: FileGenerator, parent: MessageGenerator?) {
201201
p.print(
202202
"\n",
203-
"extension \(swiftFullName): SwiftProtobuf.Message, SwiftProtobuf._MessageImplementationBase, SwiftProtobuf._ProtoNameProviding {\n")
203+
"extension \(swiftFullName): \(Version.moduleName).Message, \(Version.moduleName)._MessageImplementationBase, \(Version.moduleName)._ProtoNameProviding {\n")
204204
p.indent()
205205

206206
if let parent = parent {
@@ -239,9 +239,9 @@ class MessageGenerator {
239239

240240
private func generateProtoNameProviding(printer p: inout CodePrinter) {
241241
if fields.isEmpty {
242-
p.print("\(visibility)static let _protobuf_nameMap = SwiftProtobuf._NameMap()\n")
242+
p.print("\(visibility)static let _protobuf_nameMap = \(Version.moduleName)._NameMap()\n")
243243
} else {
244-
p.print("\(visibility)static let _protobuf_nameMap: SwiftProtobuf._NameMap = [\n")
244+
p.print("\(visibility)static let _protobuf_nameMap: \(Version.moduleName)._NameMap = [\n")
245245
p.indent()
246246
for f in fields {
247247
p.print("\(f.number): \(f.fieldMapNames),\n")
@@ -256,7 +256,7 @@ class MessageGenerator {
256256
///
257257
/// - Parameter p: The code printer.
258258
private func generateDecodeMessage(printer p: inout CodePrinter) {
259-
p.print("\(visibility)mutating func decodeMessage<D: SwiftProtobuf.Decoder>(decoder: inout D) throws {\n")
259+
p.print("\(visibility)mutating func decodeMessage<D: \(Version.moduleName).Decoder>(decoder: inout D) throws {\n")
260260
p.indent()
261261
if storage != nil {
262262
p.print("_ = _uniqueStorage()\n")
@@ -319,7 +319,7 @@ class MessageGenerator {
319319
///
320320
/// - Parameter p: The code printer.
321321
private func generateTraverse(printer p: inout CodePrinter) {
322-
p.print("\(visibility)func traverse<V: SwiftProtobuf.Visitor>(visitor: inout V) throws {\n")
322+
p.print("\(visibility)func traverse<V: \(Version.moduleName).Visitor>(visitor: inout V) throws {\n")
323323
p.indent()
324324
generateWithLifetimeExtension(printer: &p, throws: true) { p in
325325
if let storage = storage {

Sources/protoc-gen-swift/StringUtils.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func trimWhitespace(_ s: String) -> String {
6161
/// \n\r\t\\\'\" and three-digit octal escapes but nothing else.
6262
func escapedToDataLiteral(_ s: String) -> String {
6363
if s.isEmpty {
64-
return "SwiftProtobuf.Internal.emptyData"
64+
return "\(Version.moduleName).Internal.emptyData"
6565
}
6666
var out = "Data(["
6767
var separator = ""

Sources/protoc-gen-swift/Version.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,8 @@ struct Version {
2727
// or API-changing).
2828
static let compatibilityVersion = 2
2929

30+
// Name of the SwiftProtobuf module
31+
static let moduleName = "SwiftProtobuf"
32+
3033
static let copyright = "Copyright (C) 2014-2017 Apple Inc. and the project authors"
3134
}

Sources/protoc-gen-swift/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ struct GeneratorPlugin {
101101
+ "In particular, if you have renamed this program, you will need to\n"
102102
+ "adjust the protoc command-line option accordingly.\n"
103103
+ "\n"
104-
+ "The generated Swift output requires the SwiftProtobuf \(SwiftProtobuf.Version.versionString)\n"
104+
+ "The generated Swift output requires the \(Version.moduleName) \(SwiftProtobuf.Version.versionString)\n"
105105
+ "library be included in your project.\n"
106106
+ "\n"
107107
+ "If you use `swift build` to compile your project, add this to\n"

0 commit comments

Comments
 (0)