forked from zzzkl/go-rancher
/
schemas.go
131 lines (111 loc) · 2.79 KB
/
schemas.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
package catalog
import (
"reflect"
"strings"
)
type Schemas struct {
Collection
Data []Schema `json:"data,omitempty"`
schemasByName map[string]*Schema
}
func (s *Schema) CheckField(name string) (Field, bool) {
for fieldName := range s.ResourceFields {
if fieldName == name {
v, ok := s.ResourceFields[fieldName]
return v, ok
}
}
return Field{}, false
}
func (s *Schema) Field(name string) Field {
f, _ := s.CheckField(name)
return f
}
func (s *Schemas) CheckSchema(name string) (Schema, bool) {
for i := range s.Data {
if s.Data[i].Id == name {
return s.Data[i], true
}
}
return Schema{}, false
}
func (s *Schemas) Schema(name string) Schema {
r, _ := s.CheckSchema(name)
return r
}
func typeToFields(t reflect.Type) map[string]Field {
result := map[string]Field{}
for i := 0; i < t.NumField(); i++ {
schemaField := Field{}
typeField := t.Field(i)
if typeField.Anonymous && typeField.Type.Kind() == reflect.Struct {
parentFields := typeToFields(typeField.Type)
for k, v := range result {
parentFields[k] = v
}
result = parentFields
continue
} else if typeField.Anonymous {
continue
} else if privateFieldRegex.FindStringIndex(typeField.Name) != nil {
continue
}
fieldString := strings.ToLower(typeField.Type.Kind().String())
switch {
case strings.HasPrefix(fieldString, "int") || strings.HasPrefix(fieldString, "uint"):
schemaField.Type = "int"
case fieldString == "bool":
schemaField.Type = fieldString
case fieldString == "float32" || fieldString == "float64":
schemaField.Type = "float"
case fieldString == "string":
schemaField.Type = "string"
case fieldString == "map":
// HACK
schemaField.Type = "map[string]"
case fieldString == "slice":
// HACK
schemaField.Type = "array[string]"
}
name := strings.Split(typeField.Tag.Get("json"), ",")[0]
if name == "" && len(typeField.Name) > 1 {
name = strings.ToLower(typeField.Name[0:1]) + typeField.Name[1:]
} else if name == "" {
name = typeField.Name
}
if schemaField.Type != "" {
result[name] = schemaField
}
}
return result
}
func (s *Schemas) AddType(schemaName string, obj interface{}) *Schema {
t := reflect.TypeOf(obj)
schema := Schema{
Resource: Resource{
Id: schemaName,
Type: "schema",
Links: map[string]string{},
},
PluralName: guessPluralName(schemaName),
ResourceFields: typeToFields(t),
CollectionMethods: []string{"GET"},
ResourceMethods: []string{"GET"},
}
if s.Data == nil {
s.Data = []Schema{}
}
s.Data = append(s.Data, schema)
return &s.Data[len(s.Data)-1]
}
func guessPluralName(name string) string {
if name == "" {
return ""
}
if strings.HasSuffix(name, "s") ||
strings.HasSuffix(name, "ch") ||
strings.HasSuffix(name, "x") {
return name + "es"
}
return name + "s"
}