Skip to content

Commit bba54d6

Browse files
authored
Update templates to [email protected] schema (#2)
* README: Update options * Update to [email protected] * Fix types * Improve generated code * Switch webrpc to webrpc/webrpc#171 * Regenerate tests
1 parent 701e5e7 commit bba54d6

File tree

8 files changed

+42
-41
lines changed

8 files changed

+42
-41
lines changed

.github/workflows/ci.yml

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ jobs:
2020
uses: actions/setup-go@v3
2121
with:
2222
go-version: 1.19
23-
- name: Install webrpc-gen
24-
#run: go install github.com/webrpc/webrpc/cmd/webrpc-gen@${{ matrix.webrpc-gen }}
23+
#- name: Install webrpc-gen
24+
# run: go install github.com/webrpc/webrpc/cmd/webrpc-gen@${{ matrix.webrpc-gen }}
25+
- name: Install webrpc-gen (development)
2526
run: |
26-
git clone --single-branch https://github.com/golang-cz/webrpc.git --branch templates_v0.7.0
27+
git clone --single-branch https://github.com/golang-cz/webrpc.git --branch message_to_struct
2728
cd webrpc
2829
make install
2930
- name: Regenerate examples

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ webrpc-gen -schema=example.ridl -target=./local-templates-on-disk -server -clien
3636
```
3737

3838
### Set custom template variables
39-
Change any of the following values by passing `-Option="Value"` CLI flag to `webrpc-gen`.
39+
Change any of the following values by passing `-option="Value"` CLI flag to `webrpc-gen`.
4040

41-
| CLI option flag | Description | Default value |
41+
| webrpc-gen -option | Description | Default value |
4242
|----------------------|----------------------------|----------------------------|
4343
| `-client` | generate client code | unset (`false`) |
4444
| `-server` | generate server code | unset (`false`) |

_examples/hello-webrpc/hello-api.ridl

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ enum Kind: uint32
77
- USER = 1
88
- ADMIN = 2
99

10-
message Empty
10+
struct Empty
1111

12-
message User
12+
struct User
1313
- ID: uint64
1414
+ json = id
1515
+ go.tag.db = id

_examples/hello-webrpc/webapp/client.gen.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// hello-webrpc v1.0.0 d12378d7d88e036c2e5f779db475e7144b638b26
1+
// hello-webrpc v1.0.0 d53c5acbdcf95d64a61f2b4e0792b38854e9353a
22
// --
3-
// Code generated by webrpc-gen@v0.7.0 with custom generator. DO NOT EDIT.
3+
// Code generated by webrpc-gen@v0.9.x-dev with custom generator. DO NOT EDIT.
44
//
55
// webrpc-gen -schema=hello-api.ridl -target=../../ -exports=false -client -out=./webapp/client.gen.js
66

@@ -11,7 +11,7 @@ export const WebRPCVersion = "v1"
1111
export const WebRPCSchemaVersion = "v1.0.0"
1212

1313
// Schema hash generated from your RIDL schema
14-
export const WebRPCSchemaHash = "d12378d7d88e036c2e5f779db475e7144b638b26"
14+
export const WebRPCSchemaHash = "d53c5acbdcf95d64a61f2b4e0792b38854e9353a"
1515

1616
//
1717
// Types

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.7.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 JS. */ -}}
34+
{{- /* Map webrpc core types to JS. */ -}}
3535
{{- $typeMap := dict }}
3636
{{- set $typeMap "null" "null" -}}
3737
{{- set $typeMap "any" "any" -}}
@@ -67,7 +67,7 @@ export const WebRPCSchemaVersion = "{{.SchemaVersion}}"
6767
// Schema hash generated from your RIDL schema
6868
export const WebRPCSchemaHash = "{{.SchemaHash}}"
6969

70-
{{template "types" dict "Services" .Services "Messages" .Messages "Opts" $opts}}
70+
{{template "types" dict "Services" .Services "Types" .Types "Opts" $opts}}
7171
{{- if $opts.client}}
7272
{{template "client" dict "Services" .Services "Opts" $opts}}
7373
{{template "client_helpers" .}}

server.go.tmpl

+15-16
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{{define "server"}}
22

3+
{{- $services := .Services -}}
34
{{- $typeMap := .TypeMap -}}
45

5-
{{- if .Services}}
6+
{{- if $services}}
67
//
78
// Server
89
//
@@ -17,11 +18,9 @@ class WebRPCError extends Error {
1718

1819
import express from 'express'
1920

20-
{{- range .Services}}
21-
{{$name := .Name}}
22-
{{$serviceName := .Name}}
21+
{{- range $_, $service := $services}}
2322

24-
export const create{{$serviceName}}App = (serviceImplementation) => {
23+
export const create{{$service.Name}}App = (serviceImplementation) => {
2524
const app = express();
2625

2726
app.use(express.json())
@@ -36,26 +35,26 @@ import express from 'express'
3635
}
3736

3837
switch(requestPath) {
39-
{{range .Methods}}
38+
{{range $_, $method := $service.Methods}}
4039

41-
case "/rpc/{{$name}}/{{.Name}}": {
40+
case "/rpc/{{$service.Name}}/{{$method.Name}}": {
4241
try {
43-
{{ range .Inputs }}
44-
{{- if not .Optional}}
45-
if (!("{{ .Name }}" in req.body)) {
46-
throw new WebRPCError("Missing Argument `{{ .Name }}`")
42+
{{ range $_, $input := $method.Inputs }}
43+
{{- if not $input.Optional}}
44+
if (!("{{ $input.Name }}" in req.body)) {
45+
throw new WebRPCError("Missing Argument `{{ $input.Name }}`")
4746
}
4847
{{end -}}
4948

50-
if (typeof req.body["{{.Name}}"] !== "{{template "type" dict "Type" .Type "TypeMap" $typeMap}}") {
51-
throw new WebRPCError("Invalid arg: {{ .Name }}, got type " + typeof req.body["{{ .Name }}"] + " expected " + "{{template "type" dict "Type" .Type "TypeMap" $typeMap}}", 400);
49+
if (typeof req.body["{{$input.Name}}"] !== "{{template "type" dict "Type" $input.Type "TypeMap" $typeMap}}") {
50+
throw new WebRPCError("Invalid arg: {{ $input.Name }}, got type " + typeof req.body["{{ $input.Name }}"] + " expected " + "{{template "type" dict "Type" $input.Type "TypeMap" $typeMap}}", 400);
5251
}
5352
{{end}}
5453

55-
const response = await serviceImplementation["{{.Name}}"](req.body);
54+
const response = await serviceImplementation["{{$method.Name}}"](req.body);
5655

57-
{{ range .Outputs}}
58-
if (!("{{ .Name }}" in response)) {
56+
{{ range $_, $output := .Outputs}}
57+
if (!("{{ $output.Name }}" in response)) {
5958
throw new WebRPCError("internal", 500);
6059
}
6160
{{end}}

type.go.tmpl

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@
33
{{- $type := .Type -}}
44
{{- $typeMap := .TypeMap -}}
55

6-
{{- if isMapType .Type -}}
6+
{{- if isMapType $type -}}
77

88
object
99

10-
{{- else if isListType .Type -}}
10+
{{- else if isListType $type -}}
1111

12-
{{template "type" dict "Type" (listElemType .Type) "TypeMap" $typeMap}}[]
12+
{{template "type" dict "Type" (listElemType $type) "TypeMap" $typeMap}}[]
1313

14-
{{- else if isStructType .Type -}}
14+
{{- else if isCoreType $type -}}
1515

16-
{{.Type}}
16+
{{ get $typeMap $type }}
1717

1818
{{- else -}}
1919

20-
{{ get $typeMap .Type }}
20+
{{ $type }}
2121

2222
{{- end -}}
2323
{{- end -}}

types.go.tmpl

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
{{define "types"}}
22

33
{{- $opts := .Opts -}}
4+
{{- $types := .Types -}}
45

56
//
67
// Types
78
//
8-
{{ if .Messages -}}
9-
{{range .Messages -}}
9+
{{ if $types -}}
10+
{{range $_, $type := $types -}}
1011

11-
{{if .Type | isEnumType -}}
12+
{{if isEnumType $type -}}
1213
{{$enumName := .Name}}
1314
{{if $opts.exports}}export {{end}}var {{$enumName}};
1415
(function ({{$enumName}}) {
@@ -18,17 +19,17 @@
1819
})({{$enumName}} || ({{$enumName}} = {}))
1920
{{end -}}
2021

21-
{{- if .Type | isStructType }}
22+
{{- if isStructType $type }}
2223
{{if $opts.exports}}export {{end}}class {{.Name}} {
2324
constructor(_data) {
2425
this._data = {}
2526
if (_data) {
26-
{{range .Fields -}}
27+
{{range $type.Fields -}}
2728
this._data['{{template "fieldName" dict "Field" . }}'] = _data['{{template "fieldName" dict "Field" . }}']
2829
{{end}}
2930
}
3031
}
31-
{{ range .Fields -}}
32+
{{ range $type.Fields -}}
3233
get {{template "fieldName" dict "Field" . }}() {
3334
return this._data['{{template "fieldName" dict "Field" . }}']
3435
}

0 commit comments

Comments
 (0)