Skip to content

Commit d6045a7

Browse files
ericwennphiliphassel
authored andcommitted
fix: dont panic on methods without http rules
* httprule.Get returns !ok when there is no http rule annotation * use nil safe field accessors for http rules
1 parent 4f8a18e commit d6045a7

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

internal/httprule/rule.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ import (
1010
)
1111

1212
func Get(m protoreflect.MethodDescriptor) (*annotations.HttpRule, bool) {
13-
if descriptors, ok := proto.GetExtension(
14-
m.Options(), annotations.E_Http,
15-
).(*annotations.HttpRule); ok {
16-
return descriptors, true
13+
descriptor, ok := proto.GetExtension(m.Options(), annotations.E_Http).(*annotations.HttpRule)
14+
if !ok || descriptor == nil {
15+
return nil, false
1716
}
18-
return nil, false
17+
return descriptor, true
1918
}
2019

2120
type Rule struct {
@@ -57,7 +56,7 @@ func ParseRule(httpRule *annotations.HttpRule) (Rule, error) {
5756
}
5857

5958
func httpRuleURL(rule *annotations.HttpRule) (string, error) {
60-
switch v := rule.Pattern.(type) {
59+
switch v := rule.GetPattern().(type) {
6160
case *annotations.HttpRule_Get:
6261
return v.Get, nil
6362
case *annotations.HttpRule_Post:
@@ -76,7 +75,7 @@ func httpRuleURL(rule *annotations.HttpRule) (string, error) {
7675
}
7776

7877
func httpRuleMethod(rule *annotations.HttpRule) (string, error) {
79-
switch v := rule.Pattern.(type) {
78+
switch v := rule.GetPattern().(type) {
8079
case *annotations.HttpRule_Get:
8180
return http.MethodGet, nil
8281
case *annotations.HttpRule_Post:

0 commit comments

Comments
 (0)