Skip to content

Commit

Permalink
Update templates to [email protected] schema (#13)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
VojtechVitek authored Dec 28, 2022
1 parent 9e0555e commit 41cd528
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 210 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion _examples/golang-basics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
6 changes: 3 additions & 3 deletions _examples/golang-basics/example.gen.go

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

10 changes: 5 additions & 5 deletions _examples/golang-basics/example.ridl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ enum Kind: uint32
- USER
- ADMIN

message User
struct User
- id: uint64
+ json = id
+ go.field.name = ID
Expand All @@ -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
Expand All @@ -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<string,any>
- metaNestedExample: map<string,map<string,uint32>>
- namesList: []string
Expand Down
159 changes: 0 additions & 159 deletions _examples/golang-basics/example.webrpc.json

This file was deleted.

6 changes: 3 additions & 3 deletions _examples/golang-imports/api.gen.go

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

2 changes: 1 addition & 1 deletion _examples/golang-imports/proto/types.ridl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
webrpc = v1

message User
struct User
- username: string
- age: uint32
2 changes: 1 addition & 1 deletion _examples/golang-imports/proto/util.ridl
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ enum Location: uint32
- TORONTO
- NEW_YORK

message Setting
struct Setting
- config: map<string,string>
31 changes: 16 additions & 15 deletions enum.go.tmpl
Original file line number Diff line number Diff line change
@@ -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
}

Expand Down
6 changes: 3 additions & 3 deletions main.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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{}" -}}
Expand Down Expand Up @@ -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 }}
Expand Down
8 changes: 5 additions & 3 deletions struct.go.tmpl
Original file line number Diff line number Diff line change
@@ -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 }}
Expand Down
6 changes: 3 additions & 3 deletions type.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 -}}
Loading

0 comments on commit 41cd528

Please sign in to comment.