Skip to content

Commit e0d4c44

Browse files
authored
Merge pull request #40 from qnqatop/update-ios-template
Swift: Update template
2 parents 5821c60 + eea6bfb commit e0d4c44

6 files changed

Lines changed: 286 additions & 17 deletions

File tree

swift/swift_client.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
)
1414

1515
const (
16-
version = "1.0.0"
16+
version = "1.0.1"
1717
lang = "swift"
1818
defaultClass = "RPCAPI"
1919

@@ -77,6 +77,7 @@ type templateData struct {
7777
Methods []Method
7878
Models []Model
7979
Namespaces []NamespaceData
80+
IsPublic bool
8081
}
8182

8283
type NamespaceData struct {
@@ -94,6 +95,7 @@ type Settings struct {
9495
Class string
9596
TypeMapper TypeMapper
9697
IsProtocol bool
98+
IsPublic bool
9799
}
98100

99101
func NewClient(schema smd.Schema, settings Settings) *Generator {
@@ -102,7 +104,7 @@ func NewClient(schema smd.Schema, settings Settings) *Generator {
102104

103105
// Generate returns generated Swift client
104106
func (g *Generator) Generate() ([]byte, error) {
105-
data := templateData{Class: defaultClass, GeneratorData: gen.DefaultGeneratorData().AddLangAndLocalVersion(version, lang)}
107+
data := templateData{Class: defaultClass, IsPublic: g.settings.IsPublic, GeneratorData: gen.DefaultGeneratorData().AddLangAndLocalVersion(version, lang)}
106108
if g.settings.Class != "" {
107109
data.Class = g.settings.Class
108110
}

swift/swift_client_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
)
1212

1313
const rpcGenFilePath = "./testdata/rpc.generated.swift"
14+
const rpcPublicGenFilePath = "./testdata/rpc.public.generated.swift"
1415
const protocolGenFilePath = "./testdata/protocol.generated.swift"
1516

1617
var update = flag.Bool("update", false, "update .swift files")
@@ -36,6 +37,16 @@ func TestGenerator_Generate(t *testing.T) {
3637
},
3738
outputFile: rpcGenFilePath,
3839
},
40+
{
41+
name: "generate public rpc",
42+
fields: fields{
43+
servicesMap: map[string]zenrpc.Invoker{
44+
"arith": testdata.ArithService{},
45+
},
46+
settings: Settings{IsPublic: true},
47+
},
48+
outputFile: rpcPublicGenFilePath,
49+
},
3950
{
4051
name: "generate multi protocol",
4152
fields: fields{

swift/swift_template.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const client = `/// Code generated from jsonrpc schema by rpcgen v{{ .Version }}
55
import Foundation
66
77
extension {{ .Class }}: RPCMethod {
8-
public var rpcMethod: String {
8+
{{ if .IsPublic }}public {{ end }}var rpcMethod: String {
99
switch self {
1010
case .batch(let requests): return requests.compactMap { $0.rpcMethod }.joined(separator: ",")
1111
{{- range .Methods }}
@@ -16,7 +16,7 @@ extension {{ .Class }}: RPCMethod {
1616
}
1717
1818
extension {{ .Class }}: RPCParameters {
19-
public var rpcParameters: [String: Any?]? {
19+
{{ if .IsPublic }}public {{ end }}var rpcParameters: [String: Any?]? {
2020
switch self {
2121
case .batch:
2222
return nil
@@ -30,7 +30,7 @@ extension {{ .Class }}: RPCParameters {
3030
}
3131
}
3232
33-
public enum {{ .Class }}: Codable, Hashable {
33+
{{ if .IsPublic }}public {{ end }}enum {{ .Class }}: Codable, Hashable {
3434
/// Make batch requests.
3535
case batch(requests: [{{ .Class }}])
3636
{{ range .Methods }}{{- $paramsLen := len .Parameters }}
@@ -49,7 +49,7 @@ public enum {{ .Class }}: Codable, Hashable {
4949
{{- if .Description }}
5050
/// {{ .Description }}
5151
{{- end }}
52-
public struct {{ .Name }}: Codable, Hashable {
52+
{{ if $.IsPublic }}public {{ end }}struct {{ .Name }}: Codable, Hashable {
5353
{{- $fieldsLen := len .Fields }}{{- range .Fields }}
5454
{{- if .Description }}
5555
/// {{ .Description }}
@@ -68,7 +68,7 @@ public struct {{ .Name }}: Codable, Hashable {
6868
{{ end }}
6969
7070
extension {{ .Class }} {
71-
public var rpcId: String? {
71+
{{ if .IsPublic }}public {{ end }}var rpcId: String? {
7272
switch self {
7373
case .batch:
7474
return nil

swift/testdata/protocol.generated.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/// Code generated from jsonrpc schema by rpcgen v2.5.x with swift v1.0.0; DO NOT EDIT.
1+
/// Code generated from jsonrpc schema by rpcgen v2.5.x with swift v1.0.1; DO NOT EDIT.
22

33
import Foundation
44

swift/testdata/rpc.generated.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
/// Code generated from jsonrpc schema by rpcgen v2.5.x with swift v1.0.0; DO NOT EDIT.
1+
/// Code generated from jsonrpc schema by rpcgen v2.5.x with swift v1.0.1; DO NOT EDIT.
22

33
import Foundation
44

55
extension RPCAPI: RPCMethod {
6-
public var rpcMethod: String {
6+
var rpcMethod: String {
77
switch self {
88
case .batch(let requests): return requests.compactMap { $0.rpcMethod }.joined(separator: ",")
99
case .arithCheckError: return "arith.CheckError"
@@ -24,7 +24,7 @@ extension RPCAPI: RPCMethod {
2424
}
2525

2626
extension RPCAPI: RPCParameters {
27-
public var rpcParameters: [String: Any?]? {
27+
var rpcParameters: [String: Any?]? {
2828
switch self {
2929
case .batch:
3030
return nil
@@ -70,7 +70,7 @@ extension RPCAPI: RPCParameters {
7070
}
7171
}
7272

73-
public enum RPCAPI: Codable, Hashable {
73+
enum RPCAPI: Codable, Hashable {
7474
/// Make batch requests.
7575
case batch(requests: [RPCAPI])
7676

@@ -110,23 +110,23 @@ public enum RPCAPI: Codable, Hashable {
110110
}
111111

112112

113-
public struct CycleInitStruct: Codable, Hashable {
113+
struct CycleInitStruct: Codable, Hashable {
114114
@DecodableDefault.False
115115
var isCycleInit: Bool
116116
init(isCycleInit: Bool) {
117117
self.isCycleInit = isCycleInit
118118
}
119119
}
120120

121-
public struct ExternalData: Codable, Hashable {
121+
struct ExternalData: Codable, Hashable {
122122
@DecodableDefault.EmptyString
123123
var name: String
124124
init(name: String) {
125125
self.name = name
126126
}
127127
}
128128

129-
public struct Point: Codable, Hashable {
129+
struct Point: Codable, Hashable {
130130
/// coordinate
131131
@DecodableDefault.IntegerZero
132132
var X: Int
@@ -210,7 +210,7 @@ public struct Point: Codable, Hashable {
210210
}
211211
}
212212

213-
public struct Quotient: Codable, Hashable {
213+
struct Quotient: Codable, Hashable {
214214
/// Quo docs
215215
@DecodableDefault.IntegerZero
216216
var Quo: Int
@@ -232,7 +232,7 @@ public struct Quotient: Codable, Hashable {
232232

233233

234234
extension RPCAPI {
235-
public var rpcId: String? {
235+
var rpcId: String? {
236236
switch self {
237237
case .batch:
238238
return nil

0 commit comments

Comments
 (0)