From 2269e08fb5f61b27a8e2442ef9f6655597a9a812 Mon Sep 17 00:00:00 2001 From: Lukas Jenicek Date: Mon, 30 Sep 2024 15:28:04 +0200 Subject: [PATCH] prevent nil panics --- _examples/golang-basics/example.gen.go | 5 ++++- _examples/golang-imports/api.gen.go | 9 ++++++--- helpers.go.tmpl | 11 +++++++---- server.go.tmpl | 4 ++-- types.go.tmpl | 2 +- 5 files changed, 20 insertions(+), 11 deletions(-) diff --git a/_examples/golang-basics/example.gen.go b/_examples/golang-basics/example.gen.go index a8d745e..6603744 100644 --- a/_examples/golang-basics/example.gen.go +++ b/_examples/golang-basics/example.gen.go @@ -818,7 +818,10 @@ func RequestFromContext(ctx context.Context) *http.Request { func MethodFromContext(ctx context.Context) MethodCtx { name, _ := ctx.Value(MethodNameCtxKey).(string) service, _ := ctx.Value(ServiceNameCtxKey).(string) - annotations, _ := ctx.Value(methodAnnotationsCtxKey).(map[string]string) + annotations, ok := ctx.Value(methodAnnotationsCtxKey).(map[string]string) + if !ok { + annotations = map[string]string{} + } return MethodCtx{ Name: name, diff --git a/_examples/golang-imports/api.gen.go b/_examples/golang-imports/api.gen.go index 3127573..7222f61 100644 --- a/_examples/golang-imports/api.gen.go +++ b/_examples/golang-imports/api.gen.go @@ -190,7 +190,7 @@ func (s *exampleAPIServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { switch contentType { case "application/json": - if s.OnRequest != nil { + if s.OnRequest != nil { if err := s.OnRequest(w, r); err != nil { rpcErr, ok := err.(WebRPCError) if !ok { @@ -199,7 +199,7 @@ func (s *exampleAPIServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { s.sendErrorJSON(w, r, rpcErr) return } - } + } handler(ctx, w, r) default: @@ -556,7 +556,10 @@ func RequestFromContext(ctx context.Context) *http.Request { func MethodFromContext(ctx context.Context) MethodCtx { name, _ := ctx.Value(MethodNameCtxKey).(string) service, _ := ctx.Value(ServiceNameCtxKey).(string) - annotations, _ := ctx.Value(methodAnnotationsCtxKey).(map[string]string) + annotations, ok := ctx.Value(methodAnnotationsCtxKey).(map[string]string) + if !ok { + annotations = map[string]string{} + } return MethodCtx{ Name: name, diff --git a/helpers.go.tmpl b/helpers.go.tmpl index 3a7f2ad..cbf995d 100644 --- a/helpers.go.tmpl +++ b/helpers.go.tmpl @@ -6,9 +6,9 @@ // type MethodCtx struct { - Name string - Service string - Annotations map[string]string + Name string + Service string + Annotations map[string]string } type contextKey struct { @@ -54,7 +54,10 @@ func RequestFromContext(ctx context.Context) *http.Request { func MethodFromContext(ctx context.Context) MethodCtx { name, _ := ctx.Value(MethodNameCtxKey).(string) service, _ := ctx.Value(ServiceNameCtxKey).(string) - annotations, _ := ctx.Value(methodAnnotationsCtxKey).(map[string]string) + annotations, ok := ctx.Value(methodAnnotationsCtxKey).(map[string]string) + if !ok { + annotations = map[string]string{} + } return MethodCtx{ Name: name, diff --git a/server.go.tmpl b/server.go.tmpl index 5c0a77a..6589f00 100644 --- a/server.go.tmpl +++ b/server.go.tmpl @@ -71,7 +71,7 @@ func (s *{{$serviceName}}) ServeHTTP(w http.ResponseWriter, r *http.Request) { switch contentType { case "application/json": - if s.OnRequest != nil { + if s.OnRequest != nil { if err := s.OnRequest(w, r); err != nil { rpcErr, ok := err.(WebRPCError) if !ok { @@ -80,7 +80,7 @@ func (s *{{$serviceName}}) ServeHTTP(w http.ResponseWriter, r *http.Request) { s.sendErrorJSON(w, r, rpcErr) return } - } + } handler(ctx, w, r) default: diff --git a/types.go.tmpl b/types.go.tmpl index 6cd3b19..6ec5115 100644 --- a/types.go.tmpl +++ b/types.go.tmpl @@ -35,7 +35,7 @@ var ( {{- range $_, $method := $service.Methods }} "/rpc/{{$service.Name}}/{{$method.Name}}": { {{- range $_, $annotation := $method.Annotations -}} - "{{$annotation.AnnotationType}}": "{{$annotation.Value}}", + "{{$annotation.AnnotationType}}": "{{$annotation.Value}}", {{- end -}} }, {{- end -}}