Skip to content

Commit

Permalink
fix: dont panic on methods without http rules
Browse files Browse the repository at this point in the history
* httprule.Get returns !ok when there is no http rule annotation
* use nil safe field accessors for http rules
  • Loading branch information
ericwenn authored and philiphassel committed Jul 30, 2021
1 parent 4f8a18e commit d6045a7
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions internal/httprule/rule.go
Expand Up @@ -10,12 +10,11 @@ import (
)

func Get(m protoreflect.MethodDescriptor) (*annotations.HttpRule, bool) {
if descriptors, ok := proto.GetExtension(
m.Options(), annotations.E_Http,
).(*annotations.HttpRule); ok {
return descriptors, true
descriptor, ok := proto.GetExtension(m.Options(), annotations.E_Http).(*annotations.HttpRule)
if !ok || descriptor == nil {
return nil, false
}
return nil, false
return descriptor, true
}

type Rule struct {
Expand Down Expand Up @@ -57,7 +56,7 @@ func ParseRule(httpRule *annotations.HttpRule) (Rule, error) {
}

func httpRuleURL(rule *annotations.HttpRule) (string, error) {
switch v := rule.Pattern.(type) {
switch v := rule.GetPattern().(type) {
case *annotations.HttpRule_Get:
return v.Get, nil
case *annotations.HttpRule_Post:
Expand All @@ -76,7 +75,7 @@ func httpRuleURL(rule *annotations.HttpRule) (string, error) {
}

func httpRuleMethod(rule *annotations.HttpRule) (string, error) {
switch v := rule.Pattern.(type) {
switch v := rule.GetPattern().(type) {
case *annotations.HttpRule_Get:
return http.MethodGet, nil
case *annotations.HttpRule_Post:
Expand Down

0 comments on commit d6045a7

Please sign in to comment.