forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
145 lines (123 loc) · 3.61 KB
/
util.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
package apidocs
import (
"reflect"
"regexp"
"sort"
"strings"
"unicode"
"github.com/go-openapi/spec"
"k8s.io/apimachinery/pkg/runtime/schema"
)
// RefType returns the type name of a reference, suitable for looking up in
// s.Definitions.
func RefType(s *spec.Schema) string {
return strings.TrimPrefix(s.Ref.String(), "#/definitions/")
}
// FriendlyTypeName returns a user-friendly type name.
func FriendlyTypeName(s *spec.Schema) string {
refType := RefType(s)
if refType == "" { // a base type, e.g. "string"
return s.Type[0]
}
// convert, e.g. "io.k8s.kubernetes.pkg.api.v1.Pod" -> "v1.Pod"
parts := strings.Split(refType, ".")
return strings.Join(parts[len(parts)-2:], ".")
}
// EscapeMediaTypes ensures that */* renders correctly in asciidoc format.
// TODO: it'd be better if the template library could escape correctly for
// asciidoc.
func EscapeMediaTypes(mediatypes []string) []string {
rv := make([]string, len(mediatypes))
for i, mediatype := range mediatypes {
rv[i] = mediatype
if mediatype == "*/*" {
rv[i] = `\*/*`
}
}
return rv
}
// GroupVersionKinds returns the GroupVersionKinds from the
// "x-kubernetes-group-version-kind" OpenAPI extension.
func GroupVersionKinds(s spec.Schema) []schema.GroupVersionKind {
e := s.Extensions["x-kubernetes-group-version-kind"]
if e == nil {
return nil
}
gvks := make([]schema.GroupVersionKind, 0, len(e.([]interface{})))
for _, gvk := range e.([]interface{}) {
gvk := gvk.(map[string]interface{})
gvks = append(gvks, schema.GroupVersionKind{
Group: gvk["group"].(string),
Version: gvk["version"].(string),
Kind: gvk["kind"].(string),
})
}
return gvks
}
var opNames = []string{"Get", "Put", "Post", "Delete", "Options", "Head", "Patch"}
// Operations returns the populated operations of a spec.PathItem as a map, for
// easier iteration
func Operations(path spec.PathItem) map[string]*spec.Operation {
ops := make(map[string]*spec.Operation, len(opNames))
v := reflect.ValueOf(path)
for _, opName := range opNames {
op := v.FieldByName(opName).Interface().(*spec.Operation)
if op != nil {
ops[opName] = op
}
}
return ops
}
var envStyleRegexp = regexp.MustCompile(`\{[^}]+\}`)
// EnvStyle replaces instances of {foo} in a string with $FOO.
func EnvStyle(s string) string {
return envStyleRegexp.ReplaceAllStringFunc(s, func(s string) string {
return "$" + strings.ToUpper(s[1:len(s)-1])
})
}
var alreadyPluralSuffixes = []string{"versions", "constraints", "endpoints"}
// Pluralise dumbly attempts to pluralise s.
func Pluralise(s string) string {
l := strings.ToLower(s)
for _, ss := range alreadyPluralSuffixes {
if strings.HasSuffix(l, ss) {
return s
}
}
if strings.HasSuffix(s, "s") {
return s + "es"
}
if strings.HasSuffix(s, "y") {
return s[:len(s)-1] + "ies"
}
return s + "s"
}
// SortedKeys returns a slice containing the sorted keys of map m. Argument t
// is the type implementing sort.Interface which is used to sort.
func SortedKeys(m interface{}, t reflect.Type) interface{} {
v := reflect.ValueOf(m)
if v.Kind() != reflect.Map {
panic("wrong type")
}
s := reflect.MakeSlice(reflect.SliceOf(v.Type().Key()), v.Len(), v.Len())
for i, k := range v.MapKeys() {
s.Index(i).Set(k)
}
s = s.Convert(t)
sort.Sort(s.Interface().(sort.Interface))
return s.Interface()
}
// ToUpper returns s with the first letter capitalised.
func ToUpper(s string) string {
r := []rune(s)
r[0] = unicode.ToUpper(r[0])
return string(r)
}
// ReverseStringSlice returns s reversed, not in place.
func ReverseStringSlice(s []string) []string {
r := make([]string, len(s))
for i := range s {
r[len(r)-1-i] = s[i]
}
return r
}