/
TargetLocation.go
146 lines (131 loc) · 4.41 KB
/
TargetLocation.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package model
import "github.com/tsaikd/KDGoLib/enumutil"
// TargetLocations slice of TargetLocation
type TargetLocations []TargetLocation
// UnmarshalYAML implement yaml unmarshaler
func (t *TargetLocations) UnmarshalYAML(unmarshaler func(interface{}) error) (err error) {
// allowedTargets could be a string or string array
*t = TargetLocations{}
tarlocs := []string{}
bufstr := ""
if err = unmarshaler(&bufstr); err != nil {
if !isErrorYAMLIntoString(err) {
return
}
if err = unmarshaler(&tarlocs); err != nil {
return
}
} else {
tarlocs = []string{bufstr}
}
for _, tarloc := range tarlocs {
enum := ParseTargetLocation(tarloc)
if enum < 1 {
return ErrorInvalidTargetLocation1.New(nil, tarloc)
}
*t = append(*t, enum)
}
return nil
}
// IsEmpty return true if it is empty
func (t TargetLocations) IsEmpty() bool {
return len(t) < 1
}
// IsAllowed return true if targetLocation is allowed
func (t TargetLocations) IsAllowed(targetLocation TargetLocation) bool {
if t.IsEmpty() {
// not limited for target location
return true
}
for _, tarloc := range t {
if targetLocation == tarloc {
return true
}
}
return false
}
// TargetLocation the location within an API specification where annotations
// can be applied MUST be one of the target locations in the following
// Target Locations table. The targets are the locations themselves,
// not sub-properties within the locations; for example, the Method target
// refers to the method node, not to the method display name, description,
// and so on.
type TargetLocation int8
// List all valid enum
const (
// The root of a RAML document
TargetLocationAPI TargetLocation = 1 + iota
// An item in the collection of items that is the value of the root-level documentation node
TargetLocationDocumentationItem
// A resource (relative URI) node, root-level or nested
TargetLocationResource
// A method node
TargetLocationMethod
// A declaration of the responses node, whose key is an HTTP status code
TargetLocationResponse
// The body node of a method
TargetLocationRequestBody
// The body node of a response
TargetLocationResponseBody
// A data type declaration (inline or in a global types collection),
// header declaration, query parameter declaration, URI parameter declaration,
// or a property within any of these declarations, where the type property
// can be used
TargetLocationTypeDeclaration
// Either an example or examples node
TargetLocationExample
// A resource type node
TargetLocationResourceType
// A trait node
TargetLocationTrait
// A security scheme declaration
TargetLocationSecurityScheme
// The settings node of a security scheme declaration
TargetLocationSecuritySchemeSettings
// A declaration of the annotationTypes node, whose key is a name of an annotation type and whose value describes the annotation
TargetLocationAnnotationType
// The root of a library
TargetLocationLibrary
// The root of an overlay
TargetLocationOverlay
// The root of an extension
TargetLocationExtension
)
var factoryTargetLocation = enumutil.NewEnumFactory().
Add(TargetLocationAPI, "API").
Add(TargetLocationDocumentationItem, "DocumentationItem").
Add(TargetLocationResource, "Resource").
Add(TargetLocationMethod, "Method").
Add(TargetLocationResponse, "Response").
Add(TargetLocationRequestBody, "RequestBody").
Add(TargetLocationResponseBody, "ResponseBody").
Add(TargetLocationTypeDeclaration, "TypeDeclaration").
Add(TargetLocationExample, "Example").
Add(TargetLocationResourceType, "ResourceType").
Add(TargetLocationTrait, "Trait").
Add(TargetLocationSecurityScheme, "SecurityScheme").
Add(TargetLocationSecuritySchemeSettings, "SecuritySchemeSettings").
Add(TargetLocationAnnotationType, "AnnotationType").
Add(TargetLocationLibrary, "Library").
Add(TargetLocationOverlay, "Overlay").
Add(TargetLocationExtension, "Extension").
Build()
func (t TargetLocation) String() string {
return factoryTargetLocation.String(t)
}
// MarshalJSON return jsonfy []byte of enum
func (t TargetLocation) MarshalJSON() ([]byte, error) {
return factoryTargetLocation.MarshalJSON(t)
}
// UnmarshalJSON decode json data to enum
func (t *TargetLocation) UnmarshalJSON(b []byte) (err error) {
return factoryTargetLocation.UnmarshalJSON(t, b)
}
// ParseTargetLocation string to enum
func ParseTargetLocation(s string) TargetLocation {
enum, err := factoryTargetLocation.ParseString(s)
if err != nil {
return 0
}
return enum.(TargetLocation)
}