Skip to content

Commit 6fe4436

Browse files
committed
Document/Clean up Descriptors.
- Add docs for just about everything (based on C++ Descriptors). - Tweak some method naming based on C++ Descriptors. - Add some apis that are on the C++ Descriptors. - Reduce how much of the raw protos are kept in memory.
1 parent 7e029fe commit 6fe4436

12 files changed

+476
-193
lines changed

Sources/SwiftProtobufPluginLibrary/Descriptor+Extensions.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,6 @@ extension FieldDescriptor: ProvidesLocationPath, ProvidesSourceCodeLocation {
8585
}
8686
}
8787

88-
/// Is this field packable.
89-
var isPackable: Bool {
90-
// This logic comes from the C++ FieldDescriptor::is_packable() impl.
91-
return label == .repeated && FieldDescriptor.isPackable(type: type)
92-
}
93-
9488
/// Helper to return the name to as the "base" for naming of generated fields.
9589
///
9690
/// Groups use the underlying message's name. The way groups are declared in

Sources/SwiftProtobufPluginLibrary/Descriptor.swift

Lines changed: 431 additions & 151 deletions
Large diffs are not rendered by default.

Sources/SwiftProtobufPluginLibrary/ProvidesLocationPath.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@
1010

1111
import Foundation
1212

13+
/// Protocol that all the Descriptors conform to for original .proto file
14+
/// location lookup.
1315
public protocol ProvidesLocationPath {
16+
/// Updates `path` to the source location of the complete extent of
17+
/// the object conforming to this protocol. This is a replacement for
18+
/// `GetSourceLocation()` in the C++ Descriptor apis.
1419
func getLocationPath(path: inout IndexPath)
20+
/// Returns the File this conforming object is in.
1521
var file: FileDescriptor! { get }
1622
}

Sources/SwiftProtobufPluginLibrary/ProvidesSourceCodeLocation.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@
1111
import Foundation
1212
import SwiftProtobuf
1313

14+
/// Protocol that all the Descriptors conform to for original .proto file
15+
/// location lookup.
1416
public protocol ProvidesSourceCodeLocation {
17+
/// Returns the Location of a given object (Descriptor).
1518
var sourceCodeInfoLocation: Google_Protobuf_SourceCodeInfo.Location? { get }
1619
}
1720

18-
// Default implementation for things that support ProvidesLocationPath.
21+
/// Default implementation for things that support ProvidesLocationPath.
1922
extension ProvidesSourceCodeLocation where Self: ProvidesLocationPath {
2023
public var sourceCodeInfoLocation: Google_Protobuf_SourceCodeInfo.Location? {
2124
var path = IndexPath()
@@ -24,14 +27,12 @@ extension ProvidesSourceCodeLocation where Self: ProvidesLocationPath {
2427
}
2528
}
2629

27-
// Helper to get source comments out of ProvidesSourceCodeLocation
2830
extension ProvidesSourceCodeLocation {
31+
/// Helper to get a source comments as a string.
2932
public func protoSourceComments(commentPrefix: String = "///",
3033
leadingDetachedPrefix: String? = nil) -> String {
31-
if let loc = sourceCodeInfoLocation {
32-
return loc.asSourceComment(commentPrefix: commentPrefix,
33-
leadingDetachedPrefix: leadingDetachedPrefix)
34-
}
35-
return String()
34+
guard let loc = sourceCodeInfoLocation else { return String() }
35+
return loc.asSourceComment(commentPrefix: commentPrefix,
36+
leadingDetachedPrefix: leadingDetachedPrefix)
3637
}
3738
}

Sources/SwiftProtobufPluginLibrary/SwiftProtobufInfo.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import Foundation
1616
import SwiftProtobuf
1717

18-
/// Scope for helpers about the library.
18+
/// Helpers about the library.
1919
public enum SwiftProtobufInfo {
2020
/// Proto Files that ship with the library.
2121
public static let bundledProtoFiles: Set<String> = [
@@ -35,8 +35,14 @@ public enum SwiftProtobufInfo {
3535
"google/protobuf/wrappers.proto",
3636
]
3737

38-
// Checks if a FileDescriptor is a library bundled proto file.
38+
/// Checks if a `Google_Protobuf_FileDescriptorProto` is a library bundled proto file.
39+
@available(*, deprecated, message: "Use the version that takes a FileDescriptor instead.")
3940
public static func isBundledProto(file: Google_Protobuf_FileDescriptorProto) -> Bool {
4041
return file.package == "google.protobuf" && bundledProtoFiles.contains(file.name)
4142
}
43+
44+
/// Checks if a `FileDescriptor` is a library bundled proto file.
45+
public static func isBundledProto(file: FileDescriptor) -> Bool {
46+
return file.package == "google.protobuf" && bundledProtoFiles.contains(file.name)
47+
}
4248
}

Sources/SwiftProtobufPluginLibrary/SwiftProtobufNamer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ public final class SwiftProtobufNamer {
332332
}
333333

334334
let result = NamingUtils.typePrefix(protoPackage: file.package,
335-
fileOptions: file.fileOptions)
335+
fileOptions: file.options)
336336
filePrefixCache[file.name] = result
337337
return result
338338
}

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,13 @@ extension FileDescriptor {
1717
}
1818

