@@ -44,11 +44,11 @@ public struct CodeGenerationRequest {
44
44
///
45
45
/// For example, to serialize Protobuf messages you could specify a serializer as:
46
46
/// ```swift
47
- /// request.lookupSerializer = { messageType in
47
+ /// request.makeSerializerCodeSnippet = { messageType in
48
48
/// "ProtobufSerializer<\(messageType)>()"
49
49
/// }
50
50
/// ```
51
- public var lookupSerializer : ( _ messageType: String ) -> String
51
+ public var makeSerializerCodeSnippet : ( _ messageType: String ) -> String
52
52
53
53
/// Closure that receives a message type as a `String` and returns a code snippet to
54
54
/// initialize a `MessageDeserializer` for that type as a `String`.
@@ -58,26 +58,64 @@ public struct CodeGenerationRequest {
58
58
///
59
59
/// For example, to serialize Protobuf messages you could specify a serializer as:
60
60
/// ```swift
61
- /// request.lookupDeserializer = { messageType in
61
+ /// request.makeDeserializerCodeSnippet = { messageType in
62
62
/// "ProtobufDeserializer<\(messageType)>()"
63
63
/// }
64
64
/// ```
65
- public var lookupDeserializer : ( _ messageType: String ) -> String
65
+ public var makeDeserializerCodeSnippet : ( _ messageType: String ) -> String
66
66
67
67
public init (
68
68
fileName: String ,
69
69
leadingTrivia: String ,
70
70
dependencies: [ Dependency ] ,
71
71
services: [ ServiceDescriptor ] ,
72
- lookupSerializer : @escaping ( String ) -> String ,
73
- lookupDeserializer : @escaping ( String ) -> String
72
+ makeSerializerCodeSnippet : @escaping ( _ messageType : String ) -> String ,
73
+ makeDeserializerCodeSnippet : @escaping ( _ messageType : String ) -> String
74
74
) {
75
75
self . fileName = fileName
76
76
self . leadingTrivia = leadingTrivia
77
77
self . dependencies = dependencies
78
78
self . services = services
79
- self . lookupSerializer = lookupSerializer
80
- self . lookupDeserializer = lookupDeserializer
79
+ self . makeSerializerCodeSnippet = makeSerializerCodeSnippet
80
+ self . makeDeserializerCodeSnippet = makeDeserializerCodeSnippet
81
+ }
82
+ }
83
+
84
+ extension CodeGenerationRequest {
85
+ @available ( * , deprecated, renamed: " makeSerializerSnippet " )
86
+ public var lookupSerializer : ( _ messageType: String ) -> String {
87
+ get { self . makeSerializerCodeSnippet }
88
+ set { self . makeSerializerCodeSnippet = newValue }
89
+ }
90
+
91
+ @available ( * , deprecated, renamed: " makeDeserializerSnippet " )
92
+ public var lookupDeserializer : ( _ messageType: String ) -> String {
93
+ get { self . makeDeserializerCodeSnippet }
94
+ set { self . makeDeserializerCodeSnippet = newValue }
95
+ }
96
+
97
+ @available (
98
+ * ,
99
+ deprecated,
100
+ renamed:
101
+ " init(fileName:leadingTrivia:dependencies:services:lookupSerializer:lookupDeserializer:) "
102
+ )
103
+ public init (
104
+ fileName: String ,
105
+ leadingTrivia: String ,
106
+ dependencies: [ Dependency ] ,
107
+ services: [ ServiceDescriptor ] ,
108
+ lookupSerializer: @escaping ( String ) -> String ,
109
+ lookupDeserializer: @escaping ( String ) -> String
110
+ ) {
111
+ self . init (
112
+ fileName: fileName,
113
+ leadingTrivia: leadingTrivia,
114
+ dependencies: dependencies,
115
+ services: services,
116
+ makeSerializerCodeSnippet: lookupSerializer,
117
+ makeDeserializerCodeSnippet: lookupDeserializer
118
+ )
81
119
}
82
120
}
83
121
@@ -88,7 +126,7 @@ public struct Dependency: Equatable {
88
126
public var item : Item ?
89
127
90
128
/// The access level to be included in imports of this dependency.
91
- public var accessLevel : SourceGenerator . Config . AccessLevel
129
+ public var accessLevel : CodeGenerator . Config . AccessLevel
92
130
93
131
/// The name of the imported module or of the module an item is imported from.
94
132
public var module : String
@@ -107,7 +145,7 @@ public struct Dependency: Equatable {
107
145
module: String ,
108
146
spi: String ? = nil ,
109
147
preconcurrency: PreconcurrencyRequirement = . notRequired,
110
- accessLevel: SourceGenerator . Config . AccessLevel
148
+ accessLevel: CodeGenerator . Config . AccessLevel
111
149
) {
112
150
self . item = item
113
151
self . module = module
@@ -228,33 +266,53 @@ public struct ServiceDescriptor: Hashable {
228
266
/// It is already formatted, meaning it contains "///" and new lines.
229
267
public var documentation : String
230
268
231
- /// The service name in different formats.
232
- ///
233
- /// All properties of this object must be unique for each service from within a namespace.
234
- public var name : Name
235
-
236
- /// The service namespace in different formats.
237
- ///
238
- /// All different services from within the same namespace must have
239
- /// the same ``Name`` object as this property.
240
- /// For `.proto` files the base name of this object is the package name.
241
- public var namespace : Name
269
+ /// The name of the service.
270
+ public var name : ServiceName
242
271
243
272
/// A description of each method of a service.
244
273
///
245
274
/// - SeeAlso: ``MethodDescriptor``.
246
275
public var methods : [ MethodDescriptor ]
247
276
277
+ public init (
278
+ documentation: String ,
279
+ name: ServiceName ,
280
+ methods: [ MethodDescriptor ]
281
+ ) {
282
+ self . documentation = documentation
283
+ self . name = name
284
+ self . methods = methods
285
+ }
286
+ }
287
+
288
+ extension ServiceDescriptor {
289
+ @available ( * , deprecated, renamed: " init(documentation:name:methods:) " )
248
290
public init (
249
291
documentation: String ,
250
292
name: Name ,
251
293
namespace: Name ,
252
294
methods: [ MethodDescriptor ]
253
295
) {
254
296
self . documentation = documentation
255
- self . name = name
256
- self . namespace = namespace
257
297
self . methods = methods
298
+
299
+ let identifier = namespace. base. isEmpty ? name. base : namespace. base + " . " + name. base
300
+
301
+ let typeName =
302
+ namespace. generatedUpperCase. isEmpty
303
+ ? name. generatedUpperCase
304
+ : namespace. generatedUpperCase + " _ " + name. generatedUpperCase
305
+
306
+ let propertyName =
307
+ namespace. generatedLowerCase. isEmpty
308
+ ? name. generatedUpperCase
309
+ : namespace. generatedLowerCase + " _ " + name. generatedUpperCase
310
+
311
+ self . name = ServiceName (
312
+ identifyingName: identifier,
313
+ typeName: typeName,
314
+ propertyName: propertyName
315
+ )
258
316
}
259
317
}
260
318
@@ -268,7 +326,7 @@ public struct MethodDescriptor: Hashable {
268
326
///
269
327
/// All properties of this object must be unique for each method
270
328
/// from within a service.
271
- public var name : Name
329
+ public var name : MethodName
272
330
273
331
/// Identifies if the method is input streaming.
274
332
public var isInputStreaming : Bool
@@ -284,7 +342,7 @@ public struct MethodDescriptor: Hashable {
284
342
285
343
public init (
286
344
documentation: String ,
287
- name: Name ,
345
+ name: MethodName ,
288
346
isInputStreaming: Bool ,
289
347
isOutputStreaming: Bool ,
290
348
inputType: String ,
@@ -299,7 +357,94 @@ public struct MethodDescriptor: Hashable {
299
357
}
300
358
}
301
359
360
+ extension MethodDescriptor {
361
+ @available ( * , deprecated, message: " Use MethodName instead of Name " )
362
+ public init (
363
+ documentation: String ,
364
+ name: Name ,
365
+ isInputStreaming: Bool ,
366
+ isOutputStreaming: Bool ,
367
+ inputType: String ,
368
+ outputType: String
369
+ ) {
370
+ self . documentation = documentation
371
+ self . name = MethodName (
372
+ identifyingName: name. base,
373
+ typeName: name. generatedUpperCase,
374
+ functionName: name. generatedLowerCase
375
+ )
376
+ self . isInputStreaming = isInputStreaming
377
+ self . isOutputStreaming = isOutputStreaming
378
+ self . inputType = inputType
379
+ self . outputType = outputType
380
+ }
381
+ }
382
+
383
+ public struct ServiceName : Hashable {
384
+ /// The identifying name as used in the service/method descriptors including any namespace.
385
+ ///
386
+ /// This value is also used to identify the service to the remote peer, usually as part of the
387
+ /// ":path" pseudoheader if doing gRPC over HTTP/2.
388
+ ///
389
+ /// If the service is declared in package "foo.bar" and the service is called "Baz" then this
390
+ /// value should be "foo.bar.Baz".
391
+ public var identifyingName : String
392
+
393
+ /// The name as used on types including any namespace.
394
+ ///
395
+ /// This is used to generate a namespace for each service which contains a number of client and
396
+ /// server protocols and concrete types.
397
+ ///
398
+ /// If the service is declared in package "foo.bar" and the service is called "Baz" then this
399
+ /// value should be "Foo\_Bar\_Baz".
400
+ public var typeName : String
401
+
402
+ /// The name as used as a property.
403
+ ///
404
+ /// This is used to provide a convenience getter for a descriptor of the service.
405
+ ///
406
+ /// If the service is declared in package "foo.bar" and the service is called "Baz" then this
407
+ /// value should be "foo\_bar\_Baz".
408
+ public var propertyName : String
409
+
410
+ public init ( identifyingName: String , typeName: String , propertyName: String ) {
411
+ self . identifyingName = identifyingName
412
+ self . typeName = typeName
413
+ self . propertyName = propertyName
414
+ }
415
+ }
416
+
417
+ public struct MethodName : Hashable {
418
+ /// The identifying name as used in the service/method descriptors.
419
+ ///
420
+ /// This value is also used to identify the method to the remote peer, usually as part of the
421
+ /// ":path" pseudoheader if doing gRPC over HTTP/2.
422
+ ///
423
+ /// This value typically starts with an uppercase character, for example "Get".
424
+ public var identifyingName : String
425
+
426
+ /// The name as used on types including any namespace.
427
+ ///
428
+ /// This is used to generate a namespace for each method which contains information about
429
+ /// the method.
430
+ ///
431
+ /// This value typically starts with an uppercase character, for example "Get".
432
+ public var typeName : String
433
+
434
+ /// The name as used as a property.
435
+ ///
436
+ /// This value typically starts with an lowercase character, for example "get".
437
+ public var functionName : String
438
+
439
+ public init ( identifyingName: String , typeName: String , functionName: String ) {
440
+ self . identifyingName = identifyingName
441
+ self . typeName = typeName
442
+ self . functionName = functionName
443
+ }
444
+ }
445
+
302
446
/// Represents the name associated with a namespace, service or a method, in three different formats.
447
+ @available ( * , deprecated, message: " Use ServiceName/MethodName instead. " )
303
448
public struct Name : Hashable {
304
449
/// The base name is the name used for the namespace/service/method in the IDL file, so it should follow
305
450
/// the specific casing of the IDL.
@@ -327,6 +472,7 @@ public struct Name: Hashable {
327
472
}
328
473
}
329
474
475
+ @available ( * , deprecated, message: " Use ServiceName/MethodName instead. " )
330
476
extension Name {
331
477
/// The base name replacing occurrences of "." with "_".
332
478
///
@@ -335,17 +481,3 @@ extension Name {
335
481
return self . base. replacing ( " . " , with: " _ " )
336
482
}
337
483
}
338
-
339
- extension ServiceDescriptor {
340
- var namespacedServicePropertyName : String {
341
- let prefix : String
342
-
343
- if self . namespace. normalizedBase. isEmpty {
344
- prefix = " "
345
- } else {
346
- prefix = self . namespace. normalizedBase + " _ "
347
- }
348
-
349
- return prefix + self . name. normalizedBase
350
- }
351
- }
0 commit comments