diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4092473..2dd8d08 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,13 +21,13 @@ jobs: uses: actions/setup-go@v3 with: go-version: ${{ matrix.go-version }} - - name: Install webrpc-gen - run: go install github.com/webrpc/webrpc/cmd/webrpc-gen@${{ matrix.webrpc-gen }} - # - name: Install webrpc-gen (development) - # run: | - # git clone --single-branch https://github.com/golang-cz/webrpc.git --branch templates_v0.7.0 - # cd webrpc - # make install + # - name: Install webrpc-gen + # run: go install github.com/webrpc/webrpc/cmd/webrpc-gen@${{ matrix.webrpc-gen }} + - name: Install webrpc-gen (development) + run: | + git clone --single-branch https://github.com/golang-cz/webrpc.git --branch message_to_struct + cd webrpc + make install - name: Regenerate examples run: cd _examples && make generate - name: Git diff of regenerated files diff --git a/_examples/golang-basics/README.md b/_examples/golang-basics/README.md index 1fe8117..f5b6490 100644 --- a/_examples/golang-basics/README.md +++ b/_examples/golang-basics/README.md @@ -13,7 +13,7 @@ you can also write your schema in JSON format like so, [./example.webrpc.json](. 2. Design your schema file and think about the methods calls clients will need to make to your service 3. Write the "services" section of the schema file -4. From the inputs and outputs for the function definitions, start writing the "messages" +4. From the inputs and outputs for the function definitions, start writing the "structs" section of the data types needed in your program. 5. Run the code generator to build the server and client: * `webrpc-gen -schema=example.ridl -target=golang -pkg=main -server -client -out=./example.gen.go` diff --git a/_examples/golang-basics/example.gen.go b/_examples/golang-basics/example.gen.go index abbba75..3b67e4f 100644 --- a/_examples/golang-basics/example.gen.go +++ b/_examples/golang-basics/example.gen.go @@ -1,6 +1,6 @@ -// example v0.0.1 c70fb537a22a95af4c8fadd6520eeb02aa38cdc6 +// example v0.0.1 cf0549342fa363009ec353971267af724cfc95aa // -- -// Code generated by webrpc-gen@v0.8.x-dev with custom generator. DO NOT EDIT. +// Code generated by webrpc-gen@v0.9.x-dev with custom generator. DO NOT EDIT. // // webrpc-gen -schema=example.ridl -target=../../../gen-golang -pkg=main -server -client -out=./example.gen.go package main @@ -31,7 +31,7 @@ func WebRPCSchemaVersion() string { // Schema hash generated from your RIDL schema func WebRPCSchemaHash() string { - return "c70fb537a22a95af4c8fadd6520eeb02aa38cdc6" + return "cf0549342fa363009ec353971267af724cfc95aa" } // diff --git a/_examples/golang-basics/example.ridl b/_examples/golang-basics/example.ridl index 008b968..9cb4d5a 100644 --- a/_examples/golang-basics/example.ridl +++ b/_examples/golang-basics/example.ridl @@ -8,7 +8,7 @@ enum Kind: uint32 - USER - ADMIN -message User +struct User - id: uint64 + json = id + go.field.name = ID @@ -32,7 +32,7 @@ message User - updatedAt?: timestamp + go.tag.db = updated_at -message Nickname +struct Nickname - ID: uint64 + go.tag.db = id - nickname: string @@ -42,15 +42,15 @@ message Nickname - updatedAt?: timestamp + go.tag.db = updated_at -message SearchFilter +struct SearchFilter - q: string -message Version +struct Version - webrpcVersion: string - schemaVersion: string - schemaHash: string -message ComplexType +struct ComplexType - meta: map - metaNestedExample: map> - namesList: []string diff --git a/_examples/golang-basics/example.webrpc.json b/_examples/golang-basics/example.webrpc.json deleted file mode 100644 index d046689..0000000 --- a/_examples/golang-basics/example.webrpc.json +++ /dev/null @@ -1,159 +0,0 @@ -{ - "webrpc": "v1", - "name": "example", - "version": "v0.0.1", - "messages": [ - { - "name": "Kind", - "type": "enum", - "fields": [ - { - "name": "USER", - "type": "uint32", - "value": "0" - }, - { - "name": "ADMIN", - "type": "uint32", - "value": "1" - } - ] - }, - { - "name": "Empty", - "type": "struct", - "fields": [ - ] - }, - { - "name": "User", - "type": "struct", - "fields": [ - { - "name": "ID", - "type": "uint64", - "optional": false, - "meta": [ - { "json": "id" }, - { "go.tag.db": "id" } - ] - }, - { - "name": "username", - "type": "string", - "optional": false, - "meta": [ - { "json": "USERNAME" }, - { "go.tag.db": "username" } - ] - }, - { - "name": "role", - "type": "string", - "optional": false, - "meta": [ - { "go.tag.db": "-" } - ] - }, - { - "name": "createdAt", - "type": "timestamp", - "optional": true, - "meta": [ - { "json": "created_at" }, - { "go.tag.json": "created_at,omitempty" }, - { "go.tag.db": "created_at" } - ] - } - - ] - }, - { - "name": "ComplexType", - "type": "struct", - "fields": [ - { - "name": "meta", - "type": "map" - }, - { - "name": "metaNestedExample", - "type": "map>" - }, - { - "name": "namesList", - "type": "[]string" - }, - { - "name": "numsList", - "type": "[]int64" - }, - { - "name": "doubleArray", - "type": "[][]string" - }, - { - "name": "listOfMaps", - "type": "[]map" - }, - { - "name": "listOfUsers", - "type": "[]User" - }, - { - "name": "mapOfUsers", - "type": "map" - }, - { - "name": "user", - "type": "User" - } - ] - } - ], - "services": [ - { - "name": "ExampleService", - "methods": [ - { - "name": "Ping", - "inputs": [], - "outputs": [] - }, - { - "name": "Status", - "inputs": [], - "outputs": [ - { - "name": "status", - "type": "bool" - } - ] - }, - { - "name": "GetUser", - "inputs": [ - { - "name": "header", - "type": "map" - }, - { - "name": "userID", - "type": "uint64" - } - ], - "outputs": [ - { - "name": "code", - "type": "uint32" - }, - { - "name": "user", - "type": "User" - } - ] - } - ] - } - ] -} diff --git a/_examples/golang-imports/api.gen.go b/_examples/golang-imports/api.gen.go index 6f43e54..3f0039a 100644 --- a/_examples/golang-imports/api.gen.go +++ b/_examples/golang-imports/api.gen.go @@ -1,6 +1,6 @@ -// example-api-service v1.0.0 483889fb084764e3a2565d2c6b3eae99f88f2481 +// example-api-service v1.0.0 e50900e0e33b565b6f3e09f67212232ddc1478bf // -- -// Code generated by webrpc-gen@v0.8.x-dev with custom generator. DO NOT EDIT. +// Code generated by webrpc-gen@v0.9.x-dev with custom generator. DO NOT EDIT. // // webrpc-gen -schema=./proto/api.ridl -target=../../../gen-golang -out=./api.gen.go -pkg=main -server -client package main @@ -30,7 +30,7 @@ func WebRPCSchemaVersion() string { // Schema hash generated from your RIDL schema func WebRPCSchemaHash() string { - return "483889fb084764e3a2565d2c6b3eae99f88f2481" + return "e50900e0e33b565b6f3e09f67212232ddc1478bf" } // diff --git a/_examples/golang-imports/proto/types.ridl b/_examples/golang-imports/proto/types.ridl index 16f2223..24f1e5b 100644 --- a/_examples/golang-imports/proto/types.ridl +++ b/_examples/golang-imports/proto/types.ridl @@ -1,5 +1,5 @@ webrpc = v1 -message User +struct User - username: string - age: uint32 diff --git a/_examples/golang-imports/proto/util.ridl b/_examples/golang-imports/proto/util.ridl index 55ceac2..ab0e73d 100644 --- a/_examples/golang-imports/proto/util.ridl +++ b/_examples/golang-imports/proto/util.ridl @@ -4,5 +4,5 @@ enum Location: uint32 - TORONTO - NEW_YORK -message Setting +struct Setting - config: map diff --git a/enum.go.tmpl b/enum.go.tmpl index b530836..ebd841c 100644 --- a/enum.go.tmpl +++ b/enum.go.tmpl @@ -1,46 +1,47 @@ {{- define "enum" -}} -{{- $enumName := .Name -}} -{{- $enumType := .EnumType -}} +{{- $name := .Name -}} +{{- $type := .Type -}} +{{- $fields := .Fields -}} -type {{$enumName}} {{$enumType}} +type {{$name}} {{$type}} const ( -{{- range .Fields}} - {{$enumName}}_{{.Name}} {{$enumName}} = {{.Value}} +{{- range $fields}} + {{$name}}_{{.Name}} {{$name}} = {{.Value}} {{- end}} ) -var {{$enumName}}_name = map[{{$enumType}}]string{ -{{- range .Fields}} +var {{$name}}_name = map[{{$type}}]string{ +{{- range $fields}} {{.Value}}: "{{.Name}}", {{- end}} } -var {{$enumName}}_value = map[string]{{$enumType}}{ -{{- range .Fields}} +var {{$name}}_value = map[string]{{$type}}{ +{{- range $fields}} "{{.Name}}": {{.Value}}, {{- end}} } -func (x {{$enumName}}) String() string { - return {{$enumName}}_name[{{$enumType}}(x)] +func (x {{$name}}) String() string { + return {{$name}}_name[{{$type}}(x)] } -func (x {{$enumName}}) MarshalJSON() ([]byte, error) { +func (x {{$name}}) MarshalJSON() ([]byte, error) { buf := bytes.NewBufferString(`"`) - buf.WriteString({{$enumName}}_name[{{$enumType}}(x)]) + buf.WriteString({{$name}}_name[{{$type}}(x)]) buf.WriteString(`"`) return buf.Bytes(), nil } -func (x *{{$enumName}}) UnmarshalJSON(b []byte) error { +func (x *{{$name}}) UnmarshalJSON(b []byte) error { var j string err := json.Unmarshal(b, &j) if err != nil { return err } - *x = {{$enumName}}({{$enumName}}_value[j]) + *x = {{$name}}({{$name}}_value[j]) return nil } diff --git a/main.go.tmpl b/main.go.tmpl index b9985c1..78ac9f0 100644 --- a/main.go.tmpl +++ b/main.go.tmpl @@ -26,12 +26,12 @@ {{- exit 1 -}} {{- end -}} -{{- if not (minVersion .WebrpcGenVersion "v0.8.0") -}} +{{- if not (minVersion .WebrpcGenVersion "v0.9.0") -}} {{- stderrPrintf "%s generator error: unsupported webrpc-gen version %s, please update\n" .WebrpcTarget .WebrpcGenVersion -}} {{- exit 1 -}} {{- end -}} -{{- /* Map webrpc data types to Go. */ -}} +{{- /* Map webrpc core types to Go. */ -}} {{- $typeMap := dict }} {{- set $typeMap "null" "struct{}" -}} {{- set $typeMap "any" "interface{}" -}} @@ -85,7 +85,7 @@ func WebRPCSchemaHash() string { return "{{.SchemaHash}}" } -{{ template "types" dict "Services" .Services "Messages" .Messages "TypeMap" $typeMap }} +{{ template "types" dict "Services" .Services "Types" .Types "TypeMap" $typeMap }} {{- if $opts.server}} {{ template "server" dict "Services" .Services "TypeMap" $typeMap }} diff --git a/struct.go.tmpl b/struct.go.tmpl index cf982f4..7352e5c 100644 --- a/struct.go.tmpl +++ b/struct.go.tmpl @@ -1,9 +1,11 @@ {{- define "struct" -}} -{{- $struct := .Struct -}} + +{{- $name := .Name -}} +{{- $fields := .Fields -}} {{- $typeMap := .TypeMap -}} -type {{$struct.Name}} struct { -{{- range $_, $field := $struct.Fields -}} +type {{$name}} struct { +{{- range $_, $field := $fields -}} {{- $fieldName := $field.Name | firstLetterToUpper -}} {{- $customType := "" -}} {{- $jsonTag := printf "json:%q" $field.Name }} diff --git a/type.go.tmpl b/type.go.tmpl index 9c3cb80..0549055 100644 --- a/type.go.tmpl +++ b/type.go.tmpl @@ -12,13 +12,13 @@ []{{template "type" dict "Type" (listElemType $type) "TypeMap" $typeMap}} -{{- else if isStructType $type -}} +{{- else if isCoreType $type -}} - *{{$type}} + {{if $optional}}*{{end}}{{ get $typeMap $type }} {{- else -}} - {{if $optional}}*{{end}}{{ get $typeMap $type }} + *{{$type}} {{- end -}} {{- end -}} \ No newline at end of file diff --git a/types.go.tmpl b/types.go.tmpl index e45162f..2a64718 100644 --- a/types.go.tmpl +++ b/types.go.tmpl @@ -1,18 +1,18 @@ {{define "types"}} {{- $typeMap := .TypeMap -}} -{{- if .Messages -}} +{{- if .Types -}} // // Types // -{{ range .Messages -}} +{{ range $_, $type := .Types -}} -{{- if isEnumType .Type }} -{{template "enum" .}} +{{- if eq $type.Kind "enum" }} +{{template "enum" dict "Name" $type.Name "Type" $type.Type "Fields" $type.Fields }} {{ end -}} -{{- if isStructType .Type }} -{{template "struct" dict "Struct" . "TypeMap" $typeMap}} +{{- if eq $type.Kind "struct" }} +{{template "struct" dict "Name" $type.Name "Fields" $type.Fields "TypeMap" $typeMap}} {{ end -}} {{- end -}}