1919
var isBundledProto: Bool {
20-
return SwiftProtobufInfo.isBundledProto(file: proto)
20+
return SwiftProtobufInfo.isBundledProto(file: self)
2121
}
2222
}
2323

2424
extension Descriptor {
25-
/// Returns True if this is the Any WKT
26-
var isAnyMessage: Bool {
27-
return (file.syntax == .proto3 &&
28-
fullName == ".google.protobuf.Any" &&
29-
file.name == "google/protobuf/any.proto")
30-
}
25+
/// Returns true if the message should use the message set wireformat.
26+
var useMessageSetWireFormat: Bool { return options.messageSetWireFormat }
3127

3228
/// Returns True if this message recurisvely contains a required field.
3329
/// This is a helper for generating isInitialized methods.
@@ -54,7 +50,7 @@ extension Descriptor {
5450
}
5551

5652
for f in descriptor.fields {
57-
if f.label == .required {
53+
if f.isRequired {
5854
return true
5955
}
6056
switch f.type {
@@ -138,7 +134,7 @@ extension FieldDescriptor {
138134
case .repeated:
139135
return swiftType
140136
case .optional, .required:
141-
guard realOneof == nil else {
137+
guard realContainingOneof == nil else {
142138
return swiftType
143139
}
144140
if hasPresence {
@@ -217,7 +213,7 @@ extension FieldDescriptor {
217213
case .group, .message:
218214
return namer.fullName(message: messageType) + "()"
219215
case .enum:
220-
return namer.dottedRelativeName(enumValue: enumType.defaultValue)
216+
return namer.dottedRelativeName(enumValue: enumType.values.first!)
221217
default:
222218
return "0"
223219
}

Sources/protoc-gen-swift/EnumGenerator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class EnumGenerator {
6969
p.print("\n")
7070
p.print("\(visibility)init() {\n")
7171
p.indent()
72-
let dottedDefault = namer.dottedRelativeName(enumValue: enumDescriptor.defaultValue)
72+
let dottedDefault = namer.dottedRelativeName(enumValue: enumDescriptor.values.first!)
7373
p.print("self = \(dottedDefault)\n")
7474
p.outdent()
7575
p.print("}\n")

Sources/protoc-gen-swift/FileGenerator.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ class FileGenerator {
5050
/// Generate, if `errorString` gets filled in, then report error instead of using
5151
/// what written into `printer`.
5252
func generateOutputFile(printer p: inout CodePrinter, errorString: inout String?) {
53-
guard fileDescriptor.fileOptions.swiftPrefix.isEmpty ||
54-
isValidSwiftIdentifier(fileDescriptor.fileOptions.swiftPrefix,
53+
guard fileDescriptor.options.swiftPrefix.isEmpty ||
54+
isValidSwiftIdentifier(fileDescriptor.options.swiftPrefix,
5555
allowQuoted: false) else {
56-
errorString = "\(fileDescriptor.name) has an 'swift_prefix' that isn't a valid Swift identifier (\(fileDescriptor.fileOptions.swiftPrefix))."
56+
errorString = "\(fileDescriptor.name) has an 'swift_prefix' that isn't a valid Swift identifier (\(fileDescriptor.options.swiftPrefix))."
5757
return
5858
}
5959
p.print(

Sources/protoc-gen-swift/MessageFieldGenerator.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class MessageFieldGenerator: FieldGeneratorBase, FieldGenerator {
3838
private var isPacked: Bool { return fieldDescriptor.isPacked }
3939

4040
// Note: this could still be a map (since those are repeated message fields
41-
private var isRepeated: Bool {return fieldDescriptor.label == .repeated}
41+
private var isRepeated: Bool {return fieldDescriptor.isRepeated}
4242
private var isGroupOrMessage: Bool {
4343
switch fieldDescriptor.type {
4444
case .group, .message:
@@ -53,13 +53,13 @@ class MessageFieldGenerator: FieldGeneratorBase, FieldGenerator {
5353
namer: SwiftProtobufNamer,
5454
usesHeapStorage: Bool)
5555
{
56-
precondition(descriptor.realOneof == nil)
56+
precondition(descriptor.realContainingOneof == nil)
5757

5858
self.generatorOptions = generatorOptions
5959
self.usesHeapStorage = usesHeapStorage
6060
self.namer = namer
6161

62-
hasFieldPresence = descriptor.hasPresence && descriptor.realOneof == nil
62+
hasFieldPresence = descriptor.hasPresence
6363
let names = namer.messagePropertyNames(field: descriptor,
6464
prefixed: "_",
6565
includeHasAndClear: hasFieldPresence)
@@ -155,7 +155,7 @@ class MessageFieldGenerator: FieldGeneratorBase, FieldGenerator {
155155
}
156156

157157
func generateRequiredFieldCheck(printer p: inout CodePrinter) {
158-
guard fieldDescriptor.label == .required else { return }
158+
guard fieldDescriptor.isRequired else { return }
159159
p.print("if \(storedProperty) == nil {return false}\n")
160160
}
161161

0 commit comments

Comments
 (0)