Skip to content
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
6 changes: 4 additions & 2 deletions swift/swift_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

const (
version = "1.0.0"
version = "1.0.1"
lang = "swift"
defaultClass = "RPCAPI"

Expand Down Expand Up @@ -77,6 +77,7 @@ type templateData struct {
Methods []Method
Models []Model
Namespaces []NamespaceData
IsPublic bool
}

type NamespaceData struct {
Expand All @@ -94,6 +95,7 @@ type Settings struct {
Class string
TypeMapper TypeMapper
IsProtocol bool
IsPublic bool
}

func NewClient(schema smd.Schema, settings Settings) *Generator {
Expand All @@ -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
}
Expand Down
11 changes: 11 additions & 0 deletions swift/swift_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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{
Expand Down
10 changes: 5 additions & 5 deletions swift/swift_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand All @@ -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
Expand All @@ -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 }}
Expand All @@ -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 }}
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion swift/testdata/protocol.generated.swift
Original file line number Diff line number Diff line change
@@ -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

Expand Down
18 changes: 9 additions & 9 deletions swift/testdata/rpc.generated.swift
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -70,7 +70,7 @@ extension RPCAPI: RPCParameters {
}
}

public enum RPCAPI: Codable, Hashable {
enum RPCAPI: Codable, Hashable {
/// Make batch requests.
case batch(requests: [RPCAPI])

Expand Down Expand Up @@ -110,23 +110,23 @@ public enum RPCAPI: Codable, Hashable {
}


public struct CycleInitStruct: Codable, Hashable {
struct CycleInitStruct: Codable, Hashable {
@DecodableDefault.False
var isCycleInit: Bool
init(isCycleInit: Bool) {
self.isCycleInit = isCycleInit
}
}

public struct ExternalData: Codable, Hashable {
struct ExternalData: Codable, Hashable {
@DecodableDefault.EmptyString
var name: String
init(name: String) {
self.name = name
}
}

public struct Point: Codable, Hashable {
struct Point: Codable, Hashable {
/// coordinate
@DecodableDefault.IntegerZero
var X: Int
Expand Down Expand Up @@ -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
Expand All @@ -232,7 +232,7 @@ public struct Quotient: Codable, Hashable {


extension RPCAPI {
public var rpcId: String? {
var rpcId: String? {
switch self {
case .batch:
return nil
Expand Down
Loading
Loading