-
Notifications
You must be signed in to change notification settings - Fork 370
/
resttag.go
59 lines (48 loc) · 1.02 KB
/
resttag.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
package main
import (
"errors"
"strings"
)
const (
header = "header"
body = "body"
query = "query"
requiredTag = "required"
)
// RestTag is a type that represents the valid values of a 'rest' struct tag.
type RestTag struct {
In string
Name string
Required bool
Default string
}
// RestTagFromString is a method to parse a 'rest' struct tag to a resulting RestTag struct.
// This can take the form of rest:in,name,required
func RestTagFromString(tag string) (RestTag, error) {
if tag == "" {
return RestTag{}, errors.New("Nil or empty tag string")
}
entries := strings.Split(tag, ",")
elen := len(entries)
r := RestTag{In: "", Name: "", Required: false}
if elen >= 1 {
r.In = entries[0]
switch r.In {
case header:
case body:
case query:
default:
return RestTag{}, errors.New("Incorrect 'in' value: " + r.In)
}
}
if elen >= 2 {
r.Name = entries[1]
}
if elen >= 3 && entries[2] == "required" {
r.Required = true
}
if elen >= 4 {
r.Default = entries[3]
}
return r, nil
}