-
Notifications
You must be signed in to change notification settings - Fork 16
/
body.go
83 lines (69 loc) · 1.73 KB
/
body.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
package annotation
import (
"fmt"
"regexp"
"strings"
"github.com/RussellLuo/kun/gen/http/spec"
"github.com/RussellLuo/kun/gen/util/annotation"
)
const (
OptionNoBody = "-"
)
var (
reSingleVarName = regexp.MustCompile(`^\w+$`)
)
type Manipulation struct {
Name string
Type string
Description string
}
type Body struct {
Field string
Manipulations map[string]*Manipulation
}
// ParseBody parses s per the format as below:
//
// <field> or <manipulation> [; <manipulation2> [; ...]]
//
// The format of `<manipulation>`:
//
// <argName> name=<name> type=<type> descr=<descr>
//
func ParseBody(s string) (*Body, error) {
// Simple format: <field>
if s == OptionNoBody || reSingleVarName.MatchString(s) {
return &Body{Field: s}, nil
}
// Complicated format: <manipulation> [; <manipulation2> [; ...]]
m := make(map[string]*Manipulation)
for _, text := range strings.Split(s, ";") {
text = strings.TrimSpace(text)
if text == "" {
break
}
param, err := parseParam(text)
if err != nil {
return nil, err
}
if len(param.Params) != 1 {
return nil, fmt.Errorf("bad manipulation %q in %s", s, annotation.DirectiveHTTPBody)
}
p := param.Params[0]
if p.In != spec.InQuery {
// XXX: Handle the case of manual definition `in=query`.
return nil, fmt.Errorf("parameter `in` is unsupported in body manipulation")
}
if p.Required {
return nil, fmt.Errorf("parameter `required` is unsupported in body manipulation")
}
m[param.ArgName] = &Manipulation{
Name: p.Name,
Type: p.Type,
Description: p.Description,
}
}
if len(m) > 0 {
return &Body{Manipulations: m}, nil
}
return nil, fmt.Errorf("invalid %s directive: %s", annotation.DirectiveHTTPBody, s)
}