From 1af2de23ced037778b8e1f8d3fea1a0970e1b672 Mon Sep 17 00:00:00 2001 From: Lukas Jenicek Date: Fri, 27 Sep 2024 12:11:56 +0200 Subject: [PATCH] rename to MethodAnnotationsFromCtx --- _examples/golang-basics/example.gen.go | 37 +++++++++++++------------- _examples/golang-basics/example.ridl | 1 + _examples/golang-imports/api.gen.go | 29 ++++++++++---------- helpers.go.tmpl | 4 +-- server.go.tmpl | 2 +- 5 files changed, 38 insertions(+), 35 deletions(-) diff --git a/_examples/golang-basics/example.gen.go b/_examples/golang-basics/example.gen.go index ebabb62..4300d25 100644 --- a/_examples/golang-basics/example.gen.go +++ b/_examples/golang-basics/example.gen.go @@ -1,6 +1,6 @@ -// example v0.0.1 9d2e85c0083323599454454bc06526ab08edd24e +// example v0.0.1 05b7a5c86b98738f4fe6ce9bb1fccd4af064847a // -- -// Code generated by webrpc-gen@v0.19.3-8-g87b50a2 with ../../../gen-golang generator. DO NOT EDIT. +// Code generated by webrpc-gen@v0.19.3-10-gacc48d2 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 @@ -33,7 +33,7 @@ func WebRPCSchemaVersion() string { // Schema hash generated from your RIDL schema func WebRPCSchemaHash() string { - return "9d2e85c0083323599454454bc06526ab08edd24e" + return "05b7a5c86b98738f4fe6ce9bb1fccd4af064847a" } // @@ -160,6 +160,17 @@ type ComplexType struct { User *User `json:"user"` } +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": {}, + } +) + var WebRPCServices = map[string][]string{ "ExampleService": { "Ping", @@ -211,20 +222,10 @@ type WebRPCServer interface { http.Handler } -var ( - annotations = map[string]map[string]string{ - "/rpc/ExampleService/Ping": {}, - "/rpc/ExampleService/Status": {}, - "/rpc/ExampleService/Version": {}, - "/rpc/ExampleService/GetUser": {}, - "/rpc/ExampleService/FindUser": {}, - "/rpc/ExampleService/LogEvent": {}} -) - type exampleServiceServer struct { ExampleService OnError func(r *http.Request, rpcErr *WebRPCError) - OnRequest func(w http.ResponseWriter, r *http.Request) + OnRequest func(w http.ResponseWriter, r *http.Request) error } func NewExampleServiceServer(svc ExampleService) *exampleServiceServer { @@ -246,7 +247,7 @@ func (s *exampleServiceServer) ServeHTTP(w http.ResponseWriter, r *http.Request) ctx = context.WithValue(ctx, HTTPResponseWriterCtxKey, w) ctx = context.WithValue(ctx, HTTPRequestCtxKey, r) ctx = context.WithValue(ctx, ServiceNameCtxKey, "ExampleService") - ctx = context.WithValue(ctx, AnnotationsCtxKey, annotations[r.URL.Path]) + ctx = context.WithValue(ctx, MethodAnnotationsCtxKey, methodAnnotations[r.URL.Path]) if s.OnRequest != nil { s.OnRequest(w, r) @@ -783,7 +784,7 @@ var ( MethodNameCtxKey = &contextKey{"MethodName"} - AnnotationsCtxKey = &contextKey{"Annotations"} + MethodAnnotationsCtxKey = &contextKey{"MethodAnnotations"} ) func ServiceNameFromContext(ctx context.Context) string { @@ -801,8 +802,8 @@ func RequestFromContext(ctx context.Context) *http.Request { return r } -func AnnotationsFromContext(ctx context.Context) map[string]string { - annotations, _ := ctx.Value(AnnotationsCtxKey).(map[string]string) +func MethodAnnotationsFromContext(ctx context.Context) map[string]string { + annotations, _ := ctx.Value(MethodAnnotationsCtxKey).(map[string]string) return annotations } func ResponseWriterFromContext(ctx context.Context) http.ResponseWriter { diff --git a/_examples/golang-basics/example.ridl b/_examples/golang-basics/example.ridl index 9b9f739..d60169f 100644 --- a/_examples/golang-basics/example.ridl +++ b/_examples/golang-basics/example.ridl @@ -92,6 +92,7 @@ service ExampleService # Status endpoint # # gives you current status of running application + @internal - Status() => (status: bool) - Version() => (version: Version) - GetUser(header: map, userID: uint64) => (user: User) diff --git a/_examples/golang-imports/api.gen.go b/_examples/golang-imports/api.gen.go index f717b10..6976cbd 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 cae4e128f4fb4c938bfe1ea312deeea3dfd6b6af // -- -// Code generated by webrpc-gen@v0.19.3-8-g87b50a2 with ../../../gen-golang generator. DO NOT EDIT. +// Code generated by webrpc-gen@v0.19.3-10-gacc48d2 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 @@ -82,7 +82,15 @@ func (x *Location) Is(values ...Location) bool { } } return false -} +}var ( + methodAnnotations = map[string]map[string]string{ + "/rpc/ExampleAPI/Ping": {}, + "/rpc/ExampleAPI/Status": {}, + "/rpc/ExampleAPI/GetUsers": {}, + } +) + + var WebRPCServices = map[string][]string{ "ExampleAPI": { @@ -126,17 +134,10 @@ type WebRPCServer interface { http.Handler } -var ( - annotations = map[string]map[string]string{ - "/rpc/ExampleAPI/Ping": {}, - "/rpc/ExampleAPI/Status": {}, - "/rpc/ExampleAPI/GetUsers": {},} -) - type exampleAPIServer struct { ExampleAPI OnError func(r *http.Request, rpcErr *WebRPCError) - OnRequest func(w http.ResponseWriter, r *http.Request) + OnRequest func(w http.ResponseWriter, r *http.Request) error } func NewExampleAPIServer(svc ExampleAPI) *exampleAPIServer { @@ -158,7 +159,7 @@ func (s *exampleAPIServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { ctx = context.WithValue(ctx, HTTPResponseWriterCtxKey, w) ctx = context.WithValue(ctx, HTTPRequestCtxKey, r) ctx = context.WithValue(ctx, ServiceNameCtxKey, "ExampleAPI") - ctx = context.WithValue(ctx, AnnotationsCtxKey, annotations[r.URL.Path]) + ctx = context.WithValue(ctx, MethodAnnotationsCtxKey, methodAnnotations[r.URL.Path]) if s.OnRequest != nil { s.OnRequest(w, r) @@ -521,7 +522,7 @@ var ( MethodNameCtxKey = &contextKey{"MethodName"} - AnnotationsCtxKey = &contextKey{"Annotations"} + MethodAnnotationsCtxKey = &contextKey{"MethodAnnotations"} ) func ServiceNameFromContext(ctx context.Context) string { @@ -539,8 +540,8 @@ func RequestFromContext(ctx context.Context) *http.Request { return r } -func AnnotationsFromContext(ctx context.Context) map[string]string { - annotations, _ := ctx.Value(AnnotationsCtxKey).(map[string]string) +func MethodAnnotationsFromContext(ctx context.Context) map[string]string { + annotations, _ := ctx.Value(MethodAnnotationsCtxKey).(map[string]string) return annotations } func ResponseWriterFromContext(ctx context.Context) http.ResponseWriter { diff --git a/helpers.go.tmpl b/helpers.go.tmpl index cd41541..32036d8 100644 --- a/helpers.go.tmpl +++ b/helpers.go.tmpl @@ -27,7 +27,7 @@ var ( MethodNameCtxKey = &contextKey{"MethodName"} - AnnotationsCtxKey = &contextKey{"Annotations"} + MethodAnnotationsCtxKey = &contextKey{"MethodAnnotations"} ) func ServiceNameFromContext(ctx context.Context) string { @@ -46,7 +46,7 @@ func RequestFromContext(ctx context.Context) *http.Request { } func MethodAnnotationsFromContext(ctx context.Context) map[string]string { - annotations, _ := ctx.Value(AnnotationsCtxKey).(map[string]string) + annotations, _ := ctx.Value(MethodAnnotationsCtxKey).(map[string]string) return annotations } diff --git a/server.go.tmpl b/server.go.tmpl index dad9d89..87039a1 100644 --- a/server.go.tmpl +++ b/server.go.tmpl @@ -42,7 +42,7 @@ func (s *{{$serviceName}}) ServeHTTP(w http.ResponseWriter, r *http.Request) { ctx = context.WithValue(ctx, HTTPResponseWriterCtxKey, w) ctx = context.WithValue(ctx, HTTPRequestCtxKey, r) ctx = context.WithValue(ctx, ServiceNameCtxKey, "{{.Name}}") - ctx = context.WithValue(ctx, AnnotationsCtxKey, annotations[r.URL.Path]) + ctx = context.WithValue(ctx, MethodAnnotationsCtxKey, methodAnnotations[r.URL.Path]) if s.OnRequest != nil { s.OnRequest(w, r)