forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
objectschema.go
64 lines (53 loc) · 1.65 KB
/
objectschema.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
package apidocs
import (
"fmt"
"reflect"
"sort"
"strings"
"github.com/go-openapi/spec"
)
type line struct {
Prefix string
Name string
Schema *spec.Schema
Lines []*line
}
func newLine(prefix, name string, schema *spec.Schema) *line {
return &line{
Prefix: prefix,
Name: name,
Schema: schema,
}
}
func (l *line) Tooltip() string {
tooltipText := fmt.Sprintf("(%s)", FriendlyTypeName(l.Schema))
if l.Schema.Description != "" {
tooltipText += " " + l.Schema.Description
}
return tooltipText
}
// Open controls which lines have the details displayed
func (l *line) Open() bool {
return l.Prefix == "" && (l.Name == "metadata" || l.Name == "spec")
}
func buildLines(s *spec.Swagger, schema spec.Schema, prefix string) (lines []*line) {
for _, name := range SortedKeys(schema.Properties, reflect.TypeOf(sort.StringSlice{})).(sort.StringSlice) {
property := schema.Properties[name]
l := newLine(prefix, name, &property)
switch {
case property.Type.Contains("array"):
if property.Items.Schema.Ref.String() != "" {
l.Lines = buildLines(s, s.Definitions[RefType(property.Items.Schema)], prefix+"- ")
} else {
l.Lines = []*line{newLine(prefix+"- ", "["+property.Items.Schema.Type[0]+"]", property.Items.Schema)}
}
case property.Type.Contains("object"):
l.Lines = []*line{newLine(prefix+" ", "[string]", &spec.Schema{SchemaProps: spec.SchemaProps{Type: []string{"string"}}})}
case RefType(&property) != "" && len(s.Definitions[RefType(&property)].Properties) > 0:
l.Lines = buildLines(s, s.Definitions[RefType(&property)], prefix+" ")
}
lines = append(lines, l)
prefix = strings.Replace(prefix, "-", " ", -1)
}
return
}