Skip to content

Commit ecf16d3

Browse files
authored
Implement faster JSON (un)marshaling via jsoniter pkg (#24)
webrpc-gen -schema=proto.ridl -target=golang -client -json=jsoniter -out=client.gen.go Fixes webrpc/webrpc#20
1 parent 856d281 commit ecf16d3

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Change any of the following values by passing `-option="Value"` CLI flag to `web
2727
| `-pkg=<name>` | package name | `"proto"` | |
2828
| `-client` | generate client code | unset (`false`) | |
2929
| `-server` | generate server code | unset (`false`) | |
30+
| `-json=jsoniter` | use alternative json encoding package | unset (`"stdlib"`) | v0.12.0 |
3031
| `-importTypesFrom=<pkg>` | do not generate types; import from a pkg | unset (`""`) | v0.12.0 |
3132
| `-legacyErrors=true` | enable legacy errors (v0.10.0 or older) | unset (`false`) | v0.11.0 |
3233

imports.go.tmpl

+25-4
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
{{- $stdlibImports := dict -}}
77
{{- set $stdlibImports "context" "" -}}
88
{{- set $stdlibImports "errors" "" -}}
9-
{{- set $stdlibImports "encoding/json" "" -}}
109
{{- set $stdlibImports "fmt" "" -}}
1110
{{- set $stdlibImports "io/ioutil" "" -}}
1211
{{- set $stdlibImports "net/http" "" -}}
1312

13+
{{- if eq $opts.json "stdlib" -}}
14+
{{- set $stdlibImports "encoding/json" "" -}}
15+
{{- end -}}
16+
1417
{{- if $opts.client}}
1518
{{- set $stdlibImports "bytes" "" -}}
1619
{{- set $stdlibImports "io" "" -}}
@@ -33,17 +36,29 @@
3336
{{- end -}}
3437
{{- end -}}
3538

39+
import (
40+
3641
{{- /* Print stdlib imports. */ -}}
3742
{{- range $import, $rename := $stdlibImports }}
3843
{{if ne $rename ""}}{{$rename}} {{end}}"{{$import}}"
3944
{{- end -}}
4045

46+
{{- /* Print custom type imports. */ -}}
47+
{{ $imports := dict }}
48+
49+
{{- if eq $opts.json "stdlib" -}}
50+
{{- /* Already imported in the stdlib section. */ -}}
51+
{{- else if eq $opts.json "jsoniter" -}}
52+
{{- set $imports "github.com/json-iterator/go" "jsoniter" -}}
53+
{{- else -}}
54+
{{- stderrPrintf "unsupported -json=%s" $opts.json -}}
55+
{{- exit 1 -}}
56+
{{- end -}}
57+
4158
{{ if ne $opts.importTypesFrom "" }}
4259

4360
"{{ $opts.importTypesFrom }}"
4461
{{- else }}
45-
{{- /* Print custom type imports. */ -}}
46-
{{ $imports := dict }}
4762
{{ range $_, $type := $types -}}
4863
{{- range $_, $field := $type.Fields -}}
4964
{{- range $meta := $field.Meta -}}
@@ -56,6 +71,12 @@
5671
{{- range $import, $rename := $imports }}
5772
{{if ne $rename ""}}{{$rename}} {{end}}"{{$import}}"
5873
{{- end -}}
59-
{{- end }}
74+
{{ end }}
75+
)
76+
77+
{{- if eq $opts.json "jsoniter" }}
78+
79+
var json = jsoniter.ConfigCompatibleWithStandardLibrary
80+
{{ end -}}
6081

6182
{{- end -}}

main.go.tmpl

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
{{- set $opts "pkg" (default .Opts.pkg "proto") -}}
66
{{- set $opts "client" (ternary (in .Opts.client "" "true") true false) -}}
77
{{- set $opts "server" (ternary (in .Opts.server "" "true") true false) -}}
8+
{{- set $opts "json" (default .Opts.json "stdlib") -}}
89
{{- set $opts "importTypesFrom" (default .Opts.importTypesFrom "" ) -}}
910
{{- set $opts "legacyErrors" (ternary (in .Opts.legacyErrors "true") true false) -}}
1011

@@ -66,9 +67,7 @@
6667
// {{.WebrpcGenCommand}}
6768
package {{get $opts "pkg"}}
6869

69-
import (
70-
{{- template "imports" dict "Types" .Types "Opts" $opts }}
71-
)
70+
{{template "imports" dict "Types" .Types "Opts" $opts }}
7271

7372
// WebRPC description and code-gen version
7473
func WebRPCVersion() string {

0 commit comments

Comments
 (0)