Skip to content

Commit

Permalink
Merge pull request vmkteam#22 from sas1024/swift-client
Browse files Browse the repository at this point in the history
Implement Swift rpc client generation, use go 1.18
  • Loading branch information
sas1024 authored Dec 9, 2022
2 parents d46a63d + 990fd40 commit 19ff0d5
Show file tree
Hide file tree
Showing 26 changed files with 1,016 additions and 69 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.16
go-version: 1.18

- name: Build
run: go build -v ./...
Expand Down
48 changes: 44 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Golang
- PHP
- TypeScript
- Swift
- OpenRPC schema

## Examples
Expand All @@ -19,7 +20,7 @@ import (
"fmt"
"log"

"github.com/vmkteam/rpcgen"
"github.com/vmkteam/rpcgen/v2"
"github.com/vmkteam/zenrpc/v2"
)

Expand All @@ -43,7 +44,8 @@ package main
import (
"net/http"

"github.com/vmkteam/rpcgen"
"github.com/vmkteam/rpcgen/v2"
"github.com/vmkteam/rpcgen/v2/swift"
"github.com/vmkteam/zenrpc/v2"
)

Expand All @@ -55,6 +57,7 @@ func main () {
http.HandleFunc("/client.go", rpcgen.Handler(gen.GoClient()))
http.HandleFunc("/client.ts", rpcgen.Handler(gen.TSClient(nil)))
http.HandleFunc("/RpcClient.php", rpcgen.Handler(gen.PHPClient("")))
http.HandleFunc("/client.swift", rpcgen.Handler(gen.SwiftClient(swift.Settings{})))
}
```

Expand All @@ -64,10 +67,11 @@ func main () {
package main

import (
"fmt"
"net/http"

"github.com/vmkteam/rpcgen"
"github.com/vmkteam/rpcgen/typescript"
"github.com/vmkteam/rpcgen/v2"
"github.com/vmkteam/rpcgen/v2/typescript"
"github.com/vmkteam/zenrpc/v2"
"github.com/vmkteam/zenrpc/v2/smd"
)
Expand All @@ -90,3 +94,39 @@ func main() {
http.HandleFunc("/client.ts", rpcgen.Handler(gen.TSClient(typeMapper)))
}
```

### Add custom Swift type mapper

```go
package main

import (
"fmt"
"net/http"

"github.com/vmkteam/rpcgen/v2"
"github.com/vmkteam/rpcgen/v2/swift"
"github.com/vmkteam/zenrpc/v2"
"github.com/vmkteam/zenrpc/v2/smd"
)

func main() {
rpc := zenrpc.NewServer(zenrpc.Options{})

gen := rpcgen.FromSMD(rpc.SMD())

typeMapper := func(typeName string, in smd.Property, param swift.Parameter) swift.Parameter {
switch typeName {
case "Group":
switch in.Name {
case "groups":
param.Type = fmt.Sprintf("[Int: %s]", param.Type)
param.DecodableDefault = swift.DefaultMap
}
}
return param
}

http.HandleFunc("/client.swift", rpcgen.Handler(gen.SwiftClient(swift.Settings{"", typeMapper})))
}
```
15 changes: 15 additions & 0 deletions gen/gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package gen

const version = "2.4.0"

const DefinitionsPrefix = "#/definitions/"

type GeneratorData struct {
Version string
}

func DefaultGeneratorData() GeneratorData {
return GeneratorData{
Version: version,
}
}
5 changes: 5 additions & 0 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/vmkteam/rpcgen/v2/golang"
"github.com/vmkteam/rpcgen/v2/openrpc"
"github.com/vmkteam/rpcgen/v2/php"
"github.com/vmkteam/rpcgen/v2/swift"
"github.com/vmkteam/rpcgen/v2/typescript"

smd1 "github.com/vmkteam/zenrpc/smd"
Expand Down Expand Up @@ -37,6 +38,10 @@ func (g RPCGen) TSCustomClient(settings typescript.Settings) Generator {
return typescript.NewClient(g.schema, settings)
}

func (g RPCGen) SwiftClient(settings swift.Settings) Generator {
return swift.NewClient(g.schema, settings)
}

func (g RPCGen) OpenRPC(title, host string) Generator {
return openrpc.NewClient(g.schema, title, host)
}
Expand Down
20 changes: 18 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
module github.com/vmkteam/rpcgen/v2

go 1.16
go 1.18

require (
github.com/vmkteam/meta-schema/v2 v2.0.1
github.com/vmkteam/zenrpc v1.1.1
github.com/vmkteam/zenrpc/v2 v2.2.7
github.com/vmkteam/zenrpc/v2 v2.2.9
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/iancoleman/orderedmap v0.2.0 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/prometheus/client_golang v1.13.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
golang.org/x/sys v0.0.0-20220615213510-4f61da869c0c // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/protobuf v1.28.1 // indirect
)
384 changes: 367 additions & 17 deletions go.sum

Large diffs are not rendered by default.

13 changes: 3 additions & 10 deletions golang/go_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,10 @@ import (
"text/template"
"unicode"

"github.com/vmkteam/rpcgen/v2/gen"
"github.com/vmkteam/zenrpc/v2/smd"
)

const (
definitionsPrefix = "#/definitions/"

typeArray = "array"
typeInteger = "integer"
typeNumber = "number"
typeObject = "object"
typeInt = "int"
)

