This repository was archived by the owner on Sep 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathparam.go
130 lines (113 loc) · 2.63 KB
/
param.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
package models
import "github.com/xeipuuv/gojsonschema"
// Param represents metadata about a Parameter or a Preference.
// Specifically, it contains a description of what the information
// is for, detailed documentation about the param, and a JSON schema that
// the param must match to be considered valid.
// swagger:model
type Param struct {
Validation
Access
Meta
Owned
Bundled
// Name is the name of the param. Params must be uniquely named.
//
// required: true
Name string `index:",key"`
// Description is a one-line description of the parameter.
Description string
// Documentation details what the parameter does, what values it can
// take, what it is used for, etc.
Documentation string
// Secure implies that any API interactions with this Param
// will deal with SecureData values.
//
// required: true
Secure bool
// Schema must be a valid JSONSchema as of draft v4.
//
// required: true
Schema interface{}
}
func (p *Param) GetMeta() Meta {
return p.Meta
}
func (p *Param) SetMeta(d Meta) {
p.Meta = d
}
func (p *Param) GetDocumentation() string {
return p.Documentation
}
func (p *Param) GetDescription() string {
return p.Description
}
func (p *Param) DefaultValue() (interface{}, bool) {
if km, ok := p.Schema.(map[string]interface{}); ok {
v, vok := km["default"]
return v, vok
}
return nil, false
}
func (p *Param) TypeValue() (interface{}, bool) {
if km, ok := p.Schema.(map[string]interface{}); ok {
v, vok := km["type"]
return v, vok
}
return nil, false
}
func (p *Param) Validate() {
p.AddError(ValidParamName("Invalid Name", p.Name))
if p.Schema != nil {
validator, err := gojsonschema.NewSchema(gojsonschema.NewGoLoader(p.Schema))
if err != nil {
p.AddError(err)
return
}
dv, ok := p.DefaultValue()
if !ok {
return
}
res, err := validator.Validate(gojsonschema.NewGoLoader(dv))
if err != nil {
p.Errorf("Error validating default value: %v", err)
} else if !res.Valid() {
for _, e := range res.Errors() {
p.Errorf("Error in default value: %v", e.String())
}
}
}
}
func (p *Param) SetName(s string) {
p.Name = s
}
func (p *Param) Prefix() string {
return "params"
}
func (p *Param) Key() string {
return p.Name
}
func (p *Param) KeyName() string {
return "Name"
}
func (p *Param) Fill() {
if p.Meta == nil {
p.Meta = Meta{}
}
p.Validation.fill(p)
}
func (p *Param) AuthKey() string {
return p.Key()
}
func (b *Param) SliceOf() interface{} {
s := []*Param{}
return &s
}
func (b *Param) ToModels(obj interface{}) []Model {
items := obj.(*[]*Param)
res := make([]Model, len(*items))
for i, item := range *items {
res[i] = Model(item)
}
return res
}