-
Notifications
You must be signed in to change notification settings - Fork 459
/
Copy pathContext.swift
265 lines (239 loc) · 7.97 KB
/
Context.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Sources/protoc-gen-swift/Context.swift - Overall code generation handling
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
// -----------------------------------------------------------------------------
///
/// The 'Context' wraps the CodeGeneratorRequest provided by protoc. As such,
/// it is the only place that actually has access to all of the information provided
/// by protoc.
///
/// Much of protoc-gen-swift is based around two basic idioms:
/// - Each descriptor object is wrapped by a 'generator' that provides
/// additional data storage and logic
/// - Most methods are invoked with a reference to the Context class so
/// they can look up information about remote types. Note that this
/// reference is never stored locally.
///
// -----------------------------------------------------------------------------
import Foundation
import PluginLibrary
import SwiftProtobuf
/*
* A tool for looking up information about various types within
* the overall context.
*/
typealias CodeGeneratorRequest = Google_Protobuf_Compiler_CodeGeneratorRequest
typealias CodeGeneratorResponse = Google_Protobuf_Compiler_CodeGeneratorResponse
extension CodeGeneratorRequest {
func getMessageForPath(path: String) -> Google_Protobuf_DescriptorProto? {
for f in protoFile {
if let m = f.getMessageForPath(path: path) {
return m
}
}
return nil
}
func getMessageNameForPath(path: String) -> String? {
for f in protoFile {
if let m = f.getMessageNameForPath(path: path) {
return m
}
}
return nil
}
func getEnumNameForPath(path: String) -> String? {
for f in protoFile {
if let m = f.getEnumNameForPath(path: path) {
return m
}
}
return nil
}
func getSwiftNameForEnumCase(path: String, caseName: String) -> String {
for f in protoFile {
if let m = f.getSwiftNameForEnumCase(path: path, caseName: caseName) {
return m
}
}
fatalError("Unable to locate Enum case \(caseName) in path \(path)")
}
}
extension CodeGeneratorResponse {
init(error: String) {
self.init()
self.error = error
}
}
extension CodeGeneratorResponse.File {
init(name: String, content: String) {
self.init()
self.name = name
self.content = content
}
}
class GeneratorOptions {
enum OutputNaming : String {
case FullPath
case PathToUnderscores
case DropPath
}
enum Visibility : String {
case Internal
case Public
}
private(set) var outputNaming: OutputNaming = .FullPath
private(set) var visibility: Visibility = .Internal
init(parameter: String?) throws {
for pair in parseParameter(string:parameter) {
switch pair.key {
case "FileNaming":
if let naming = OutputNaming(rawValue: pair.value) {
outputNaming = naming
} else {
throw GenerationError.invalidParameterValue(name: pair.key,
value: pair.value)
}
case "Visibility":
if let value = Visibility(rawValue: pair.value) {
visibility = value
} else {
throw GenerationError.invalidParameterValue(name: pair.key,
value: pair.value)
}
default:
throw GenerationError.unknownParameter(name: pair.key)
}
}
}
}
extension GeneratorOptions {
/// Returns a string snippet to insert for the visibility
var visibilitySourceSnippet: String {
switch visibility {
case .Internal:
return ""
case .Public:
return "public "
}
}
}
class Context {
let request: CodeGeneratorRequest
let options: GeneratorOptions
private(set) var parent = [String:String]()
private(set) var fileByProtoName = [String:Google_Protobuf_FileDescriptorProto]()
private(set) var enumByProtoName = [String:Google_Protobuf_EnumDescriptorProto]()
private(set) var messageByProtoName = [String:Google_Protobuf_DescriptorProto]()
private(set) var protoNameIsGroup = Set<String>()
func swiftNameForProtoName(protoName: String, appending: String? = nil, separator: String = ".") -> String {
let p = parent[protoName]
if let e = enumByProtoName[protoName] {
return swiftNameForProtoName(protoName: p!, appending: e.name, separator: separator)
} else if let m = messageByProtoName[protoName] {
let baseName: String
if protoNameIsGroup.contains(protoName) {
// TODO: Find a way to actually get to this line of code.
// Then fix it to be whatever it should be.
// If it can't be reached, assert an error in this case.
baseName = "XXGROUPXX_" + m.name + "_XXGROUPXX"
} else {
baseName = m.name
}
let name: String
if let a = appending {
name = baseName + separator + a
} else {
name = baseName
}
return swiftNameForProtoName(protoName: p!, appending: name, separator: separator)
} else if let f = fileByProtoName[protoName] {
return f.swiftPrefix + (appending ?? "")
}
return ""
}
func getMessageForPath(path: String) -> Google_Protobuf_DescriptorProto? {
return request.getMessageForPath(path: path)
}
func getMessageNameForPath(path: String) -> String? {
return request.getMessageNameForPath(path: path)
}
func getEnumNameForPath(path: String) -> String? {
return request.getEnumNameForPath(path: path)
}
init(request: CodeGeneratorRequest) throws {
self.request = request
self.options = try GeneratorOptions(parameter: request.parameter)
for fileProto in request.protoFile {
populateFrom(fileProto: fileProto)
}
}
func populateFrom(fileProto: Google_Protobuf_FileDescriptorProto) {
let prefix: String
let pkg = fileProto.package
if !pkg.isEmpty {
prefix = "." + pkg
} else {
prefix = ""
}
fileByProtoName[prefix] = fileProto
for e in fileProto.enumType {
populateFrom(enumProto: e, prefix: prefix)
}
for m in fileProto.messageType {
populateFrom(messageProto: m, prefix: prefix)
}
for f in fileProto.extension_p {
if f.type == .group {
protoNameIsGroup.insert(f.typeName)
}
}
}
func populateFrom(enumProto: Google_Protobuf_EnumDescriptorProto, prefix: String) {
let name = prefix + "." + enumProto.name
enumByProtoName[name] = enumProto
parent[name] = prefix
}
func populateFrom(messageProto: Google_Protobuf_DescriptorProto, prefix: String) {
let name = prefix + "." + messageProto.name
parent[name] = prefix
messageByProtoName[name] = messageProto
for f in messageProto.field {
if f.type == .group {
protoNameIsGroup.insert(f.typeName)
}
}
for f in messageProto.extension_p {
if f.type == .group {
protoNameIsGroup.insert(f.typeName)
}
}
for e in messageProto.enumType {
populateFrom(enumProto: e, prefix: name)
}
for m in messageProto.nestedType {
populateFrom(messageProto: m, prefix: name)
}
}
func getSwiftNameForEnumCase(path: String, caseName: String) -> String {
return request.getSwiftNameForEnumCase(path: path, caseName: caseName)
}
func generateResponse() -> CodeGeneratorResponse {
var response = CodeGeneratorResponse()
let explicit = Set<String>(request.fileToGenerate)
for fileProto in request.protoFile where explicit.contains(fileProto.name) {
var printer = CodePrinter()
let file = FileGenerator(descriptor: fileProto, generatorOptions: options)
file.generateOutputFile(printer: &printer, context: self)
let fileResponse = CodeGeneratorResponse.File(name: file.outputFilename, content: printer.content)
response.file.append(fileResponse)
}
return response
}
}