Skip to content

Commit 41cd528

Browse files
authored
Update templates to [email protected] schema (#13)
* Require [email protected] * Update templates to [email protected] JSON schema * Improve the templates * Test against webrpc/webrpc#171 * Migrate examples to [email protected] find . -name '*.ridl' -exec sed -i -e 's/^message /struct /g' {} \; * Fix template codegen * Regenerate examples * Examples: Use local templates
1 parent 9e0555e commit 41cd528

File tree

13 files changed

+54
-210
lines changed

13 files changed

+54
-210
lines changed

.github/workflows/ci.yml

+7-7
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ jobs:
2121
uses: actions/setup-go@v3
2222
with:
2323
go-version: ${{ matrix.go-version }}
24-
- name: Install webrpc-gen
25-
run: go install github.com/webrpc/webrpc/cmd/webrpc-gen@${{ matrix.webrpc-gen }}
26-
# - name: Install webrpc-gen (development)
27-
# run: |
28-
# git clone --single-branch https://github.com/golang-cz/webrpc.git --branch templates_v0.7.0
29-
# cd webrpc
30-
# make install
24+
# - name: Install webrpc-gen
25+
# run: go install github.com/webrpc/webrpc/cmd/webrpc-gen@${{ matrix.webrpc-gen }}
26+
- name: Install webrpc-gen (development)
27+
run: |
28+
git clone --single-branch https://github.com/golang-cz/webrpc.git --branch message_to_struct
29+
cd webrpc
30+
make install
3131
- name: Regenerate examples
3232
run: cd _examples && make generate
3333
- name: Git diff of regenerated files

_examples/golang-basics/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ you can also write your schema in JSON format like so, [./example.webrpc.json](.
1313
2. Design your schema file and think about the methods calls clients will need to make
1414
to your service
1515
3. Write the "services" section of the schema file
16-
4. From the inputs and outputs for the function definitions, start writing the "messages"
16+
4. From the inputs and outputs for the function definitions, start writing the "structs"
1717
section of the data types needed in your program.
1818
5. Run the code generator to build the server and client:
1919
* `webrpc-gen -schema=example.ridl -target=golang -pkg=main -server -client -out=./example.gen.go`

_examples/golang-basics/example.gen.go

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

_examples/golang-basics/example.ridl

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ enum Kind: uint32
88
- USER
99
- ADMIN
1010

11-
message User
11+
struct User
1212
- id: uint64
1313
+ json = id
1414
+ go.field.name = ID
@@ -32,7 +32,7 @@ message User
3232
- updatedAt?: timestamp
3333
+ go.tag.db = updated_at
3434

35-
message Nickname
35+
struct Nickname
3636
- ID: uint64
3737
+ go.tag.db = id
3838
- nickname: string
@@ -42,15 +42,15 @@ message Nickname
4242
- updatedAt?: timestamp
4343
+ go.tag.db = updated_at
4444

45-
message SearchFilter
45+
struct SearchFilter
4646
- q: string
4747

48-
message Version
48+
struct Version
4949
- webrpcVersion: string
5050
- schemaVersion: string
5151
- schemaHash: string
5252

53-
message ComplexType
53+
struct ComplexType
5454
- meta: map<string,any>
5555
- metaNestedExample: map<string,map<string,uint32>>
5656
- namesList: []string

_examples/golang-basics/example.webrpc.json

-159
This file was deleted.

_examples/golang-imports/api.gen.go

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
webrpc = v1
22

3-
message User
3+
struct User
44
- username: string
55
- age: uint32

_examples/golang-imports/proto/util.ridl

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ enum Location: uint32
44
- TORONTO
55
- NEW_YORK
66

7-
message Setting
7+
struct Setting
88
- config: map<string,string>

enum.go.tmpl

+16-15
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,47 @@
11
{{- define "enum" -}}
22

3-
{{- $enumName := .Name -}}
4-
{{- $enumType := .EnumType -}}
3+
{{- $name := .Name -}}
4+
{{- $type := .Type -}}
5+
{{- $fields := .Fields -}}
56

6-
type {{$enumName}} {{$enumType}}
7+
type {{$name}} {{$type}}
78

89
const (
9-
{{- range .Fields}}
10-
{{$enumName}}_{{.Name}} {{$enumName}} = {{.Value}}
10+
{{- range $fields}}
11+
{{$name}}_{{.Name}} {{$name}} = {{.Value}}
1112
{{- end}}
1213
)
1314

14-
var {{$enumName}}_name = map[{{$enumType}}]string{
15-
{{- range .Fields}}
15+
var {{$name}}_name = map[{{$type}}]string{
16+
{{- range $fields}}
1617
{{.Value}}: "{{.Name}}",
1718
{{- end}}
1819
}
1920

20-
var {{$enumName}}_value = map[string]{{$enumType}}{
21-
{{- range .Fields}}
21+
var {{$name}}_value = map[string]{{$type}}{
22+
{{- range $fields}}
2223
"{{.Name}}": {{.Value}},
2324
{{- end}}
2425
}
2526

26-
func (x {{$enumName}}) String() string {
27-
return {{$enumName}}_name[{{$enumType}}(x)]
27+
func (x {{$name}}) String() string {
28+
return {{$name}}_name[{{$type}}(x)]
2829
}
2930

30-
func (x {{$enumName}}) MarshalJSON() ([]byte, error) {
31+
func (x {{$name}}) MarshalJSON() ([]byte, error) {
3132
buf := bytes.NewBufferString(`"`)
32-
buf.WriteString({{$enumName}}_name[{{$enumType}}(x)])
33+
buf.WriteString({{$name}}_name[{{$type}}(x)])
3334
buf.WriteString(`"`)
3435
return buf.Bytes(), nil
3536
}
3637

37-
func (x *{{$enumName}}) UnmarshalJSON(b []byte) error {
38+
func (x *{{$name}}) UnmarshalJSON(b []byte) error {
3839
var j string
3940
err := json.Unmarshal(b, &j)
4041
if err != nil {
4142
return err
4243
}
43-
*x = {{$enumName}}({{$enumName}}_value[j])
44+
*x = {{$name}}({{$name}}_value[j])
4445
return nil
4546
}
4647

main.go.tmpl

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@
2626
{{- exit 1 -}}
2727
{{- end -}}
2828

29-
{{- if not (minVersion .WebrpcGenVersion "v0.8.0") -}}
29+
{{- if not (minVersion .WebrpcGenVersion "v0.9.0") -}}
3030
{{- stderrPrintf "%s generator error: unsupported webrpc-gen version %s, please update\n" .WebrpcTarget .WebrpcGenVersion -}}
3131
{{- exit 1 -}}
3232
{{- end -}}
3333

34-
{{- /* Map webrpc data types to Go. */ -}}
34+
{{- /* Map webrpc core types to Go. */ -}}
3535
{{- $typeMap := dict }}
3636
{{- set $typeMap "null" "struct{}" -}}
3737
{{- set $typeMap "any" "interface{}" -}}
@@ -85,7 +85,7 @@ func WebRPCSchemaHash() string {
8585
return "{{.SchemaHash}}"
8686
}
8787

88-
{{ template "types" dict "Services" .Services "Messages" .Messages "TypeMap" $typeMap }}
88+
{{ template "types" dict "Services" .Services "Types" .Types "TypeMap" $typeMap }}
8989

9090
{{- if $opts.server}}
9191
{{ template "server" dict "Services" .Services "TypeMap" $typeMap }}

struct.go.tmpl

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
{{- define "struct" -}}
2-
{{- $struct := .Struct -}}
2+
3+
{{- $name := .Name -}}
4+
{{- $fields := .Fields -}}
35
{{- $typeMap := .TypeMap -}}
46

5-
type {{$struct.Name}} struct {
6-
{{- range $_, $field := $struct.Fields -}}
7+
type {{$name}} struct {
8+
{{- range $_, $field := $fields -}}
79
{{- $fieldName := $field.Name | firstLetterToUpper -}}
810
{{- $customType := "" -}}
911
{{- $jsonTag := printf "json:%q" $field.Name }}

type.go.tmpl

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212

1313
[]{{template "type" dict "Type" (listElemType $type) "TypeMap" $typeMap}}
1414

15-
{{- else if isStructType $type -}}
15+
{{- else if isCoreType $type -}}
1616

17-
*{{$type}}
17+
{{if $optional}}*{{end}}{{ get $typeMap $type }}
1818

1919
{{- else -}}
2020

21-
{{if $optional}}*{{end}}{{ get $typeMap $type }}
21+
*{{$type}}
2222

2323
{{- end -}}
2424
{{- end -}}

0 commit comments

Comments
 (0)