diff --git a/README.md b/README.md index 047a7e2..56a85cf 100644 --- a/README.md +++ b/README.md @@ -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=` | 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=` | 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=` | 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: ``` diff --git a/enum.go.tmpl b/enum.go.tmpl index ebd841c..8e24f1e 100644 --- a/enum.go.tmpl +++ b/enum.go.tmpl @@ -3,8 +3,11 @@ {{- $name := .Name -}} {{- $type := .Type -}} {{- $fields := .Fields -}} +{{- $opts := .Opts -}} +{{- if $opts.types -}} type {{$name}} {{$type}} +{{- end }} const ( {{- range $fields}} diff --git a/imports.go.tmpl b/imports.go.tmpl index aac967c..7ad89cb 100644 --- a/imports.go.tmpl +++ b/imports.go.tmpl @@ -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 ( @@ -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" }} diff --git a/main.go.tmpl b/main.go.tmpl index 0e6cf43..43ab00f 100644 --- a/main.go.tmpl +++ b/main.go.tmpl @@ -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 "" -}} @@ -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}} @@ -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" . }} diff --git a/types.go.tmpl b/types.go.tmpl index 05df40c..53c5a7e 100644 --- a/types.go.tmpl +++ b/types.go.tmpl @@ -3,6 +3,7 @@ {{- $typePrefix := .TypePrefix -}} {{- $types := .Types -}} {{- $services := .Services -}} +{{- $opts := .Opts -}} {{- if $types -}} // @@ -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}}