diff --git a/swift/swift_client.go b/swift/swift_client.go index 8c05fce..ad34d68 100644 --- a/swift/swift_client.go +++ b/swift/swift_client.go @@ -13,7 +13,7 @@ import ( ) const ( - version = "1.0.0" + version = "1.0.1" lang = "swift" defaultClass = "RPCAPI" @@ -77,6 +77,7 @@ type templateData struct { Methods []Method Models []Model Namespaces []NamespaceData + IsPublic bool } type NamespaceData struct { @@ -94,6 +95,7 @@ type Settings struct { Class string TypeMapper TypeMapper IsProtocol bool + IsPublic bool } func NewClient(schema smd.Schema, settings Settings) *Generator { @@ -102,7 +104,7 @@ func NewClient(schema smd.Schema, settings Settings) *Generator { // Generate returns generated Swift client func (g *Generator) Generate() ([]byte, error) { - data := templateData{Class: defaultClass, GeneratorData: gen.DefaultGeneratorData().AddLangAndLocalVersion(version, lang)} + data := templateData{Class: defaultClass, IsPublic: g.settings.IsPublic, GeneratorData: gen.DefaultGeneratorData().AddLangAndLocalVersion(version, lang)} if g.settings.Class != "" { data.Class = g.settings.Class } diff --git a/swift/swift_client_test.go b/swift/swift_client_test.go index 2f2ac0e..1697668 100644 --- a/swift/swift_client_test.go +++ b/swift/swift_client_test.go @@ -11,6 +11,7 @@ import ( ) const rpcGenFilePath = "./testdata/rpc.generated.swift" +const rpcPublicGenFilePath = "./testdata/rpc.public.generated.swift" const protocolGenFilePath = "./testdata/protocol.generated.swift" var update = flag.Bool("update", false, "update .swift files") @@ -36,6 +37,16 @@ func TestGenerator_Generate(t *testing.T) { }, outputFile: rpcGenFilePath, }, + { + name: "generate public rpc", + fields: fields{ + servicesMap: map[string]zenrpc.Invoker{ + "arith": testdata.ArithService{}, + }, + settings: Settings{IsPublic: true}, + }, + outputFile: rpcPublicGenFilePath, + }, { name: "generate multi protocol", fields: fields{ diff --git a/swift/swift_template.go b/swift/swift_template.go index 522ea0e..dfd0416 100644 --- a/swift/swift_template.go +++ b/swift/swift_template.go @@ -5,7 +5,7 @@ const client = `/// Code generated from jsonrpc schema by rpcgen v{{ .Version }} import Foundation extension {{ .Class }}: RPCMethod { - public var rpcMethod: String { + {{ if .IsPublic }}public {{ end }}var rpcMethod: String { switch self { case .batch(let requests): return requests.compactMap { $0.rpcMethod }.joined(separator: ",") {{- range .Methods }} @@ -16,7 +16,7 @@ extension {{ .Class }}: RPCMethod { } extension {{ .Class }}: RPCParameters { - public var rpcParameters: [String: Any?]? { + {{ if .IsPublic }}public {{ end }}var rpcParameters: [String: Any?]? { switch self { case .batch: return nil @@ -30,7 +30,7 @@ extension {{ .Class }}: RPCParameters { } } -public enum {{ .Class }}: Codable, Hashable { +{{ if .IsPublic }}public {{ end }}enum {{ .Class }}: Codable, Hashable { /// Make batch requests. case batch(requests: [{{ .Class }}]) {{ range .Methods }}{{- $paramsLen := len .Parameters }} @@ -49,7 +49,7 @@ public enum {{ .Class }}: Codable, Hashable { {{- if .Description }} /// {{ .Description }} {{- end }} -public struct {{ .Name }}: Codable, Hashable { +{{ if $.IsPublic }}public {{ end }}struct {{ .Name }}: Codable, Hashable { {{- $fieldsLen := len .Fields }}{{- range .Fields }} {{- if .Description }} /// {{ .Description }} @@ -68,7 +68,7 @@ public struct {{ .Name }}: Codable, Hashable { {{ end }} extension {{ .Class }} { - public var rpcId: String? { + {{ if .IsPublic }}public {{ end }}var rpcId: String? { switch self { case .batch: return nil diff --git a/swift/testdata/protocol.generated.swift b/swift/testdata/protocol.generated.swift index 9342c20..a1e05f0 100644 --- a/swift/testdata/protocol.generated.swift +++ b/swift/testdata/protocol.generated.swift @@ -1,4 +1,4 @@ -/// Code generated from jsonrpc schema by rpcgen v2.5.x with swift v1.0.0; DO NOT EDIT. +/// Code generated from jsonrpc schema by rpcgen v2.5.x with swift v1.0.1; DO NOT EDIT. import Foundation diff --git a/swift/testdata/rpc.generated.swift b/swift/testdata/rpc.generated.swift index adf72a1..f00dfbd 100644 --- a/swift/testdata/rpc.generated.swift +++ b/swift/testdata/rpc.generated.swift @@ -1,9 +1,9 @@ -/// Code generated from jsonrpc schema by rpcgen v2.5.x with swift v1.0.0; DO NOT EDIT. +/// Code generated from jsonrpc schema by rpcgen v2.5.x with swift v1.0.1; DO NOT EDIT. import Foundation extension RPCAPI: RPCMethod { - public var rpcMethod: String { + var rpcMethod: String { switch self { case .batch(let requests): return requests.compactMap { $0.rpcMethod }.joined(separator: ",") case .arithCheckError: return "arith.CheckError" @@ -24,7 +24,7 @@ extension RPCAPI: RPCMethod { } extension RPCAPI: RPCParameters { - public var rpcParameters: [String: Any?]? { + var rpcParameters: [String: Any?]? { switch self { case .batch: return nil @@ -70,7 +70,7 @@ extension RPCAPI: RPCParameters { } } -public enum RPCAPI: Codable, Hashable { +enum RPCAPI: Codable, Hashable { /// Make batch requests. case batch(requests: [RPCAPI]) @@ -110,7 +110,7 @@ public enum RPCAPI: Codable, Hashable { } -public struct CycleInitStruct: Codable, Hashable { +struct CycleInitStruct: Codable, Hashable { @DecodableDefault.False var isCycleInit: Bool init(isCycleInit: Bool) { @@ -118,7 +118,7 @@ public struct CycleInitStruct: Codable, Hashable { } } -public struct ExternalData: Codable, Hashable { +struct ExternalData: Codable, Hashable { @DecodableDefault.EmptyString var name: String init(name: String) { @@ -126,7 +126,7 @@ public struct ExternalData: Codable, Hashable { } } -public struct Point: Codable, Hashable { +struct Point: Codable, Hashable { /// coordinate @DecodableDefault.IntegerZero var X: Int @@ -210,7 +210,7 @@ public struct Point: Codable, Hashable { } } -public struct Quotient: Codable, Hashable { +struct Quotient: Codable, Hashable { /// Quo docs @DecodableDefault.IntegerZero var Quo: Int @@ -232,7 +232,7 @@ public struct Quotient: Codable, Hashable { extension RPCAPI { - public var rpcId: String? { + var rpcId: String? { switch self { case .batch: return nil diff --git a/swift/testdata/rpc.public.generated.swift b/swift/testdata/rpc.public.generated.swift new file mode 100644 index 0000000..023f5fa --- /dev/null +++ b/swift/testdata/rpc.public.generated.swift @@ -0,0 +1,256 @@ +/// Code generated from jsonrpc schema by rpcgen v2.5.x with swift v1.0.1; DO NOT EDIT. + +import Foundation + +extension RPCAPI: RPCMethod { + public var rpcMethod: String { + switch self { + case .batch(let requests): return requests.compactMap { $0.rpcMethod }.joined(separator: ",") + case .arithCheckError: return "arith.CheckError" + case .arithCheckZenRPCError: return "arith.CheckZenRPCError" + case .arithDivide: return "arith.Divide" + case .arithDoSomething: return "arith.DoSomething" + case .arithDoSomethingV2: return "arith.DoSomethingV2" + case .arithDoSomethingWithPoint: return "arith.DoSomethingWithPoint" + case .arithGetPoints: return "arith.GetPoints" + case .arithMultiply: return "arith.Multiply" + case .arithPi: return "arith.Pi" + case .arithPositive: return "arith.Positive" + case .arithPow: return "arith.Pow" + case .arithSum: return "arith.Sum" + case .arithSumArray: return "arith.SumArray" + } + } +} + +extension RPCAPI: RPCParameters { + public var rpcParameters: [String: Any?]? { + switch self { + case .batch: + return nil + case let .arithCheckError(isErr, _): + return ["isErr": isErr] + + case let .arithCheckZenRPCError(isErr, _): + return ["isErr": isErr] + + case let .arithDivide(a, b, _): + return ["a": a, "b": b] + + case .arithDoSomething: + return nil + + case .arithDoSomethingV2: + return nil + + case let .arithDoSomethingWithPoint(p, pp, _): + return ["p": p.any, "pp": pp.any] + + case .arithGetPoints: + return nil + + case let .arithMultiply(a, b, _): + return ["a": a, "b": b] + + case .arithPi: + return nil + + case .arithPositive: + return nil + + case let .arithPow(base, exp, _): + return ["base": base, "exp": exp] + + case let .arithSum(a, b, _): + return ["a": a, "b": b] + + case let .arithSumArray(array, _): + return ["array": array.any] + } + } +} + +public enum RPCAPI: Codable, Hashable { + /// Make batch requests. + case batch(requests: [RPCAPI]) + + /// CheckError throws error is isErr true. + /// TEST row 2 + case arithCheckError(isErr: Bool, requestId: String? = nil) + /// CheckZenRPCError throws zenrpc error is isErr true. + /// Second description row + case arithCheckZenRPCError(isErr: Bool, requestId: String? = nil) + /// Divide divides two numbers. + /// - Returns: Quotient? + case arithDivide(a: Int, b: Int, requestId: String? = nil) + case arithDoSomething(requestId: String? = nil) + /// - Returns: ExternalData + case arithDoSomethingV2(requestId: String? = nil) + /// - Returns: Point + case arithDoSomethingWithPoint(p: Point, pp: [Point], requestId: String? = nil) + /// - Returns: [Point] + case arithGetPoints(requestId: String? = nil) + /// Multiply multiples two digits and returns result. + /// - Returns: Int + case arithMultiply(a: Int, b: Int, requestId: String? = nil) + /// Pi returns math.Pi. + /// - Returns: Double + case arithPi(requestId: String? = nil) + /// - Returns: Bool + case arithPositive(requestId: String? = nil) + /// Pow returns x**y, the base-x exponential of y. If Exp is not set then default value is 2. + /// - Returns: Double + case arithPow(base: Double, exp: Double?, requestId: String? = nil) + /// Sum sums two digits and returns error with error code as result and IP from context. + /// - Returns: Bool + case arithSum(a: Int, b: Int, requestId: String? = nil) + /// SumArray returns sum all items from array + /// - Returns: Double + case arithSumArray(array: [Double]?, requestId: String? = nil) +} + + +public struct CycleInitStruct: Codable, Hashable { + @DecodableDefault.False + var isCycleInit: Bool + init(isCycleInit: Bool) { + self.isCycleInit = isCycleInit + } +} + +public struct ExternalData: Codable, Hashable { + @DecodableDefault.EmptyString + var name: String + init(name: String) { + self.name = name + } +} + +public struct Point: Codable, Hashable { + /// coordinate + @DecodableDefault.IntegerZero + var X: Int + /// coordinate + @DecodableDefault.IntegerZero + var Y: Int + /// version group float - 1 + @DecodableDefault.DoubleZero + var baseFloat: Double + /// version id - 2 + @DecodableDefault.IntegerZero + var baseId: Int + @DecodableDefault.EmptyString + var `class`: String + /// version date - 1 + @DecodableDefault.EmptyString + var createdAt: String + var emptyString: String? + /// version id - 1 + @DecodableDefault.IntegerZero + var id: Int + /// version group geo coordinate № - 2 + @DecodableDefault.DoubleZero + var lat: Double + /// version group geo coordinate № - 3 + @DecodableDefault.DoubleZero + var latitude: Double + /// version group geo coordinate № - 2 + @DecodableDefault.DoubleZero + var lon: Double + /// version group geo coordinate № - 3 + @DecodableDefault.DoubleZero + var longitude: Double + /// version date - 3 + @DecodableDefault.EmptyString + var manualChangedAt: String + @DecodableDefault.EmptyString + var name: String + /// version group geo coordinate № - 1 + @DecodableDefault.DoubleZero + var newLat: Double + /// version group geo coordinate № - 1 + @DecodableDefault.DoubleZero + var newLon: Double + var nextQuotient: Quotient + /// version group float - 2 + @DecodableDefault.DoubleZero + var secondFloat: Double + /// version id - 3 + @DecodableDefault.IntegerZero + var secondID: Int + @DecodableDefault.EmptyList + var secondPoints: [Point] + var secondQuotient: Quotient? + /// version date - 2 + @DecodableDefault.EmptyString + var updatedAt: String + init(X: Int, Y: Int, baseFloat: Double, baseId: Int, class: String, createdAt: String, emptyString: String? = nil, id: Int, lat: Double, latitude: Double, lon: Double, longitude: Double, manualChangedAt: String, name: String, newLat: Double, newLon: Double, nextQuotient: Quotient, secondFloat: Double, secondID: Int, secondPoints: [Point], secondQuotient: Quotient? = nil, updatedAt: String) { + self.X = X + self.Y = Y + self.baseFloat = baseFloat + self.baseId = baseId + self.class = `class` + self.createdAt = createdAt + self.emptyString = emptyString + self.id = id + self.lat = lat + self.latitude = latitude + self.lon = lon + self.longitude = longitude + self.manualChangedAt = manualChangedAt + self.name = name + self.newLat = newLat + self.newLon = newLon + self.nextQuotient = nextQuotient + self.secondFloat = secondFloat + self.secondID = secondID + self.secondPoints = secondPoints + self.secondQuotient = secondQuotient + self.updatedAt = updatedAt + } +} + +public struct Quotient: Codable, Hashable { + /// Quo docs + @DecodableDefault.IntegerZero + var Quo: Int + @DecodableDefault.EmptyString + var baseRow: String + var data: CycleInitStruct + /// Rem docs + @DecodableDefault.IntegerZero + var rem: Int + var rowNil: String? + init(Quo: Int, baseRow: String, data: CycleInitStruct, rem: Int, rowNil: String? = nil) { + self.Quo = Quo + self.baseRow = baseRow + self.data = data + self.rem = rem + self.rowNil = rowNil + } +} + + +extension RPCAPI { + public var rpcId: String? { + switch self { + case .batch: + return nil + + case.arithCheckError(_, let requestId), + .arithCheckZenRPCError(_, let requestId), + .arithDivide(_, _, let requestId), + .arithDoSomething(let requestId), + .arithDoSomethingV2(let requestId), + .arithDoSomethingWithPoint(_, _, let requestId), + .arithGetPoints(let requestId), + .arithMultiply(_, _, let requestId), + .arithPi(let requestId), + .arithPositive(let requestId), + .arithPow(_, _, let requestId), + .arithSum(_, _, let requestId), + .arithSumArray(_, let requestId): + return requestId + } + } +}