本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

386 line
9.1KB

  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. "reflect"
  17. "strings"
  18. "unicode"
  19. )
  20. // commonInitialisms are common acronyms that are kept as whole uppercased words.
  21. var commonInitialisms *indexOfInitialisms
  22. // initialisms is a slice of sorted initialisms
  23. var initialisms []string
  24. var isInitialism func(string) bool
  25. // GoNamePrefixFunc sets an optional rule to prefix go names
  26. // which do not start with a letter.
  27. //
  28. // e.g. to help converting "123" into "{prefix}123"
  29. //
  30. // The default is to prefix with "X"
  31. var GoNamePrefixFunc func(string) string
  32. func init() {
  33. // Taken from https://github.com/golang/lint/blob/3390df4df2787994aea98de825b964ac7944b817/lint.go#L732-L769
  34. var configuredInitialisms = map[string]bool{
  35. "ACL": true,
  36. "API": true,
  37. "ASCII": true,
  38. "CPU": true,
  39. "CSS": true,
  40. "DNS": true,
  41. "EOF": true,
  42. "GUID": true,
  43. "HTML": true,
  44. "HTTPS": true,
  45. "HTTP": true,
  46. "ID": true,
  47. "IP": true,
  48. "IPv4": true,
  49. "IPv6": true,
  50. "JSON": true,
  51. "LHS": true,
  52. "OAI": true,
  53. "QPS": true,
  54. "RAM": true,
  55. "RHS": true,
  56. "RPC": true,
  57. "SLA": true,
  58. "SMTP": true,
  59. "SQL": true,
  60. "SSH": true,
  61. "TCP": true,
  62. "TLS": true,
  63. "TTL": true,
  64. "UDP": true,
  65. "UI": true,
  66. "UID": true,
  67. "UUID": true,
  68. "URI": true,
  69. "URL": true,
  70. "UTF8": true,
  71. "VM": true,
  72. "XML": true,
  73. "XMPP": true,
  74. "XSRF": true,
  75. "XSS": true,
  76. }
  77. // a thread-safe index of initialisms
  78. commonInitialisms = newIndexOfInitialisms().load(configuredInitialisms)
  79. initialisms = commonInitialisms.sorted()
  80. // a test function
  81. isInitialism = commonInitialisms.isInitialism
  82. }
  83. const (
  84. //collectionFormatComma = "csv"
  85. collectionFormatSpace = "ssv"
  86. collectionFormatTab = "tsv"
  87. collectionFormatPipe = "pipes"
  88. collectionFormatMulti = "multi"
  89. )
  90. // JoinByFormat joins a string array by a known format (e.g. swagger's collectionFormat attribute):
  91. // ssv: space separated value
  92. // tsv: tab separated value
  93. // pipes: pipe (|) separated value
  94. // csv: comma separated value (default)
  95. func JoinByFormat(data []string, format string) []string {
  96. if len(data) == 0 {
  97. return data
  98. }
  99. var sep string
  100. switch format {
  101. case collectionFormatSpace:
  102. sep = " "
  103. case collectionFormatTab:
  104. sep = "\t"
  105. case collectionFormatPipe:
  106. sep = "|"
  107. case collectionFormatMulti:
  108. return data
  109. default:
  110. sep = ","
  111. }
  112. return []string{strings.Join(data, sep)}
  113. }
  114. // SplitByFormat splits a string by a known format:
  115. // ssv: space separated value
  116. // tsv: tab separated value
  117. // pipes: pipe (|) separated value
  118. // csv: comma separated value (default)
  119. //
  120. func SplitByFormat(data, format string) []string {
  121. if data == "" {
  122. return nil
  123. }
  124. var sep string
  125. switch format {
  126. case collectionFormatSpace:
  127. sep = " "
  128. case collectionFormatTab:
  129. sep = "\t"
  130. case collectionFormatPipe:
  131. sep = "|"
  132. case collectionFormatMulti:
  133. return nil
  134. default:
  135. sep = ","
  136. }
  137. var result []string
  138. for _, s := range strings.Split(data, sep) {
  139. if ts := strings.TrimSpace(s); ts != "" {
  140. result = append(result, ts)
  141. }
  142. }
  143. return result
  144. }
  145. type byInitialism []string
  146. func (s byInitialism) Len() int {
  147. return len(s)
  148. }
  149. func (s byInitialism) Swap(i, j int) {
  150. s[i], s[j] = s[j], s[i]
  151. }
  152. func (s byInitialism) Less(i, j int) bool {
  153. if len(s[i]) != len(s[j]) {
  154. return len(s[i]) < len(s[j])
  155. }
  156. return strings.Compare(s[i], s[j]) > 0
  157. }
  158. // Removes leading whitespaces
  159. func trim(str string) string {
  160. return strings.Trim(str, " ")
  161. }
  162. // Shortcut to strings.ToUpper()
  163. func upper(str string) string {
  164. return strings.ToUpper(trim(str))
  165. }
  166. // Shortcut to strings.ToLower()
  167. func lower(str string) string {
  168. return strings.ToLower(trim(str))
  169. }
  170. // Camelize an uppercased word
  171. func Camelize(word string) (camelized string) {
  172. for pos, ru := range []rune(word) {
  173. if pos > 0 {
  174. camelized += string(unicode.ToLower(ru))
  175. } else {
  176. camelized += string(unicode.ToUpper(ru))
  177. }
  178. }
  179. return
  180. }
  181. // ToFileName lowercases and underscores a go type name
  182. func ToFileName(name string) string {
  183. in := split(name)
  184. out := make([]string, 0, len(in))
  185. for _, w := range in {
  186. out = append(out, lower(w))
  187. }
  188. return strings.Join(out, "_")
  189. }
  190. // ToCommandName lowercases and underscores a go type name
  191. func ToCommandName(name string) string {
  192. in := split(name)
  193. out := make([]string, 0, len(in))
  194. for _, w := range in {
  195. out = append(out, lower(w))
  196. }
  197. return strings.Join(out, "-")
  198. }
  199. // ToHumanNameLower represents a code name as a human series of words
  200. func ToHumanNameLower(name string) string {
  201. in := newSplitter(withPostSplitInitialismCheck).split(name)
  202. out := make([]string, 0, len(in))
  203. for _, w := range in {
  204. if !w.IsInitialism() {
  205. out = append(out, lower(w.GetOriginal()))
  206. } else {
  207. out = append(out, w.GetOriginal())
  208. }
  209. }
  210. return strings.Join(out, " ")
  211. }
  212. // ToHumanNameTitle represents a code name as a human series of words with the first letters titleized
  213. func ToHumanNameTitle(name string) string {
  214. in := newSplitter(withPostSplitInitialismCheck).split(name)
  215. out := make([]string, 0, len(in))
  216. for _, w := range in {
  217. original := w.GetOriginal()
  218. if !w.IsInitialism() {
  219. out = append(out, Camelize(original))
  220. } else {
  221. out = append(out, original)
  222. }
  223. }
  224. return strings.Join(out, " ")
  225. }
  226. // ToJSONName camelcases a name which can be underscored or pascal cased
  227. func ToJSONName(name string) string {
  228. in := split(name)
  229. out := make([]string, 0, len(in))
  230. for i, w := range in {
  231. if i == 0 {
  232. out = append(out, lower(w))
  233. continue
  234. }
  235. out = append(out, Camelize(w))
  236. }
  237. return strings.Join(out, "")
  238. }
  239. // ToVarName camelcases a name which can be underscored or pascal cased
  240. func ToVarName(name string) string {
  241. res := ToGoName(name)
  242. if isInitialism(res) {
  243. return lower(res)
  244. }
  245. if len(res) <= 1 {
  246. return lower(res)
  247. }
  248. return lower(res[:1]) + res[1:]
  249. }
  250. // ToGoName translates a swagger name which can be underscored or camel cased to a name that golint likes
  251. func ToGoName(name string) string {
  252. lexems := newSplitter(withPostSplitInitialismCheck).split(name)
  253. result := ""
  254. for _, lexem := range lexems {
  255. goName := lexem.GetUnsafeGoName()
  256. // to support old behavior
  257. if lexem.IsInitialism() {
  258. goName = upper(goName)
  259. }
  260. result += goName
  261. }
  262. if len(result) > 0 {
  263. // Only prefix with X when the first character isn't an ascii letter
  264. first := []rune(result)[0]
  265. if !unicode.IsLetter(first) || (first > unicode.MaxASCII && !unicode.IsUpper(first)) {
  266. if GoNamePrefixFunc == nil {
  267. return "X" + result
  268. }
  269. result = GoNamePrefixFunc(name) + result
  270. }
  271. first = []rune(result)[0]
  272. if unicode.IsLetter(first) && !unicode.IsUpper(first) {
  273. result = string(append([]rune{unicode.ToUpper(first)}, []rune(result)[1:]...))
  274. }
  275. }
  276. return result
  277. }
  278. // ContainsStrings searches a slice of strings for a case-sensitive match
  279. func ContainsStrings(coll []string, item string) bool {
  280. for _, a := range coll {
  281. if a == item {
  282. return true
  283. }
  284. }
  285. return false
  286. }
  287. // ContainsStringsCI searches a slice of strings for a case-insensitive match
  288. func ContainsStringsCI(coll []string, item string) bool {
  289. for _, a := range coll {
  290. if strings.EqualFold(a, item) {
  291. return true
  292. }
  293. }
  294. return false
  295. }
  296. type zeroable interface {
  297. IsZero() bool
  298. }
  299. // IsZero returns true when the value passed into the function is a zero value.
  300. // This allows for safer checking of interface values.
  301. func IsZero(data interface{}) bool {
  302. // check for things that have an IsZero method instead
  303. if vv, ok := data.(zeroable); ok {
  304. return vv.IsZero()
  305. }
  306. // continue with slightly more complex reflection
  307. v := reflect.ValueOf(data)
  308. switch v.Kind() {
  309. case reflect.String:
  310. return v.Len() == 0
  311. case reflect.Bool:
  312. return !v.Bool()
  313. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  314. return v.Int() == 0
  315. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  316. return v.Uint() == 0
  317. case reflect.Float32, reflect.Float64:
  318. return v.Float() == 0
  319. case reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
  320. return v.IsNil()
  321. case reflect.Struct, reflect.Array:
  322. return reflect.DeepEqual(data, reflect.Zero(v.Type()).Interface())
  323. case reflect.Invalid:
  324. return true
  325. }
  326. return false
  327. }
  328. // AddInitialisms add additional initialisms
  329. func AddInitialisms(words ...string) {
  330. for _, word := range words {
  331. //commonInitialisms[upper(word)] = true
  332. commonInitialisms.add(upper(word))
  333. }
  334. // sort again
  335. initialisms = commonInitialisms.sorted()
  336. }
  337. // CommandLineOptionsGroup represents a group of user-defined command line options
  338. type CommandLineOptionsGroup struct {
  339. ShortDescription string
  340. LongDescription string
  341. Options interface{}
  342. }
上海开阖软件有限公司 沪ICP备12045867号-1