Skip to content

Add structured swift represntation for the generated metadata #2117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
304 changes: 304 additions & 0 deletions Sources/GRPCCodeGen/Internal/StructuredSwift+ServiceMetadata.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,304 @@
/*
* Copyright 2024, gRPC Authors All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

extension TypealiasDescription {
/// `typealias Input = <name>`
package static func methodInput(
accessModifier: AccessModifier? = nil,
name: String
) -> Self {
return TypealiasDescription(
accessModifier: accessModifier,
name: "Input",
existingType: .member(name)
)
}

/// `typealias Output = <name>`
package static func methodOutput(
accessModifier: AccessModifier? = nil,
name: String
) -> Self {
return TypealiasDescription(
accessModifier: accessModifier,
name: "Output",
existingType: .member(name)
)
}
}

extension VariableDescription {
/// ```
/// static let descriptor = GRPCCore.MethodDescriptor(
/// service: <serviceNamespace>.descriptor.fullyQualifiedService,
/// method: "<literalMethodName>"
/// ```
package static func methodDescriptor(
accessModifier: AccessModifier? = nil,
serviceNamespace: String,
literalMethodName: String
) -> Self {
return VariableDescription(
accessModifier: accessModifier,
isStatic: true,
kind: .let,
left: .identifier(.pattern("descriptor")),
right: .functionCall(
FunctionCallDescription(
calledExpression: .identifierType(.methodDescriptor),
arguments: [
FunctionArgumentDescription(
label: "service",
expression: .identifierType(
.member([serviceNamespace, "descriptor"])
).dot("fullyQualifiedService")
),
FunctionArgumentDescription(
label: "method",
expression: .literal(literalMethodName)
),
]
)
)
)
}

/// ```
/// static let descriptor = GRPCCore.ServiceDescriptor.<namespacedProperty>
/// ```
package static func serviceDescriptor(
accessModifier: AccessModifier? = nil,
namespacedProperty: String
) -> Self {
return VariableDescription(
accessModifier: accessModifier,
isStatic: true,
kind: .let,
left: .identifierPattern("descriptor"),
right: .identifier(.type(.serviceDescriptor)).dot(namespacedProperty)
)
}
}

extension ExtensionDescription {
/// ```
/// extension GRPCCore.ServiceDescriptor {
/// static let <PropertyName> = Self(
/// package: "<LiteralNamespaceName>",
/// service: "<LiteralServiceName>"
/// )
/// }
/// ```
package static func serviceDescriptor(
accessModifier: AccessModifier? = nil,
propertyName: String,
literalNamespace: String,
literalService: String
) -> ExtensionDescription {
return ExtensionDescription(
onType: "GRPCCore.ServiceDescriptor",
declarations: [
.variable(
accessModifier: accessModifier,
isStatic: true,
kind: .let,
left: .identifier(.pattern(propertyName)),
right: .functionCall(
calledExpression: .identifierType(.member("Self")),
arguments: [
FunctionArgumentDescription(
label: "package",
expression: .literal(literalNamespace)
),
FunctionArgumentDescription(
label: "service",
expression: .literal(literalService)
),
]
)
)
]
)
}
}

extension VariableDescription {
/// ```
/// static let descriptors: [GRPCCore.MethodDescriptor] = [<Name1>.descriptor, ...]
/// ```
package static func methodDescriptorsArray(
accessModifier: AccessModifier? = nil,
methodNamespaceNames names: [String]
) -> Self {
return VariableDescription(
accessModifier: accessModifier,
isStatic: true,
kind: .let,
left: .identifier(.pattern("descriptors")),
type: .array(.methodDescriptor),
right: .literal(.array(names.map { name in .identifierPattern(name).dot("descriptor") }))
)
}
}

extension EnumDescription {
/// ```
/// enum <Method> {
/// typealias Input = <InputType>
/// typealias Output = <OutputType>
/// static let descriptor = GRPCCore.MethodDescriptor(
/// service: <ServiceNamespace>.descriptor.fullyQualifiedService,
/// method: "<LiteralMethod>"
/// )
/// }
/// ```
package static func methodNamespace(
accessModifier: AccessModifier? = nil,
name: String,
literalMethod: String,
serviceNamespace: String,
inputType: String,
outputType: String
) -> Self {
return EnumDescription(
accessModifier: accessModifier,
name: name,
members: [
.typealias(.methodInput(accessModifier: accessModifier, name: inputType)),
.typealias(.methodOutput(accessModifier: accessModifier, name: outputType)),
.variable(
.methodDescriptor(
accessModifier: accessModifier,
serviceNamespace: serviceNamespace,
literalMethodName: literalMethod
)
),
]
)
}

/// ```
/// enum Method {
/// enum <Method> {
/// typealias Input = <MethodInput>
/// typealias Output = <MethodOutput>
/// static let descriptor = GRPCCore.MethodDescriptor(
/// service: <serviceNamespaceName>.descriptor.fullyQualifiedService,
/// method: "<Method>"
/// )
/// }
/// ...
/// static let descriptors: [GRPCCore.MethodDescriptor] = [
/// <Method>.descriptor,
/// ...
/// ]
/// }
/// ```
package static func methodsNamespace(
accessModifier: AccessModifier? = nil,
serviceNamespace: String,
methods: [MethodDescriptor]
) -> EnumDescription {
var description = EnumDescription(accessModifier: accessModifier, name: "Method")

// Add a namespace for each method.
let methodNamespaces: [Declaration] = methods.map { method in
return .enum(
.methodNamespace(
accessModifier: accessModifier,
name: method.name.base,
literalMethod: method.name.base,
serviceNamespace: serviceNamespace,
inputType: method.inputType,
outputType: method.outputType
)
)
}
description.members.append(contentsOf: methodNamespaces)

// Add an array of method descriptors
let methodDescriptorsArray: VariableDescription = .methodDescriptorsArray(
accessModifier: accessModifier,
methodNamespaceNames: methods.map { $0.name.base }
)
description.members.append(.variable(methodDescriptorsArray))

return description
}

/// ```
/// enum <Name> {
/// static let descriptor = GRPCCore.ServiceDescriptor.<namespacedServicePropertyName>
/// enum Method {
/// ...
/// }
/// @available(...)
/// typealias StreamingServiceProtocol = ...
/// @available(...)
/// typealias ServiceProtocol = ...
/// ...
/// }
/// ```
package static func serviceNamespace(
accessModifier: AccessModifier? = nil,
name: String,
serviceDescriptorProperty: String,
client: Bool,
server: Bool,
methods: [MethodDescriptor]
) -> EnumDescription {
var description = EnumDescription(accessModifier: accessModifier, name: name)

// static let descriptor = GRPCCore.ServiceDescriptor.<namespacedServicePropertyName>
let descriptor = VariableDescription.serviceDescriptor(
accessModifier: accessModifier,
namespacedProperty: serviceDescriptorProperty
)
description.members.append(.variable(descriptor))

// enum Method { ... }
let methodsNamespace: EnumDescription = .methodsNamespace(
accessModifier: accessModifier,
serviceNamespace: name,
methods: methods
)
description.members.append(.enum(methodsNamespace))

// Typealiases for the various protocols.
var typealiasNames: [String] = []
if server {
typealiasNames.append("StreamingServiceProtocol")
typealiasNames.append("ServiceProtocol")
}
if client {
typealiasNames.append("ClientProtocol")
typealiasNames.append("Client")
}
let typealiases: [Declaration] = typealiasNames.map { alias in
.guarded(
.grpc,
.typealias(
accessModifier: accessModifier,
name: alias,
existingType: .member(name + "_" + alias)
)
)
}
description.members.append(contentsOf: typealiases)

return description
}
}
88 changes: 88 additions & 0 deletions Sources/GRPCCodeGen/Internal/StructuredSwift+Types.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright 2024, gRPC Authors All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

extension AvailabilityDescription {
package static let grpc = AvailabilityDescription(
osVersions: [
OSVersion(os: .macOS, version: "15.0"),
OSVersion(os: .iOS, version: "18.0"),
OSVersion(os: .watchOS, version: "11.0"),
OSVersion(os: .tvOS, version: "18.0"),
OSVersion(os: .visionOS, version: "2.0"),
]
)
}

extension ExistingTypeDescription {
fileprivate static func grpcCore(_ typeName: String) -> Self {
return .member(["GRPCCore", typeName])
}

fileprivate static func requestResponse(
for type: String?,
isRequest: Bool,
isStreaming: Bool,
isClient: Bool
) -> Self {
let prefix = isStreaming ? "Streaming" : ""
let peer = isClient ? "Client" : "Server"
let kind = isRequest ? "Request" : "Response"
let baseType: Self = .grpcCore(prefix + peer + kind)

if let type = type {
return .generic(wrapper: baseType, wrapped: .member(type))
} else {
return baseType
}
}

package static func serverRequest(forType type: String?, streaming: Bool) -> Self {
return .requestResponse(for: type, isRequest: true, isStreaming: streaming, isClient: false)
}

package static func serverResponse(forType type: String?, streaming: Bool) -> Self {
return .requestResponse(for: type, isRequest: false, isStreaming: streaming, isClient: false)
}

package static func clientRequest(forType type: String?, streaming: Bool) -> Self {
return .requestResponse(for: type, isRequest: true, isStreaming: streaming, isClient: true)
}

package static func clientResponse(forType type: String?, streaming: Bool) -> Self {
return .requestResponse(for: type, isRequest: false, isStreaming: streaming, isClient: true)
}

package static let serverContext: Self = .grpcCore("ServerContext")
package static let rpcRouter: Self = .grpcCore("RPCRouter")
package static let serviceDescriptor: Self = .grpcCore("ServiceDescriptor")
package static let methodDescriptor: Self = .grpcCore("MethodDescriptor")

package static func serializer(forType type: String) -> Self {
.generic(wrapper: .grpcCore("MessageSerializer"), wrapped: .member(type))
}

package static func deserializer(forType type: String) -> Self {
.generic(wrapper: .grpcCore("MessageDeserializer"), wrapped: .member(type))
}

package static func rpcWriter(forType type: String) -> Self {
.generic(wrapper: .grpcCore("RPCWriter"), wrapped: .member(type))
}

package static let callOptions: Self = .grpcCore("CallOptions")
package static let metadata: Self = .grpcCore("Metadata")
package static let grpcClient: Self = .grpcCore("GRPCClient")
}
Loading
Loading