Skip to content

Commit dc4839d

Browse files
authored
Implement -types=true|false option (true by default) (#28)
1 parent 898a236 commit dc4839d

File tree

5 files changed

+38
-25
lines changed

5 files changed

+38
-25
lines changed

README.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ As you can see, the `-target` supports default `golang`, any git URI, or a local
2222
### Set custom template variables
2323
Change any of the following values by passing `-option="Value"` CLI flag to `webrpc-gen`.
2424

25-
| webrpc-gen -option | Description | Default value | Added in |
26-
|--------------------------|-------------------------------------------|----------------------------|----------|
27-
| `-pkg=<name>` | package name | `"proto"` | |
28-
| `-client` | generate client code | unset (`false`) | |
29-
| `-server` | generate server code | unset (`false`) | |
30-
| `-json=jsoniter` | use alternative json encoding package | unset (`"stdlib"`) | v0.12.0 |
31-
| `-importTypesFrom=<pkg>` | do not generate types; import from a pkg | unset (`""`) | v0.12.0 |
32-
| `-legacyErrors=true` | enable legacy errors (v0.10.0 or older) | unset (`false`) | v0.11.0 |
25+
| webrpc-gen -option | Description | Default value | Added in |
26+
|----------------------|-----------------------------------------|--------------|----------|
27+
| `-pkg=<name>` | package name | `"proto"` | v0.5.0 |
28+
| `-client` | generate client code | `false` | v0.5.0 |
29+
| `-server` | generate server code | `false` | v0.5.0 |
30+
| `-types=false` | don't generate types | `true` | v0.13.0 |
31+
| `-json=jsoniter` | use alternative json encoding package | `"stdlib"` | v0.12.0 |
32+
| `-legacyErrors=true` | enable legacy errors (v0.10.0 or older) | `false` | v0.11.0 |
3333

3434
Example:
3535
```

enum.go.tmpl

+3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
{{- $name := .Name -}}
44
{{- $type := .Type -}}
55
{{- $fields := .Fields -}}
6+
{{- $opts := .Opts -}}
67

8+
{{- if $opts.types -}}
79
type {{$name}} {{$type}}
10+
{{- end }}
811

912
const (
1013
{{- range $fields}}

imports.go.tmpl

+19-11
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,32 @@
1414
{{- set $stdlibImports "encoding/json" "" -}}
1515
{{- end -}}
1616

17-
{{- if $opts.client}}
17+
{{- if or $opts.client (not $opts.types) }}
1818
{{- set $stdlibImports "bytes" "" -}}
19+
{{- end -}}
20+
21+
{{- if $opts.client }}
1922
{{- set $stdlibImports "io" "" -}}
2023
{{- set $stdlibImports "net/url" "" -}}
2124
{{- end -}}
22-
{{- if $opts.server}}
25+
26+
{{- if $opts.server }}
2327
{{- set $stdlibImports "strings" "" -}}
2428
{{- end -}}
2529

2630
{{- /* Import "time" if there's at least one timestamp. */ -}}
31+
{{ if $opts.types }}
2732
{{ if eq $opts.importTypesFrom "" }}
28-
{{- range $_, $type := $types -}}
29-
{{- range $_, $field := $type.Fields -}}
30-
{{- if $field.Type -}}
31-
{{- if eq $field.Type.Expr "timestamp" -}}
32-
{{- set $stdlibImports "time" "" -}}
33-
{{- end -}}
34-
{{- end -}}
35-
{{- end -}}
36-
{{- end -}}
33+
{{- range $_, $type := $types -}}
34+
{{- range $_, $field := $type.Fields -}}
35+
{{- if $field.Type -}}
36+
{{- if eq $field.Type.Expr "timestamp" -}}
37+
{{- set $stdlibImports "time" "" -}}
38+
{{- end -}}
39+
{{- end -}}
40+
{{- end -}}
41+
{{- end -}}
42+
{{- end -}}
3743
{{- end -}}
3844

3945
import (
@@ -68,9 +74,11 @@ import (
6874
{{- end -}}
6975
{{- end -}}
7076
{{- end }}
77+
{{- if $opts.types }}
7178
{{ range $import, $rename := $imports }}
7279
{{if ne $rename ""}}{{$rename}} {{end}}"{{$import}}"
7380
{{- end }}
81+
{{- end }}
7482
)
7583

7684
{{- if eq $opts.json "jsoniter" }}

main.go.tmpl

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
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 "types" (ternary (eq .Opts.types "false") false true) -}}
89
{{- set $opts "json" (default .Opts.json "stdlib") -}}
910
{{- set $opts "importTypesFrom" (default .Opts.importTypesFrom "" ) -}}
10-
{{- set $opts "legacyErrors" (ternary (in .Opts.legacyErrors "true") true false) -}}
11+
{{- set $opts "legacyErrors" (ternary (in .Opts.legacyErrors "" "true") true false) -}}
1112

1213
{{- $typePrefix := (last (split "/" $opts.importTypesFrom)) -}}
1314
{{- if ne $typePrefix "" -}}
@@ -87,7 +88,7 @@ func WebRPCSchemaHash() string {
8788
{{- printf "\n" -}}
8889

8990
{{- if eq $opts.importTypesFrom "" }}
90-
{{ template "types" dict "Services" .Services "Types" .Types "TypeMap" $typeMap "TypePrefix" $typePrefix}}
91+
{{ template "types" dict "Services" .Services "Types" .Types "TypeMap" $typeMap "TypePrefix" $typePrefix "Opts" $opts }}
9192
{{ end -}}
9293

9394
{{- if $opts.server}}
@@ -100,7 +101,7 @@ func WebRPCSchemaHash() string {
100101

101102
{{ template "helpers" . }}
102103

103-
{{- template "errors" dict "WebrpcErrors" .WebrpcErrors "SchemaErrors" .Errors "Opts" $opts "TypePrefix" $typePrefix}}
104+
{{- template "errors" dict "WebrpcErrors" .WebrpcErrors "SchemaErrors" .Errors "Opts" $opts "TypePrefix" $typePrefix }}
104105

105106
{{- if $opts.legacyErrors }}
106107
{{ template "legacyErrors" . }}

types.go.tmpl

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
{{- $typePrefix := .TypePrefix -}}
44
{{- $types := .Types -}}
55
{{- $services := .Services -}}
6+
{{- $opts := .Opts -}}
67

78
{{- if $types -}}
89
//
@@ -11,17 +12,17 @@
1112
{{ range $_, $type := $types -}}
1213

1314
{{- if eq $type.Kind "enum" }}
14-
{{template "enum" dict "Name" $type.Name "Type" $type.Type "TypePrefix" $typePrefix "Fields" $type.Fields}}
15+
{{template "enum" dict "Name" $type.Name "Type" $type.Type "TypePrefix" $typePrefix "Fields" $type.Fields "Opts" $opts}}
1516
{{ end -}}
1617

17-
{{- if eq $type.Kind "struct" }}
18+
{{- if and (eq $type.Kind "struct") $opts.types }}
1819
{{template "struct" dict "Name" $type.Name "TypeMap" $typeMap "TypePrefix" $typePrefix "Fields" $type.Fields}}
1920
{{ end -}}
2021

2122
{{- end -}}
2223
{{- end -}}
2324

24-
{{- if $services -}}
25+
{{- if and $services $opts.types -}}
2526
{{ range $_, $service := $services}}
2627
type {{$service.Name}} interface {
2728
{{- range $_, $method := $service.Methods}}

0 commit comments

Comments
 (0)