| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- /*
- * 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
- }
- }
|