-
Notifications
You must be signed in to change notification settings - Fork 16
/
annotation.go
122 lines (100 loc) · 2.71 KB
/
annotation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package annotation
import (
"fmt"
"regexp"
"strings"
"github.com/RussellLuo/kun/gen/http/spec"
"github.com/RussellLuo/kun/gen/util/annotation"
"github.com/RussellLuo/kun/pkg/ifacetool"
)
var (
reHTTP = regexp.MustCompile(`^` + annotation.DirectivePrefix + `(\w+)\s+(.+)$`)
)
type InterfaceAnnotation struct {
Metadata *spec.Metadata
Aliases Aliases
}
func ParseInterfaceAnnotation(doc []string) (*InterfaceAnnotation, error) {
m, err := ParseMetadata(doc)
if err != nil {
return nil, err
}
aliases, err := ParseAliases(doc)
if err != nil {
return nil, err
}
return &InterfaceAnnotation{
Metadata: m,
Aliases: aliases,
}, nil
}
type MethodAnnotation struct {
Ops []*Op
Params map[string]*Param
Body *Body
Success *spec.Response
Tags []string
}
func ParseMethodAnnotation(method *ifacetool.Method, aliases Aliases) (*MethodAnnotation, error) {
anno := &MethodAnnotation{Params: make(map[string]*Param)}
for _, comment := range method.Doc {
if annotation.Directive(comment).Dialect() != annotation.DialectHTTP {
continue
}
result := reHTTP.FindStringSubmatch(comment)
if len(result) != 3 {
return nil, fmt.Errorf("invalid %s directive: %s", annotation.Name, comment)
}
key, value := result[1], strings.TrimSpace(result[2])
switch d := annotation.FromSubDirective(key); d {
case annotation.DirectiveHTTPOp:
op, err := ParseOp(value)
if err != nil {
return nil, err
}
anno.Ops = append(anno.Ops, op)
case annotation.DirectiveHTTPParam:
v, err := aliases.Eval(value)
if err != nil {
return nil, err
}
params, err := ParseParams(v)
if err != nil {
return nil, err
}
for _, p := range params {
anno.Params[p.ArgName] = p
}
case annotation.DirectiveHTTPBody:
if anno.Body != nil {
return nil, fmt.Errorf("duplicate %s directive in: %s", d, comment)
}
body, err := ParseBody(value)
if err != nil {
return nil, err
}
anno.Body = body
case annotation.DirectiveHTTPSuccess:
if anno.Success != nil {
return nil, fmt.Errorf("duplicate %s directive in: %s", d, comment)
}
success, err := ParseSuccess(value, method)
if err != nil {
return nil, err
}
anno.Success = success
case annotation.DirectiveHTTPOAS:
if len(anno.Tags) > 0 {
return nil, fmt.Errorf("duplicate %s directive in: %s", d, comment)
}
parts := strings.SplitN(value, ":", 2)
if len(parts) != 2 || parts[0] != "tags" {
return nil, fmt.Errorf(`%q does not match the expected format: "tags:<tag1>[,<tag2>]"`, value)
}
anno.Tags = strings.Split(parts[1], ",")
default:
return nil, fmt.Errorf(`unrecognized %s directive "%s" in comment: %s`, annotation.Name, key, comment)
}
}
return anno, nil
}