Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ jobs:
uses: actions/setup-go@v3
with:
go-version: 1.19
- name: Install webrpc-gen
#run: go install github.com/webrpc/webrpc/cmd/webrpc-gen@${{ matrix.webrpc-gen }}
#- 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
git clone --single-branch https://github.com/golang-cz/webrpc.git --branch message_to_struct
cd webrpc
make install
- name: Regenerate examples
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ webrpc-gen -schema=example.ridl -target=./local-templates-on-disk -server -clien
```

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

| CLI option flag | Description | Default value |
| webrpc-gen -option | Description | Default value |
|----------------------|----------------------------|----------------------------|
| `-client` | generate client code | unset (`false`) |
| `-server` | generate server code | unset (`false`) |
Expand Down
4 changes: 2 additions & 2 deletions _examples/hello-webrpc/hello-api.ridl
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ enum Kind: uint32
- USER = 1
- ADMIN = 2

message Empty
struct Empty

message User
struct User
- ID: uint64
+ json = id
+ go.tag.db = id
Expand Down
6 changes: 3 additions & 3 deletions _examples/hello-webrpc/webapp/client.gen.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// hello-webrpc v1.0.0 d12378d7d88e036c2e5f779db475e7144b638b26
// hello-webrpc v1.0.0 d53c5acbdcf95d64a61f2b4e0792b38854e9353a
// --
// Code generated by webrpc-gen@v0.7.0 with custom generator. DO NOT EDIT.
// Code generated by webrpc-gen@v0.9.x-dev with custom generator. DO NOT EDIT.
//
// webrpc-gen -schema=hello-api.ridl -target=../../ -exports=false -client -out=./webapp/client.gen.js

Expand All @@ -11,7 +11,7 @@ export const WebRPCVersion = "v1"
export const WebRPCSchemaVersion = "v1.0.0"

// Schema hash generated from your RIDL schema
export const WebRPCSchemaHash = "d12378d7d88e036c2e5f779db475e7144b638b26"
export const WebRPCSchemaHash = "d53c5acbdcf95d64a61f2b4e0792b38854e9353a"

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

{{template "types" dict "Services" .Services "Messages" .Messages "Opts" $opts}}
{{template "types" dict "Services" .Services "Types" .Types "Opts" $opts}}
{{- if $opts.client}}
{{template "client" dict "Services" .Services "Opts" $opts}}
{{template "client_helpers" .}}
Expand Down
31 changes: 15 additions & 16 deletions server.go.tmpl
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{{define "server"}}

{{- $services := .Services -}}
{{- $typeMap := .TypeMap -}}

{{- if .Services}}
{{- if $services}}
//
// Server
//
Expand All @@ -17,11 +18,9 @@ class WebRPCError extends Error {

import express from 'express'

{{- range .Services}}
{{$name := .Name}}
{{$serviceName := .Name}}
{{- range $_, $service := $services}}

export const create{{$serviceName}}App = (serviceImplementation) => {
export const create{{$service.Name}}App = (serviceImplementation) => {
const app = express();

app.use(express.json())
Expand All @@ -36,26 +35,26 @@ import express from 'express'
}

switch(requestPath) {
{{range .Methods}}
{{range $_, $method := $service.Methods}}

case "/rpc/{{$name}}/{{.Name}}": {
case "/rpc/{{$service.Name}}/{{$method.Name}}": {
try {
{{ range .Inputs }}
{{- if not .Optional}}
if (!("{{ .Name }}" in req.body)) {
throw new WebRPCError("Missing Argument `{{ .Name }}`")
{{ range $_, $input := $method.Inputs }}
{{- if not $input.Optional}}
if (!("{{ $input.Name }}" in req.body)) {
throw new WebRPCError("Missing Argument `{{ $input.Name }}`")
}
{{end -}}

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

const response = await serviceImplementation["{{.Name}}"](req.body);
const response = await serviceImplementation["{{$method.Name}}"](req.body);

{{ range .Outputs}}
if (!("{{ .Name }}" in response)) {
{{ range $_, $output := .Outputs}}
if (!("{{ $output.Name }}" in response)) {
throw new WebRPCError("internal", 500);
}
{{end}}
Expand Down
12 changes: 6 additions & 6 deletions type.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
{{- $type := .Type -}}
{{- $typeMap := .TypeMap -}}

{{- if isMapType .Type -}}
{{- if isMapType $type -}}

object

{{- else if isListType .Type -}}
{{- else if isListType $type -}}

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

{{- else if isStructType .Type -}}
{{- else if isCoreType $type -}}

{{.Type}}
{{ get $typeMap $type }}

{{- else -}}

{{ get $typeMap .Type }}
{{ $type }}

{{- end -}}
{{- end -}}
13 changes: 7 additions & 6 deletions types.go.tmpl
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
{{define "types"}}

{{- $opts := .Opts -}}
{{- $types := .Types -}}

//
// Types
//
{{ if .Messages -}}
{{range .Messages -}}
{{ if $types -}}
{{range $_, $type := $types -}}

{{if .Type | isEnumType -}}
{{if isEnumType $type -}}
{{$enumName := .Name}}
{{if $opts.exports}}export {{end}}var {{$enumName}};
(function ({{$enumName}}) {
Expand All @@ -18,17 +19,17 @@
})({{$enumName}} || ({{$enumName}} = {}))
{{end -}}

{{- if .Type | isStructType }}
{{- if isStructType $type }}
{{if $opts.exports}}export {{end}}class {{.Name}} {
constructor(_data) {
this._data = {}
if (_data) {
{{range .Fields -}}
{{range $type.Fields -}}
this._data['{{template "fieldName" dict "Field" . }}'] = _data['{{template "fieldName" dict "Field" . }}']
{{end}}
}
}
{{ range .Fields -}}
{{ range $type.Fields -}}
get {{template "fieldName" dict "Field" . }}() {
return this._data['{{template "fieldName" dict "Field" . }}']
}
Expand Down