From 09bae6028eca8772ae5e20b5df782a29451ed0db Mon Sep 17 00:00:00 2001 From: Vojtech Vitek Date: Wed, 3 Apr 2024 14:51:41 +0200 Subject: [PATCH] Fix imports & formatting --- _examples/golang-basics/example.gen.go | 4 ++-- _examples/golang-imports/api.gen.go | 11 ++++------- imports.go.tmpl | 18 ++++++++++++------ types.go.tmpl | 16 ++++++++-------- 4 files changed, 26 insertions(+), 23 deletions(-) diff --git a/_examples/golang-basics/example.gen.go b/_examples/golang-basics/example.gen.go index 7fb54e5..8b38611 100644 --- a/_examples/golang-basics/example.gen.go +++ b/_examples/golang-basics/example.gen.go @@ -1,6 +1,6 @@ // example v0.0.1 8713979f949e7797d4f7506750b6e18f3b9c7c1b // -- -// Code generated by webrpc-gen@v0.15.2-dev with ../../../gen-golang generator. DO NOT EDIT. +// Code generated by webrpc-gen@v0.15.5 with ../../../gen-golang generator. DO NOT EDIT. // // webrpc-gen -schema=example.ridl -target=../../../gen-golang -pkg=main -server -client -legacyErrors -fixEmptyArrays -out=./example.gen.go package main @@ -37,7 +37,7 @@ func WebRPCSchemaHash() string { } // -// Types +// Common types // type Kind uint32 diff --git a/_examples/golang-imports/api.gen.go b/_examples/golang-imports/api.gen.go index 81ce551..562a613 100644 --- a/_examples/golang-imports/api.gen.go +++ b/_examples/golang-imports/api.gen.go @@ -1,6 +1,6 @@ // example-api-service v1.0.0 eb9a5d4082a36a8cb84eaa4b3e1091fc4f1f6f3d // -- -// Code generated by webrpc-gen@v0.15.2-dev with ../../../gen-golang generator. DO NOT EDIT. +// Code generated by webrpc-gen@v0.15.5 with ../../../gen-golang generator. DO NOT EDIT. // // webrpc-gen -schema=./proto/api.ridl -target=../../../gen-golang -out=./api.gen.go -pkg=main -server -client -legacyErrors=true -fmt=false package main @@ -15,6 +15,7 @@ import ( "net/http" "net/url" "strings" + ) // WebRPC description and code-gen version @@ -32,9 +33,8 @@ func WebRPCSchemaHash() string { return "eb9a5d4082a36a8cb84eaa4b3e1091fc4f1f6f3d" } - // -// Types +// Common types // type User struct { @@ -83,7 +83,7 @@ func (x *Location) Is(values ...Location) bool { } return false } - + var WebRPCServices = map[string][]string{ "ExampleAPI": { "Ping", @@ -102,9 +102,6 @@ type ExampleAPI interface { GetUsers(ctx context.Context) ([]*User, Location, error) } - - - // // Client types // diff --git a/imports.go.tmpl b/imports.go.tmpl index 98e469f..92d61e2 100644 --- a/imports.go.tmpl +++ b/imports.go.tmpl @@ -80,22 +80,28 @@ import ( {{- set $imports $opts.importTypesFrom "" -}} {{- else -}} {{- range $_, $type := $types -}} - {{- range $_, $field := $type.Fields -}} - {{- range $meta := $field.Meta -}} + {{- /* TODO: We might need to loop through method args too. */ -}} + {{- range $meta := $type.Meta -}} {{- if exists $meta "go.type.import" -}} {{- set $imports (get $meta "go.type.import") "" -}} {{- end -}} {{- end -}} - {{- end -}} + {{- if $opts.types }} + {{- range $_, $field := $type.Fields -}} + {{- range $meta := $field.Meta -}} + {{- if exists $meta "go.type.import" -}} + {{- set $imports (get $meta "go.type.import") "" -}} + {{- end -}} + {{- end -}} + {{- end -}} + {{- end }} {{- end -}} {{- end }} -{{- if $opts.types }} {{ range $import, $rename := $imports }} {{- if not (exists $stdlibImports $import)}} {{if ne $rename ""}}{{$rename}} {{end}}"{{$import}}" - {{end}} + {{- end }} {{- end }} -{{- end -}} ) {{- if eq $opts.json "jsoniter" }} diff --git a/types.go.tmpl b/types.go.tmpl index 4e4a3ca..e269108 100644 --- a/types.go.tmpl +++ b/types.go.tmpl @@ -5,18 +5,17 @@ {{- $services := .Services -}} {{- $opts := .Opts -}} -{{- if $types }} // -// Types +// Common types // {{ range $_, $type := $types -}} {{- if eq $type.Kind "enum" }} {{- if eq $type.Type.Expr "string" }} {{template "enum_string" dict "Name" $type.Name "Type" $type.Type "TypePrefix" $typePrefix "Fields" $type.Fields "Opts" $opts}} - {{ else }} + {{- else }} {{template "enum" dict "Name" $type.Name "Type" $type.Type "TypePrefix" $typePrefix "Fields" $type.Fields "Opts" $opts}} - {{ end -}} + {{- end -}} {{ end -}} {{- if and (eq $type.Kind "struct") $opts.types }} @@ -28,8 +27,7 @@ {{template "struct" dict "Name" $type.Name "TypeMap" $typeMap "TypePrefix" $typePrefix "Fields" $type.Fields}} {{ end -}} -{{ end }} -{{ end -}} +{{- end }} var WebRPCServices = map[string][]string{ {{- range $_, $service := $services}} @@ -46,6 +44,7 @@ var WebRPCServices = map[string][]string{ // {{ range $_, $service := $services -}} +{{ if $opts.types -}} type {{$service.Name}} interface { {{- range $_, $method := $service.Methods}} {{ if eq $method.StreamOutput true -}} @@ -56,9 +55,10 @@ type {{$service.Name}} interface { {{- end}} } +{{- end }} -{{- range $_, $method := $service.Methods}} -{{ if eq $method.StreamOutput true -}} +{{- range $_, $method := $service.Methods -}} +{{ if eq $method.StreamOutput true }} type {{$method.Name}}StreamWriter interface { Write({{range $i, $output := $method.Outputs}}{{if gt $i 0}}, {{end}}{{$output.Name}} {{template "field" dict "Name" $output.Name "Type" $output.Type "Optional" $output.Optional "TypeMap" $typeMap "TypePrefix" $typePrefix "TypeMeta" $output.Meta}}{{end}}) error }