From ff11ec7ebed90bccf31b78e257d4ab077cd13a3a Mon Sep 17 00:00:00 2001 From: Lukas Jenicek Date: Mon, 30 Sep 2024 15:54:14 +0200 Subject: [PATCH] render map[string]MethodCtx in template --- _examples/golang-basics/example.gen.go | 38 +++++++++++++++++++++----- _examples/golang-imports/api.gen.go | 26 +++++++++++++----- types.go.tmpl | 12 +++++--- 3 files changed, 58 insertions(+), 18 deletions(-) diff --git a/_examples/golang-basics/example.gen.go b/_examples/golang-basics/example.gen.go index 6603744..4480219 100644 --- a/_examples/golang-basics/example.gen.go +++ b/_examples/golang-basics/example.gen.go @@ -161,13 +161,37 @@ type ComplexType struct { } var ( - methodAnnotations = map[string]map[string]string{ - "/rpc/ExampleService/Ping": {}, - "/rpc/ExampleService/Status": {"internal": ""}, - "/rpc/ExampleService/Version": {}, - "/rpc/ExampleService/GetUser": {}, - "/rpc/ExampleService/FindUser": {}, - "/rpc/ExampleService/LogEvent": {}, + methodAnnotations = map[string]MethodCtx{ + "/rpc/ExampleService/Ping": { + Name: "Ping", + Service: "ExampleService", + Annotations: map[string]string{}, + }, + "/rpc/ExampleService/Status": { + Name: "Status", + Service: "ExampleService", + Annotations: map[string]string{"internal": ""}, + }, + "/rpc/ExampleService/Version": { + Name: "Version", + Service: "ExampleService", + Annotations: map[string]string{}, + }, + "/rpc/ExampleService/GetUser": { + Name: "GetUser", + Service: "ExampleService", + Annotations: map[string]string{}, + }, + "/rpc/ExampleService/FindUser": { + Name: "FindUser", + Service: "ExampleService", + Annotations: map[string]string{}, + }, + "/rpc/ExampleService/LogEvent": { + Name: "LogEvent", + Service: "ExampleService", + Annotations: map[string]string{}, + }, } ) diff --git a/_examples/golang-imports/api.gen.go b/_examples/golang-imports/api.gen.go index 7222f61..d0283db 100644 --- a/_examples/golang-imports/api.gen.go +++ b/_examples/golang-imports/api.gen.go @@ -85,10 +85,22 @@ func (x *Location) Is(values ...Location) bool { } var ( - methodAnnotations = map[string]map[string]string{ - "/rpc/ExampleAPI/Ping": {}, - "/rpc/ExampleAPI/Status": {}, - "/rpc/ExampleAPI/GetUsers": {}, + methodAnnotations = map[string]MethodCtx{ + "/rpc/ExampleAPI/Ping": { + Name: "Ping", + Service: "ExampleAPI", + Annotations: map[string]string{}, + }, + "/rpc/ExampleAPI/Status": { + Name: "Status", + Service: "ExampleAPI", + Annotations: map[string]string{}, + }, + "/rpc/ExampleAPI/GetUsers": { + Name: "GetUsers", + Service: "ExampleAPI", + Annotations: map[string]string{}, + }, } ) @@ -512,9 +524,9 @@ func HTTPRequestHeaders(ctx context.Context) (http.Header, bool) { // type MethodCtx struct { - Name string - Service string - Annotations map[string]string + Name string + Service string + Annotations map[string]string } type contextKey struct { diff --git a/types.go.tmpl b/types.go.tmpl index 6ec5115..adb77d8 100644 --- a/types.go.tmpl +++ b/types.go.tmpl @@ -30,13 +30,17 @@ {{- end }} var ( - methodAnnotations = map[string]map[string]string{ + methodAnnotations = map[string]MethodCtx{ {{- range $_, $service := $services -}} {{- range $_, $method := $service.Methods }} "/rpc/{{$service.Name}}/{{$method.Name}}": { - {{- range $_, $annotation := $method.Annotations -}} - "{{$annotation.AnnotationType}}": "{{$annotation.Value}}", - {{- end -}} + Name: "{{$method.Name}}", + Service: "{{$service.Name}}", + Annotations: map[string]string{ + {{- range $_, $annotation := $method.Annotations -}} + "{{$annotation.AnnotationType}}": "{{$annotation.Value}}", + {{- end -}} + }, }, {{- end -}} {{ end }}