-
Notifications
You must be signed in to change notification settings - Fork 16
/
success.go
66 lines (56 loc) · 1.47 KB
/
success.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
package annotation
import (
"fmt"
"net/http"
"strconv"
"strings"
"github.com/RussellLuo/kun/gen/http/spec"
"github.com/RussellLuo/kun/pkg/ifacetool"
)
// ParseSuccess parses s per the format as below:
//
// statusCode=<statusCode> body=<body> manip=`<manipulation> [; <manipulation2> [; ...]]`
//
// The format of `<manipulation>`:
//
// <argName> name=<name> type=<type> descr=<descr>
//
func ParseSuccess(s string, method *ifacetool.Method) (*spec.Response, error) {
resp := new(spec.Response)
returns := make(map[string]*ifacetool.Param)
for _, r := range method.Returns {
returns[r.Name] = r
}
for _, part := range strings.Fields(s) {
kv := strings.SplitN(part, "=", 2)
if len(kv) != 2 {
return nil, fmt.Errorf("invalid argument format: %s", part)
}
key, value := kv[0], kv[1]
switch key {
case "statusCode":
var err error
resp.StatusCode, err = strconv.Atoi(value)
if err != nil {
return nil, fmt.Errorf("%q cannot be converted to an integer: %v", value, err)
}
case "body":
if _, ok := returns[value]; !ok {
return nil, fmt.Errorf("no result `%s` declared in the method %s", value, method.Name)
}
resp.BodyField = value
case "manip":
// TODO: Add support for the `manip` argument.
fallthrough
default:
return nil, fmt.Errorf("invalid tag part: %s", part)
}
}
if resp.StatusCode == 0 {
resp.StatusCode = http.StatusOK
}
if resp.MediaType == "" {
resp.MediaType = spec.MediaTypeJSON
}
return resp, nil
}