Skip to content

Commit

Permalink
Implement -types=true|false option (true by default) (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
VojtechVitek authored Aug 5, 2023
1 parent 898a236 commit dc4839d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 25 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ As you can see, the `-target` supports default `golang`, any git URI, or a local
### Set custom template variables
Change any of the following values by passing `-option="Value"` CLI flag to `webrpc-gen`.

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

Example:
```
Expand Down
3 changes: 3 additions & 0 deletions enum.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
{{- $name := .Name -}}
{{- $type := .Type -}}
{{- $fields := .Fields -}}
{{- $opts := .Opts -}}

{{- if $opts.types -}}
type {{$name}} {{$type}}
{{- end }}

const (
{{- range $fields}}
Expand Down
30 changes: 19 additions & 11 deletions imports.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,32 @@
{{- set $stdlibImports "encoding/json" "" -}}
{{- end -}}

{{- if $opts.client}}
{{- if or $opts.client (not $opts.types) }}
{{- set $stdlibImports "bytes" "" -}}
{{- end -}}

{{- if $opts.client }}
{{- set $stdlibImports "io" "" -}}
{{- set $stdlibImports "net/url" "" -}}
{{- end -}}
{{- if $opts.server}}

{{- if $opts.server }}
{{- set $stdlibImports "strings" "" -}}
{{- end -}}

{{- /* Import "time" if there's at least one timestamp. */ -}}
{{ if $opts.types }}
{{ if eq $opts.importTypesFrom "" }}
{{- range $_, $type := $types -}}
{{- range $_, $field := $type.Fields -}}
{{- if $field.Type -}}
{{- if eq $field.Type.Expr "timestamp" -}}
{{- set $stdlibImports "time" "" -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- range $_, $type := $types -}}
{{- range $_, $field := $type.Fields -}}
{{- if $field.Type -}}
{{- if eq $field.Type.Expr "timestamp" -}}
{{- set $stdlibImports "time" "" -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}

import (
Expand Down Expand Up @@ -68,9 +74,11 @@ import (
{{- end -}}
{{- end -}}
{{- end }}
{{- if $opts.types }}
{{ range $import, $rename := $imports }}
{{if ne $rename ""}}{{$rename}} {{end}}"{{$import}}"
{{- end }}
{{- end }}
)

{{- if eq $opts.json "jsoniter" }}
Expand Down
7 changes: 4 additions & 3 deletions main.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
{{- set $opts "pkg" (default .Opts.pkg "proto") -}}
{{- set $opts "client" (ternary (in .Opts.client "" "true") true false) -}}
{{- set $opts "server" (ternary (in .Opts.server "" "true") true false) -}}
{{- set $opts "types" (ternary (eq .Opts.types "false") false true) -}}
{{- set $opts "json" (default .Opts.json "stdlib") -}}
{{- set $opts "importTypesFrom" (default .Opts.importTypesFrom "" ) -}}
{{- set $opts "legacyErrors" (ternary (in .Opts.legacyErrors "true") true false) -}}
{{- set $opts "legacyErrors" (ternary (in .Opts.legacyErrors "" "true") true false) -}}

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

{{- if eq $opts.importTypesFrom "" }}
{{ template "types" dict "Services" .Services "Types" .Types "TypeMap" $typeMap "TypePrefix" $typePrefix}}
{{ template "types" dict "Services" .Services "Types" .Types "TypeMap" $typeMap "TypePrefix" $typePrefix "Opts" $opts }}
{{ end -}}

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

{{ template "helpers" . }}

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

{{- if $opts.legacyErrors }}
{{ template "legacyErrors" . }}
Expand Down
7 changes: 4 additions & 3 deletions types.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{{- $typePrefix := .TypePrefix -}}
{{- $types := .Types -}}
{{- $services := .Services -}}
{{- $opts := .Opts -}}

{{- if $types -}}
//
Expand All @@ -11,17 +12,17 @@
{{ range $_, $type := $types -}}

{{- if eq $type.Kind "enum" }}
{{template "enum" dict "Name" $type.Name "Type" $type.Type "TypePrefix" $typePrefix "Fields" $type.Fields}}
{{template "enum" dict "Name" $type.Name "Type" $type.Type "TypePrefix" $typePrefix "Fields" $type.Fields "Opts" $opts}}
{{ end -}}

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

{{- end -}}
{{- end -}}

{{- if $services -}}
{{- if and $services $opts.types -}}
{{ range $_, $service := $services}}
type {{$service.Name}} interface {
{{- range $_, $method := $service.Methods}}
Expand Down

0 comments on commit dc4839d

Please sign in to comment.