PageRenderTime 57ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/cmd/cluster-capacity/go/src/github.com/kubernetes-incubator/cluster-capacity/vendor/github.com/go-openapi/swag/util.go

https://bitbucket.org/enterstudiosbiz/origin
Go | 336 lines | 261 code | 30 blank | 45 comment | 38 complexity | a04afec5fd5bf9b824cb90e7121bdc96 MD5 | raw file
Possible License(s): Apache-2.0, 0BSD, GPL-2.0, BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, MIT, LGPL-3.0
  1. // Copyright 2015 go-swagger maintainers
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package swag
  15. import (
  16. "math"
  17. "reflect"
  18. "regexp"
  19. "sort"
  20. "strings"
  21. "unicode"
  22. )
  23. // Taken from https://github.com/golang/lint/blob/3390df4df2787994aea98de825b964ac7944b817/lint.go#L732-L769
  24. var commonInitialisms = map[string]bool{
  25. "ACL": true,
  26. "API": true,
  27. "ASCII": true,
  28. "CPU": true,
  29. "CSS": true,
  30. "DNS": true,
  31. "EOF": true,
  32. "GUID": true,
  33. "HTML": true,
  34. "HTTPS": true,
  35. "HTTP": true,
  36. "ID": true,
  37. "IP": true,
  38. "JSON": true,
  39. "LHS": true,
  40. "QPS": true,
  41. "RAM": true,
  42. "RHS": true,
  43. "RPC": true,
  44. "SLA": true,
  45. "SMTP": true,
  46. "SQL": true,
  47. "SSH": true,
  48. "TCP": true,
  49. "TLS": true,
  50. "TTL": true,
  51. "UDP": true,
  52. "UI": true,
  53. "UID": true,
  54. "UUID": true,
  55. "URI": true,
  56. "URL": true,
  57. "UTF8": true,
  58. "VM": true,
  59. "XML": true,
  60. "XMPP": true,
  61. "XSRF": true,
  62. "XSS": true,
  63. }
  64. var initialisms []string
  65. func init() {
  66. for k := range commonInitialisms {
  67. initialisms = append(initialisms, k)
  68. }
  69. sort.Sort(sort.Reverse(byLength(initialisms)))
  70. }
  71. // JoinByFormat joins a string array by a known format:
  72. // ssv: space separated value
  73. // tsv: tab separated value
  74. // pipes: pipe (|) separated value
  75. // csv: comma separated value (default)
  76. func JoinByFormat(data []string, format string) []string {
  77. if len(data) == 0 {
  78. return data
  79. }
  80. var sep string
  81. switch format {
  82. case "ssv":
  83. sep = " "
  84. case "tsv":
  85. sep = "\t"
  86. case "pipes":
  87. sep = "|"
  88. case "multi":
  89. return data
  90. default:
  91. sep = ","
  92. }
  93. return []string{strings.Join(data, sep)}
  94. }
  95. // SplitByFormat splits a string by a known format:
  96. // ssv: space separated value
  97. // tsv: tab separated value
  98. // pipes: pipe (|) separated value
  99. // csv: comma separated value (default)
  100. func SplitByFormat(data, format string) []string {
  101. if data == "" {
  102. return nil
  103. }
  104. var sep string
  105. switch format {
  106. case "ssv":
  107. sep = " "
  108. case "tsv":
  109. sep = "\t"
  110. case "pipes":
  111. sep = "|"
  112. case "multi":
  113. return nil
  114. default:
  115. sep = ","
  116. }
  117. var result []string
  118. for _, s := range strings.Split(data, sep) {
  119. if ts := strings.TrimSpace(s); ts != "" {
  120. result = append(result, ts)
  121. }
  122. }
  123. return result
  124. }
  125. type byLength []string
  126. func (s byLength) Len() int {
  127. return len(s)
  128. }
  129. func (s byLength) Swap(i, j int) {
  130. s[i], s[j] = s[j], s[i]
  131. }
  132. func (s byLength) Less(i, j int) bool {
  133. return len(s[i]) < len(s[j])
  134. }
  135. // Prepares strings by splitting by caps, spaces, dashes, and underscore
  136. func split(str string) (words []string) {
  137. repl := strings.NewReplacer(
  138. "@", "At ",
  139. "&", "And ",
  140. "|", "Pipe ",
  141. "$", "Dollar ",
  142. "!", "Bang ",
  143. "-", " ",
  144. "_", " ",
  145. )
  146. rex1 := regexp.MustCompile(`(\p{Lu})`)
  147. rex2 := regexp.MustCompile(`(\pL|\pM|\pN|\p{Pc})+`)
  148. str = trim(str)
  149. // Convert dash and underscore to spaces
  150. str = repl.Replace(str)
  151. // Split when uppercase is found (needed for Snake)
  152. str = rex1.ReplaceAllString(str, " $1")
  153. // check if consecutive single char things make up an initialism
  154. for _, k := range initialisms {
  155. str = strings.Replace(str, rex1.ReplaceAllString(k, " $1"), " "+k, -1)
  156. }
  157. // Get the final list of words
  158. words = rex2.FindAllString(str, -1)
  159. return
  160. }
  161. // Removes leading whitespaces
  162. func trim(str string) string {
  163. return strings.Trim(str, " ")
  164. }
  165. // Shortcut to strings.ToUpper()
  166. func upper(str string) string {
  167. return strings.ToUpper(trim(str))
  168. }
  169. // Shortcut to strings.ToLower()
  170. func lower(str string) string {
  171. return strings.ToLower(trim(str))
  172. }
  173. // ToFileName lowercases and underscores a go type name
  174. func ToFileName(name string) string {
  175. var out []string
  176. for _, w := range split(name) {
  177. out = append(out, lower(w))
  178. }
  179. return strings.Join(out, "_")
  180. }
  181. // ToCommandName lowercases and underscores a go type name
  182. func ToCommandName(name string) string {
  183. var out []string
  184. for _, w := range split(name) {
  185. out = append(out, lower(w))
  186. }
  187. return strings.Join(out, "-")
  188. }
  189. // ToHumanNameLower represents a code name as a human series of words
  190. func ToHumanNameLower(name string) string {
  191. var out []string
  192. for _, w := range split(name) {
  193. if !commonInitialisms[upper(w)] {
  194. out = append(out, lower(w))
  195. } else {
  196. out = append(out, w)
  197. }
  198. }
  199. return strings.Join(out, " ")
  200. }
  201. // ToHumanNameTitle represents a code name as a human series of words with the first letters titleized
  202. func ToHumanNameTitle(name string) string {
  203. var out []string
  204. for _, w := range split(name) {
  205. uw := upper(w)
  206. if !commonInitialisms[uw] {
  207. out = append(out, upper(w[:1])+lower(w[1:]))
  208. } else {
  209. out = append(out, w)
  210. }
  211. }
  212. return strings.Join(out, " ")
  213. }
  214. // ToJSONName camelcases a name which can be underscored or pascal cased
  215. func ToJSONName(name string) string {
  216. var out []string
  217. for i, w := range split(name) {
  218. if i == 0 {
  219. out = append(out, lower(w))
  220. continue
  221. }
  222. out = append(out, upper(w[:1])+lower(w[1:]))
  223. }
  224. return strings.Join(out, "")
  225. }
  226. // ToVarName camelcases a name which can be underscored or pascal cased
  227. func ToVarName(name string) string {
  228. res := ToGoName(name)
  229. if _, ok := commonInitialisms[res]; ok {
  230. return lower(res)
  231. }
  232. if len(res) <= 1 {
  233. return lower(res)
  234. }
  235. return lower(res[:1]) + res[1:]
  236. }
  237. // ToGoName translates a swagger name which can be underscored or camel cased to a name that golint likes
  238. func ToGoName(name string) string {
  239. var out []string
  240. for _, w := range split(name) {
  241. uw := upper(w)
  242. mod := int(math.Min(float64(len(uw)), 2))
  243. if !commonInitialisms[uw] && !commonInitialisms[uw[:len(uw)-mod]] {
  244. uw = upper(w[:1]) + lower(w[1:])
  245. }
  246. out = append(out, uw)
  247. }
  248. result := strings.Join(out, "")
  249. if len(result) > 0 {
  250. ud := upper(result[:1])
  251. ru := []rune(ud)
  252. if unicode.IsUpper(ru[0]) {
  253. result = ud + result[1:]
  254. } else {
  255. result = "X" + ud + result[1:]
  256. }
  257. }
  258. return result
  259. }
  260. // ContainsStringsCI searches a slice of strings for a case-insensitive match
  261. func ContainsStringsCI(coll []string, item string) bool {
  262. for _, a := range coll {
  263. if strings.EqualFold(a, item) {
  264. return true
  265. }
  266. }
  267. return false
  268. }
  269. type zeroable interface {
  270. IsZero() bool
  271. }
  272. // IsZero returns true when the value passed into the function is a zero value.
  273. // This allows for safer checking of interface values.
  274. func IsZero(data interface{}) bool {
  275. // check for things that have an IsZero method instead
  276. if vv, ok := data.(zeroable); ok {
  277. return vv.IsZero()
  278. }
  279. // continue with slightly more complex reflection
  280. v := reflect.ValueOf(data)
  281. switch v.Kind() {
  282. case reflect.String:
  283. return v.Len() == 0
  284. case reflect.Bool:
  285. return !v.Bool()
  286. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  287. return v.Int() == 0
  288. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  289. return v.Uint() == 0
  290. case reflect.Float32, reflect.Float64:
  291. return v.Float() == 0
  292. case reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
  293. return v.IsNil()
  294. case reflect.Struct, reflect.Array:
  295. return reflect.DeepEqual(data, reflect.Zero(v.Type()).Interface())
  296. case reflect.Invalid:
  297. return true
  298. }
  299. return false
  300. }
  301. // CommandLineOptionsGroup represents a group of user-defined command line options
  302. type CommandLineOptionsGroup struct {
  303. ShortDescription string
  304. LongDescription string
  305. Options interface{}
  306. }