Skip to content

Commit

Permalink
prevent nil panics
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasJenicek committed Sep 30, 2024
1 parent 2029ca9 commit 2269e08
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 11 deletions.
5 changes: 4 additions & 1 deletion _examples/golang-basics/example.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions _examples/golang-imports/api.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions helpers.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions server.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion types.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 -}}
Expand Down

0 comments on commit 2269e08

Please sign in to comment.