// Generator main package structure
type Generator struct {
schema Schema
Expand All @@ -32,6 +23,8 @@ func NewClient(schema smd.Schema) *Generator {

// Generate returns generated Go client.
func (g *Generator) Generate() ([]byte, error) {
g.schema.GeneratorData = gen.DefaultGeneratorData()

tmpl, err := template.New("").Funcs(templateFuncs).Parse(goTpl)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion golang/go_template.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package golang

// goTpl contains template for Go client
const goTpl = `// Code generated from jsonrpc schema by rpc_client_generator; DO NOT EDIT.
const goTpl = `// Code generated from jsonrpc schema by rpcgen v{{ .Version }}; DO NOT EDIT.
package client
Expand Down
5 changes: 5 additions & 0 deletions golang/rpcgen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"testing"

"github.com/vmkteam/rpcgen/v2/gen"
"github.com/vmkteam/zenrpc/v2"
"github.com/vmkteam/zenrpc/v2/testdata"
)
Expand All @@ -27,6 +28,10 @@ func TestGenerateGoClient(t *testing.T) {
t.Fatalf("open test data file: %v", err)
}

// cut version from comparsion
generated = bytes.ReplaceAll(generated, []byte("v"+gen.DefaultGeneratorData().Version), []byte(""))
testData = bytes.ReplaceAll(testData, []byte("v"+gen.DefaultGeneratorData().Version), []byte(""))

if !bytes.Equal(generated, testData) {
t.Fatalf("bad generator output")
}
Expand Down
8 changes: 5 additions & 3 deletions golang/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strconv"
"strings"

"github.com/vmkteam/rpcgen/v2/gen"
"github.com/vmkteam/zenrpc/v2/smd"
)

Expand All @@ -28,6 +29,7 @@ func NewSchema(schema smd.Schema) Schema {
}

type Schema struct {
gen.GeneratorData
Namespaces []Namespace
}

Expand Down Expand Up @@ -317,7 +319,7 @@ func newValue(in smd.JSONSchema, namespace, methodName string, isParam, isReturn
value.ArrayItemType = in.Items["type"]
} else { // complex type
value.ArrayItemType = "object"
value.ModelName = strings.TrimPrefix(in.Items["$ref"], definitionsPrefix)
value.ModelName = strings.TrimPrefix(in.Items["$ref"], gen.DefinitionsPrefix)
}
}

Expand Down Expand Up @@ -355,7 +357,7 @@ func newValueFromProp(in smd.Property) Value {

// in is an object
if in.Type == smd.Object && in.Ref != "" {
value.ModelName = strings.TrimPrefix(in.Ref, definitionsPrefix)
value.ModelName = strings.TrimPrefix(in.Ref, gen.DefinitionsPrefix)
}

// in is an array
Expand All @@ -364,7 +366,7 @@ func newValueFromProp(in smd.Property) Value {
value.ArrayItemType = in.Items["type"]
} else { // complex type
value.ArrayItemType = "object"
value.ModelName = strings.TrimPrefix(in.Items["$ref"], definitionsPrefix)
value.ModelName = strings.TrimPrefix(in.Items["$ref"], gen.DefinitionsPrefix)
}
}

Expand Down
12 changes: 4 additions & 8 deletions golang/testdata/catalogue_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 3 additions & 7 deletions openrpc/testdata/openrpc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"openrpc": "1.2.6",
"info": {
"title": "test",
"version": "v0.0.0-71a9f83c605fe392a7c9a74f5ee90342"
"version": "v0.0.0-9ac57ca8e88fda67daec2cbaa50d71bd"
},
"servers": [
{
Expand Down Expand Up @@ -41,7 +41,7 @@
},
{
"name": "arith.CheckZenRPCError",
"summary": "CheckError throws zenrpc error is isErr true.",
"summary": "CheckZenRPCError throws zenrpc error is isErr true.",
"tags": [
{
"name": "arith"
Expand Down Expand Up @@ -103,10 +103,6 @@
}
},
"errors": [
{
"code": -32603,
"message": "divide by zero"
},
{
"code": 401,
"message": "we do not serve 1"
Expand Down Expand Up @@ -200,7 +196,7 @@
},
{
"name": "arith.Pi",
"summary": "PI returns math.Pi.",
"summary": "Pi returns math.Pi.",
"tags": [
{
"name": "arith"
Expand Down
10 changes: 6 additions & 4 deletions php/php_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ import (
"text/template"
"time"

"github.com/vmkteam/rpcgen/v2/gen"
"github.com/vmkteam/zenrpc/v2/smd"
)

const (
definitionsPrefix = "#/definitions/"

defaultPhpNamespace = "JsonRpcClient"

phpBoolean = "bool"
Expand Down Expand Up @@ -42,6 +41,8 @@ func NewClient(schema smd.Schema, phpNamespace string) *Generator {
// Generate returns generate TypeScript client
func (g *Generator) Generate() ([]byte, error) {
m := g.PHPModels()
m.GeneratorData = gen.DefaultGeneratorData()

funcMap := template.FuncMap{
"now": time.Now,
}
Expand Down Expand Up @@ -85,6 +86,7 @@ type Parameter struct {
}

type phpModels struct {
gen.GeneratorData
Namespace string
Methods []phpMethod
Classes []phpClass
Expand Down Expand Up @@ -231,13 +233,13 @@ func objectType(ref string) string {
if ref == "" {
return phpObject
}
return strings.TrimPrefix(ref, definitionsPrefix)
return strings.TrimPrefix(ref, gen.DefinitionsPrefix)
}

// arrayType return array type from $ref
func arrayType(ref map[string]string) string {
if r, ok := ref["$ref"]; ok {
return strings.TrimPrefix(r, definitionsPrefix) + "[]"
return strings.TrimPrefix(r, gen.DefinitionsPrefix) + "[]"
}
return phpArray
}
Expand Down
Loading

0 comments on commit 19ff0d5

Please sign in to comment.