Skip to content

Commit c960d06

Browse files
authoredNov 18, 2024
Add structured swift represntation for the generated metadata (#2117)
Motivation: The code gen as it stands tests too much in one go: a small change leads to many tests being updated. This stems from the translator not being very testable. To improve this, and make future code gen changes less painful, we can refactor it such that helpers build structured swift. These are significantly less coupled and can be tested more easily. The strategy here is to add all the structured representations for the metadata, client, and server and then cut over the translators to using the new code. This first change will introduce the structured represntation for metadata. Modifications: - Add metadata structured swift - Add helpers for commonly used types Result: Metadata is in place
1 parent dd22b39 commit c960d06

6 files changed

+755
-2
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,304 @@
1+
/*
2+
* Copyright 2024, gRPC Authors All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
extension TypealiasDescription {
18+
/// `typealias Input = <name>`
19+
package static func methodInput(
20+
accessModifier: AccessModifier? = nil,
21+
name: String
22+
) -> Self {
23+
return TypealiasDescription(
24+
accessModifier: accessModifier,
25+
name: "Input",
26+
existingType: .member(name)
27+
)
28+
}
29+
30+
/// `typealias Output = <name>`
31+
package static func methodOutput(
32+
accessModifier: AccessModifier? = nil,
33+
name: String
34+
) -> Self {
35+
return TypealiasDescription(
36+
accessModifier: accessModifier,
37+
name: "Output",
38+
existingType: .member(name)
39+
)
40+
}
41+
}
42+
43+
extension VariableDescription {
44+
/// ```
45+
/// static let descriptor = GRPCCore.MethodDescriptor(
46+
/// service: <serviceNamespace>.descriptor.fullyQualifiedService,
47+
/// method: "<literalMethodName>"
48+
/// ```
49+
package static func methodDescriptor(
50+
accessModifier: AccessModifier? = nil,
51+
serviceNamespace: String,
52+
literalMethodName: String
53+
) -> Self {
54+
return VariableDescription(
55+
accessModifier: accessModifier,
56+
isStatic: true,
57+
kind: .let,
58+
left: .identifier(.pattern("descriptor")),
59+
right: .functionCall(
60+
FunctionCallDescription(
61+
calledExpression: .identifierType(.methodDescriptor),
62+
arguments: [
63+
FunctionArgumentDescription(
64+
label: "service",
65+
expression: .identifierType(
66+
.member([serviceNamespace, "descriptor"])
67+
).dot("fullyQualifiedService")
68+
),
69+
FunctionArgumentDescription(
70+
label: "method",
71+
expression: .literal(literalMethodName)
72+
),
73+
]
74+
)
75+
)
76+
)
77+
}
78+
79+
/// ```
80+
/// static let descriptor = GRPCCore.ServiceDescriptor.<namespacedProperty>
81+
/// ```
82+
package static func serviceDescriptor(
83+
accessModifier: AccessModifier? = nil,
84+
namespacedProperty: String
85+
) -> Self {
86+
return VariableDescription(
87+
accessModifier: accessModifier,
88+
isStatic: true,
89+
kind: .let,
90+
left: .identifierPattern("descriptor"),
91+
right: .identifier(.type(.serviceDescriptor)).dot(namespacedProperty)
92+
)
93+
}
94+
}
95+
96+
extension ExtensionDescription {
97+
/// ```
98+
/// extension GRPCCore.ServiceDescriptor {
99+
/// static let <PropertyName> = Self(
100+
/// package: "<LiteralNamespaceName>",
101+
/// service: "<LiteralServiceName>"
102+
/// )
103+
/// }
104+
/// ```
105+
package static func serviceDescriptor(
106+
accessModifier: AccessModifier? = nil,
107+
propertyName: String,
108+
literalNamespace: String,
109+
literalService: String
110+
) -> ExtensionDescription {
111+
return ExtensionDescription(
112+
onType: "GRPCCore.ServiceDescriptor",
113+
declarations: [
114+
.variable(
115+
accessModifier: accessModifier,
116+
isStatic: true,
117+
kind: .let,
118+
left: .identifier(.pattern(propertyName)),
119+
right: .functionCall(
120+
calledExpression: .identifierType(.member("Self")),
121+
arguments: [
122+
FunctionArgumentDescription(
123+
label: "package",
124+
expression: .literal(literalNamespace)
125+
),
126+
FunctionArgumentDescription(
127+
label: "service",
128+
expression: .literal(literalService)
129+
),
130+
]
131+
)
132+
)
133+
]
134+
)
135+
}
136+
}
137+
138+
extension VariableDescription {
139+
/// ```
140+
/// static let descriptors: [GRPCCore.MethodDescriptor] = [<Name1>.descriptor, ...]
141+
/// ```
142+
package static func methodDescriptorsArray(
143+
accessModifier: AccessModifier? = nil,
144+
methodNamespaceNames names: [String]
145+
) -> Self {
146+
return VariableDescription(
147+
accessModifier: accessModifier,
148+
isStatic: true,
149+
kind: .let,
150+
left: .identifier(.pattern("descriptors")),
151+
type: .array(.methodDescriptor),
152+
right: .literal(.array(names.map { name in .identifierPattern(name).dot("descriptor") }))
153+
)
154+
}
155+
}
156+
157+
extension EnumDescription {
158+
/// ```
159+
/// enum <Method> {
160+
/// typealias Input = <InputType>
161+
/// typealias Output = <OutputType>
162+
/// static let descriptor = GRPCCore.MethodDescriptor(
163+
/// service: <ServiceNamespace>.descriptor.fullyQualifiedService,
164+
/// method: "<LiteralMethod>"
165+
/// )
166+
/// }
167+
/// ```
168+
package static func methodNamespace(
169+
accessModifier: AccessModifier? = nil,
170+
name: String,
171+
literalMethod: String,
172+
serviceNamespace: String,
173+
inputType: String,
174+
outputType: String
175+
) -> Self {
176+
return EnumDescription(
177+
accessModifier: accessModifier,
178+
name: name,
179+
members: [
180+
.typealias(.methodInput(accessModifier: accessModifier, name: inputType)),
181+
.typealias(.methodOutput(accessModifier: accessModifier, name: outputType)),
182+
.variable(
183+
.methodDescriptor(
184+
accessModifier: accessModifier,
185+
serviceNamespace: serviceNamespace,
186+
literalMethodName: literalMethod
187+
)
188+
),
189+
]
190+
)
191+
}
192+
193+
/// ```
194+
/// enum Method {
195+
/// enum <Method> {
196+
/// typealias Input = <MethodInput>
197+
/// typealias Output = <MethodOutput>
198+
/// static let descriptor = GRPCCore.MethodDescriptor(
199+
/// service: <serviceNamespaceName>.descriptor.fullyQualifiedService,
200+
/// method: "<Method>"
201+
/// )
202+
/// }
203+
/// ...
204+
/// static let descriptors: [GRPCCore.MethodDescriptor] = [
205+
/// <Method>.descriptor,
206+
/// ...
207+
/// ]
208+
/// }
209+
/// ```
210+
package static func methodsNamespace(
211+
accessModifier: AccessModifier? = nil,
212+
serviceNamespace: String,
213+
methods: [MethodDescriptor]
214+
) -> EnumDescription {
215+
var description = EnumDescription(accessModifier: accessModifier, name: "Method")
216+
217+
// Add a namespace for each method.
218+
let methodNamespaces: [Declaration] = methods.map { method in
219+
return .enum(
220+
.methodNamespace(
221+
accessModifier: accessModifier,
222+
name: method.name.base,
223+
literalMethod: method.name.base,
224+
serviceNamespace: serviceNamespace,
225+
inputType: method.inputType,
226+
outputType: method.outputType
227+
)
228+
)
229+
}
230+
description.members.append(contentsOf: methodNamespaces)
231+
232+
// Add an array of method descriptors
233+
let methodDescriptorsArray: VariableDescription = .methodDescriptorsArray(
234+
accessModifier: accessModifier,
235+
methodNamespaceNames: methods.map { $0.name.base }
236+
)
237+
description.members.append(.variable(methodDescriptorsArray))
238+
239+
return description
240+
}
241+
242+
/// ```
243+
/// enum <Name> {
244+
/// static let descriptor = GRPCCore.ServiceDescriptor.<namespacedServicePropertyName>
245+
/// enum Method {
246+
/// ...
247+
/// }
248+
/// @available(...)
249+
/// typealias StreamingServiceProtocol = ...
250+
/// @available(...)
251+
/// typealias ServiceProtocol = ...
252+
/// ...
253+
/// }
254+
/// ```
255+
package static func serviceNamespace(
256+
accessModifier: AccessModifier? = nil,
257+
name: String,
258+
serviceDescriptorProperty: String,
259+
client: Bool,
260+
server: Bool,
261+
methods: [MethodDescriptor]
262+
) -> EnumDescription {
263+
var description = EnumDescription(accessModifier: accessModifier, name: name)
264+
265+
// static let descriptor = GRPCCore.ServiceDescriptor.<namespacedServicePropertyName>
266+
let descriptor = VariableDescription.serviceDescriptor(
267+
accessModifier: accessModifier,
268+
namespacedProperty: serviceDescriptorProperty
269+
)
270+
description.members.append(.variable(descriptor))
271+
272+
// enum Method { ... }
273+
let methodsNamespace: EnumDescription = .methodsNamespace(
274+
accessModifier: accessModifier,
275+
serviceNamespace: name,
276+
methods: methods
277+
)
278+
description.members.append(.enum(methodsNamespace))
279+
280+
// Typealiases for the various protocols.
281+
var typealiasNames: [String] = []
282+
if server {
283+
typealiasNames.append("StreamingServiceProtocol")
284+
typealiasNames.append("ServiceProtocol")
285+
}
286+
if client {
287+
typealiasNames.append("ClientProtocol")
288+
typealiasNames.append("Client")
289+
}
290+
let typealiases: [Declaration] = typealiasNames.map { alias in
291+
.guarded(
292+
.grpc,
293+
.typealias(
294+
accessModifier: accessModifier,
295+
name: alias,
296+
existingType: .member(name + "_" + alias)
297+
)
298+
)
299+
}
300+
description.members.append(contentsOf: typealiases)
301+
302+
return description
303+
}
304+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright 2024, gRPC Authors All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
extension AvailabilityDescription {
18+
package static let grpc = AvailabilityDescription(
19+
osVersions: [
20+
OSVersion(os: .macOS, version: "15.0"),
21+
OSVersion(os: .iOS, version: "18.0"),
22+
OSVersion(os: .watchOS, version: "11.0"),
23+
OSVersion(os: .tvOS, version: "18.0"),
24+
OSVersion(os: .visionOS, version: "2.0"),
25+
]
26+
)
27+
}
28+
29+
extension ExistingTypeDescription {
30+
fileprivate static func grpcCore(_ typeName: String) -> Self {
31+
return .member(["GRPCCore", typeName])
32+
}
33+
34+
fileprivate static func requestResponse(
35+
for type: String?,
36+
isRequest: Bool,
37+
isStreaming: Bool,
38+
isClient: Bool
39+
) -> Self {
40+
let prefix = isStreaming ? "Streaming" : ""
41+
let peer = isClient ? "Client" : "Server"
42+
let kind = isRequest ? "Request" : "Response"
43+
let baseType: Self = .grpcCore(prefix + peer + kind)
44+
45+
if let type = type {
46+
return .generic(wrapper: baseType, wrapped: .member(type))
47+
} else {
48+
return baseType
49+
}
50+
}
51+
52+
package static func serverRequest(forType type: String?, streaming: Bool) -> Self {
53+
return .requestResponse(for: type, isRequest: true, isStreaming: streaming, isClient: false)
54+
}
55+
56+
package static func serverResponse(forType type: String?, streaming: Bool) -> Self {
57+
return .requestResponse(for: type, isRequest: false, isStreaming: streaming, isClient: false)
58+
}
59+
60+
package static func clientRequest(forType type: String?, streaming: Bool) -> Self {
61+
return .requestResponse(for: type, isRequest: true, isStreaming: streaming, isClient: true)
62+
}
63+
64+
package static func clientResponse(forType type: String?, streaming: Bool) -> Self {
65+
return .requestResponse(for: type, isRequest: false, isStreaming: streaming, isClient: true)
66+
}
67+
68+
package static let serverContext: Self = .grpcCore("ServerContext")
69+
package static let rpcRouter: Self = .grpcCore("RPCRouter")
70+
package static let serviceDescriptor: Self = .grpcCore("ServiceDescriptor")
71+
package static let methodDescriptor: Self = .grpcCore("MethodDescriptor")
72+
73+
package static func serializer(forType type: String) -> Self {
74+
.generic(wrapper: .grpcCore("MessageSerializer"), wrapped: .member(type))
75+
}
76+
77+
package static func deserializer(forType type: String) -> Self {
78+
.generic(wrapper: .grpcCore("MessageDeserializer"), wrapped: .member(type))
79+
}
80+
81+
package static func rpcWriter(forType type: String) -> Self {
82+
.generic(wrapper: .grpcCore("RPCWriter"), wrapped: .member(type))
83+
}
84+
85+
package static let callOptions: Self = .grpcCore("CallOptions")
86+
package static let metadata: Self = .grpcCore("Metadata")
87+
package static let grpcClient: Self = .grpcCore("GRPCClient")
88+
}

‎Sources/GRPCCodeGen/Internal/StructuredSwiftRepresentation.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ struct ImportDescription: Equatable, Codable, Sendable {
100100
/// A description of an access modifier.
101101
///
102102
/// For example: `public`.
103-
internal enum AccessModifier: String, Sendable, Equatable, Codable {
103+
internal enum AccessModifier: String, Sendable, Equatable, Codable, CaseIterable {
104104
/// A declaration accessible outside of the module.
105105
case `public`
106106

‎Sources/GRPCCodeGen/Internal/Translator/IDLToStructuredSwiftTranslator.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ struct IDLToStructuredSwiftTranslator: Translator {
8787
}
8888

8989
extension AccessModifier {
90-
fileprivate init(_ accessLevel: SourceGenerator.Config.AccessLevel) {
90+
init(_ accessLevel: SourceGenerator.Config.AccessLevel) {
9191
switch accessLevel.level {
9292
case .internal: self = .internal
9393
case .package: self = .package
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,295 @@
1+
/*
2+
* Copyright 2024, gRPC Authors All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import Testing
18+
19+
@testable import GRPCCodeGen
20+
21+
extension StructuedSwiftTests {
22+
@Suite("Metadata")
23+
struct Metadata {
24+
@Test("@available(...)")
25+
func grpcAvailability() async throws {
26+
let availability: AvailabilityDescription = .grpc
27+
let structDecl = StructDescription(name: "Ignored")
28+
let expected = """
29+
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
30+
struct Ignored {}
31+
"""
32+
33+
#expect(render(.guarded(availability, .struct(structDecl))) == expected)
34+
}
35+
36+
@Test("typealias Input = <Name>", arguments: AccessModifier.allCases)
37+
func methodInputTypealias(access: AccessModifier) {
38+
let decl: TypealiasDescription = .methodInput(accessModifier: access, name: "Foo")
39+
let expected = "\(access) typealias Input = Foo"
40+
#expect(render(.typealias(decl)) == expected)
41+
}
42+
43+
@Test("typealias Output = <Name>", arguments: AccessModifier.allCases)
44+
func methodOutputTypealias(access: AccessModifier) {
45+
let decl: TypealiasDescription = .methodOutput(accessModifier: access, name: "Foo")
46+
let expected = "\(access) typealias Output = Foo"
47+
#expect(render(.typealias(decl)) == expected)
48+
}
49+
50+
@Test(
51+
"static let descriptor = GRPCCore.MethodDescriptor(...)",
52+
arguments: AccessModifier.allCases
53+
)
54+
func staticMethodDescriptorProperty(access: AccessModifier) {
55+
let decl: VariableDescription = .methodDescriptor(
56+
accessModifier: access,
57+
serviceNamespace: "FooService",
58+
literalMethodName: "Bar"
59+
)
60+
61+
let expected = """
62+
\(access) static let descriptor = GRPCCore.MethodDescriptor(
63+
service: FooService.descriptor.fullyQualifiedService,
64+
method: "Bar"
65+
)
66+
"""
67+
#expect(render(.variable(decl)) == expected)
68+
}
69+
70+
@Test(
71+
"static let descriptor = GRPCCore.ServiceDescriptor.<Name>",
72+
arguments: AccessModifier.allCases
73+
)
74+
func staticServiceDescriptorProperty(access: AccessModifier) {
75+
let decl: VariableDescription = .serviceDescriptor(
76+
accessModifier: access,
77+
namespacedProperty: "foo"
78+
)
79+
80+
let expected = "\(access) static let descriptor = GRPCCore.ServiceDescriptor.foo"
81+
#expect(render(.variable(decl)) == expected)
82+
}
83+
84+
@Test("extension GRPCCore.ServiceDescriptor { ... }", arguments: AccessModifier.allCases)
85+
func staticServiceDescriptorPropertyExtension(access: AccessModifier) {
86+
let decl: ExtensionDescription = .serviceDescriptor(
87+
accessModifier: access,
88+
propertyName: "foo",
89+
literalNamespace: "echo",
90+
literalService: "EchoService"
91+
)
92+
93+
let expected = """
94+
extension GRPCCore.ServiceDescriptor {
95+
\(access) static let foo = Self(
96+
package: "echo",
97+
service: "EchoService"
98+
)
99+
}
100+
"""
101+
#expect(render(.extension(decl)) == expected)
102+
}
103+
104+
@Test(
105+
"static let descriptors: [GRPCCore.MethodDescriptor] = [...]",
106+
arguments: AccessModifier.allCases
107+
)
108+
func staticMethodDescriptorsArray(access: AccessModifier) {
109+
let decl: VariableDescription = .methodDescriptorsArray(
110+
accessModifier: access,
111+
methodNamespaceNames: ["Foo", "Bar", "Baz"]
112+
)
113+
114+
let expected = """
115+
\(access) static let descriptors: [GRPCCore.MethodDescriptor] = [
116+
Foo.descriptor,
117+
Bar.descriptor,
118+
Baz.descriptor
119+
]
120+
"""
121+
#expect(render(.variable(decl)) == expected)
122+
}
123+
124+
@Test("enum <Method> { ... }", arguments: AccessModifier.allCases)
125+
func methodNamespaceEnum(access: AccessModifier) {
126+
let decl: EnumDescription = .methodNamespace(
127+
accessModifier: access,
128+
name: "Foo",
129+
literalMethod: "Foo",
130+
serviceNamespace: "Bar_Baz",
131+
inputType: "FooInput",
132+
outputType: "FooOutput"
133+
)
134+
135+
let expected = """
136+
\(access) enum Foo {
137+
\(access) typealias Input = FooInput
138+
\(access) typealias Output = FooOutput
139+
\(access) static let descriptor = GRPCCore.MethodDescriptor(
140+
service: Bar_Baz.descriptor.fullyQualifiedService,
141+
method: "Foo"
142+
)
143+
}
144+
"""
145+
#expect(render(.enum(decl)) == expected)
146+
}
147+
148+
@Test("enum Method { ... }", arguments: AccessModifier.allCases)
149+
func methodsNamespaceEnum(access: AccessModifier) {
150+
let decl: EnumDescription = .methodsNamespace(
151+
accessModifier: access,
152+
serviceNamespace: "Bar_Baz",
153+
methods: [
154+
.init(
155+
documentation: "",
156+
name: .init(base: "Foo", generatedUpperCase: "Foo", generatedLowerCase: "foo"),
157+
isInputStreaming: false,
158+
isOutputStreaming: false,
159+
inputType: "FooInput",
160+
outputType: "FooOutput"
161+
)
162+
]
163+
)
164+
165+
let expected = """
166+
\(access) enum Method {
167+
\(access) enum Foo {
168+
\(access) typealias Input = FooInput
169+
\(access) typealias Output = FooOutput
170+
\(access) static let descriptor = GRPCCore.MethodDescriptor(
171+
service: Bar_Baz.descriptor.fullyQualifiedService,
172+
method: "Foo"
173+
)
174+
}
175+
\(access) static let descriptors: [GRPCCore.MethodDescriptor] = [
176+
Foo.descriptor
177+
]
178+
}
179+
"""
180+
#expect(render(.enum(decl)) == expected)
181+
}
182+
183+
@Test("enum Method { ... } (no methods)", arguments: AccessModifier.allCases)
184+
func methodsNamespaceEnumNoMethods(access: AccessModifier) {
185+
let decl: EnumDescription = .methodsNamespace(
186+
accessModifier: access,
187+
serviceNamespace: "Bar_Baz",
188+
methods: []
189+
)
190+
191+
let expected = """
192+
\(access) enum Method {
193+
\(access) static let descriptors: [GRPCCore.MethodDescriptor] = []
194+
}
195+
"""
196+
#expect(render(.enum(decl)) == expected)
197+
}
198+
199+
@Test("enum <Service> { ... }", arguments: AccessModifier.allCases)
200+
func serviceNamespaceEnum(access: AccessModifier) {
201+
let decl: EnumDescription = .serviceNamespace(
202+
accessModifier: access,
203+
name: "Foo",
204+
serviceDescriptorProperty: "foo",
205+
client: false,
206+
server: false,
207+
methods: [
208+
.init(
209+
documentation: "",
210+
name: .init(base: "Bar", generatedUpperCase: "Bar", generatedLowerCase: "bar"),
211+
isInputStreaming: false,
212+
isOutputStreaming: false,
213+
inputType: "BarInput",
214+
outputType: "BarOutput"
215+
)
216+
]
217+
)
218+
219+
let expected = """
220+
\(access) enum Foo {
221+
\(access) static let descriptor = GRPCCore.ServiceDescriptor.foo
222+
\(access) enum Method {
223+
\(access) enum Bar {
224+
\(access) typealias Input = BarInput
225+
\(access) typealias Output = BarOutput
226+
\(access) static let descriptor = GRPCCore.MethodDescriptor(
227+
service: Foo.descriptor.fullyQualifiedService,
228+
method: "Bar"
229+
)
230+
}
231+
\(access) static let descriptors: [GRPCCore.MethodDescriptor] = [
232+
Bar.descriptor
233+
]
234+
}
235+
}
236+
"""
237+
#expect(render(.enum(decl)) == expected)
238+
}
239+
240+
@Test(
241+
"enum <Service> { ... } (no methods)",
242+
arguments: AccessModifier.allCases,
243+
[(true, true), (false, false), (true, false), (false, true)]
244+
)
245+
func serviceNamespaceEnumNoMethods(access: AccessModifier, config: (client: Bool, server: Bool))
246+
{
247+
let decl: EnumDescription = .serviceNamespace(
248+
accessModifier: access,
249+
name: "Foo",
250+
serviceDescriptorProperty: "foo",
251+
client: config.client,
252+
server: config.server,
253+
methods: []
254+
)
255+
256+
var expected = """
257+
\(access) enum Foo {
258+
\(access) static let descriptor = GRPCCore.ServiceDescriptor.foo
259+
\(access) enum Method {
260+
\(access) static let descriptors: [GRPCCore.MethodDescriptor] = []
261+
}\n
262+
"""
263+
264+
if config.server {
265+
expected += """
266+
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
267+
\(access) typealias StreamingServiceProtocol = Foo_StreamingServiceProtocol
268+
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
269+
\(access) typealias ServiceProtocol = Foo_ServiceProtocol
270+
"""
271+
}
272+
273+
if config.client {
274+
if config.server {
275+
expected += "\n"
276+
}
277+
278+
expected += """
279+
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
280+
\(access) typealias ClientProtocol = Foo_ClientProtocol
281+
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
282+
\(access) typealias Client = Foo_Client
283+
"""
284+
}
285+
286+
if config.client || config.server {
287+
expected += "\n}"
288+
} else {
289+
expected += "}"
290+
}
291+
292+
#expect(render(.enum(decl)) == expected)
293+
}
294+
}
295+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2024, gRPC Authors All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import Testing
18+
19+
@testable import GRPCCodeGen
20+
21+
// Used as a namespace for organising other structured swift tests.
22+
@Suite("Structued Swift")
23+
struct StructuedSwiftTests {}
24+
25+
func render(_ declaration: Declaration) -> String {
26+
let renderer = TextBasedRenderer(indentation: 2)
27+
renderer.renderDeclaration(declaration)
28+
return renderer.renderedContents()
29+
}
30+
31+
func render(_ expression: Expression) -> String {
32+
let renderer = TextBasedRenderer(indentation: 2)
33+
renderer.renderExpression(expression)
34+
return renderer.renderedContents()
35+
}
36+
37+
func render(_ blocks: [CodeBlock]) -> String {
38+
let renderer = TextBasedRenderer(indentation: 2)
39+
renderer.renderCodeBlocks(blocks)
40+
return renderer.renderedContents()
41+
}
42+
43+
enum RPCKind: Hashable, Sendable, CaseIterable {
44+
case unary
45+
case clientStreaming
46+
case serverStreaming
47+
case bidirectionalStreaming
48+
49+
var streamsInput: Bool {
50+
switch self {
51+
case .clientStreaming, .bidirectionalStreaming:
52+
return true
53+
case .unary, .serverStreaming:
54+
return false
55+
}
56+
}
57+
58+
var streamsOutput: Bool {
59+
switch self {
60+
case .serverStreaming, .bidirectionalStreaming:
61+
return true
62+
case .unary, .clientStreaming:
63+
return false
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)
Please sign in to comment.