http urls monitor.

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