/
spec.go
100 lines (85 loc) · 2.9 KB
/
spec.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
package types
import "encoding/json"
func Parse(raw []byte) (*Spec, error) {
spec := &Spec{
Modules: []*ModuleSpec{},
}
err := json.Unmarshal(raw, spec)
if err != nil {
return spec, err
}
return spec, err
}
type Spec struct {
Modules []*ModuleSpec `yaml:"modules" json:"modules"`
}
type ModuleSpec struct {
NormalizedName string `yaml:"normalizedName" json:"normalizedName"`
ModuleName string `yaml:"module" json:"module"`
ShortDescription string `yaml:"short_description" json:"short_description"`
Description []string `yaml:"description" json:"description"`
Params ParamSpecs `yaml:"options" json:"options"`
Returns ReturnSpecs `yaml:"returns" json:"returns"`
Path string `yaml:"-" json:"-"`
Documentation string `yaml:"doc" json:"doc"`
Return string `yaml:"return" json:"return"`
SourceLink string `yaml:"-" json:"-"`
}
type ParamSpecs map[string]*ParamSpec
func (p *ParamSpecs) Names() []string {
names := []string{}
for name, _ := range *p {
names = append(names, name)
}
return names
}
func (p *ParamSpecs) Defaults() map[string]interface{} {
vals := map[string]interface{}{}
for name, p := range *p {
if p.Default == nil || p.Default == "<no value>" || p.Default == "" {
continue
}
vals[name] = p.Default
}
return vals
}
type ParamSpec struct {
NormalizedName string `yaml:"normalizedName" json:"normalizedName"`
StructTag string `yaml:"structTag" json:"structTag"`
GoType string `yaml:"goType" json:"goType"`
GoElements string `yaml:"goElements" json:"goElements"`
Description []string `yaml:"description" json:"description"`
Type string `default:"str" yaml:"type" json:"type"`
Required bool `default:"no" yaml:"required" json:"required"`
Default interface{} `yaml:"default" json:"default"`
Elements string `yaml:"elements" json:"elements"`
Aliases []string `yaml:"aliases" json:"aliases"`
}
type ReturnSpecs map[string]*ReturnSpec
func (p *ReturnSpecs) Names() []string {
names := []string{}
for name, _ := range *p {
names = append(names, name)
}
return names
}
type ReturnSpec struct {
NormalizedName string `yaml:"normalizedName" json:"normalizedName"`
Description interface{} `yaml:"description" json:"description"`
Returned string `yaml:"returned" json:"returned"`
// complex type is not supported...
Type string `default:"str" yaml:"type" json:"type"`
Sample interface{} `yaml:"sample" json:"sample"`
GoType string `yaml:"goType" json:"goType"`
StructTag string `yaml:"structTag" json:"structTag"`
}
// Desc special handling since description can be either a string or a string slice.
func (r *ReturnSpec) Desc() []string {
switch t := r.Description.(type) {
case string:
return []string{t}
case []string:
return t
}
return []string{}
}