Micro & pluggable web framework for Go
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.

353 lines
8.4KB

  1. // Copyright 2019 The Tango Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package tango
  5. import (
  6. "html/template"
  7. "net/http"
  8. "net/url"
  9. "strconv"
  10. )
  11. // Queries a new enhancement of http.Request
  12. type Queries http.Request
  13. var _ Set = &Queries{}
  14. // Values returns http.Request values
  15. func (f *Queries) Values() url.Values {
  16. return (*http.Request)(f).URL.Query()
  17. }
  18. // String returns request form as string
  19. func (f *Queries) String(key string) (string, error) {
  20. if v, ok := f.Values()[key]; ok {
  21. return v[0], nil
  22. }
  23. return "", ErrorKeyIsNotExist{key}
  24. }
  25. // Strings returns request form as strings
  26. func (f *Queries) Strings(key string) ([]string, error) {
  27. if v, ok := f.Values()[key]; ok {
  28. return v, nil
  29. }
  30. return nil, ErrorKeyIsNotExist{key}
  31. }
  32. // Escape returns request form as escaped string
  33. func (f *Queries) Escape(key string) (string, error) {
  34. s, err := f.String(key)
  35. if err != nil {
  36. return "", err
  37. }
  38. return template.HTMLEscapeString(s), nil
  39. }
  40. // Int returns request form as int
  41. func (f *Queries) Int(key string) (int, error) {
  42. s, err := f.String(key)
  43. if err != nil {
  44. return 0, err
  45. }
  46. return strconv.Atoi(s)
  47. }
  48. // Int32 returns request form as int32
  49. func (f *Queries) Int32(key string) (int32, error) {
  50. s, err := f.String(key)
  51. if err != nil {
  52. return 0, err
  53. }
  54. v, err := strconv.ParseInt(s, 10, 32)
  55. return int32(v), err
  56. }
  57. // Int64 returns request form as int64
  58. func (f *Queries) Int64(key string) (int64, error) {
  59. s, err := f.String(key)
  60. if err != nil {
  61. return 0, err
  62. }
  63. v, err := strconv.ParseInt(s, 10, 64)
  64. return v, err
  65. }
  66. // Uint returns request form as uint
  67. func (f *Queries) Uint(key string) (uint, error) {
  68. s, err := f.String(key)
  69. if err != nil {
  70. return 0, err
  71. }
  72. v, err := strconv.ParseUint(s, 10, 64)
  73. return uint(v), err
  74. }
  75. // Uint32 returns request form as uint32
  76. func (f *Queries) Uint32(key string) (uint32, error) {
  77. s, err := f.String(key)
  78. if err != nil {
  79. return 0, err
  80. }
  81. v, err := strconv.ParseUint(s, 10, 32)
  82. return uint32(v), err
  83. }
  84. // Uint64 returns request form as uint64
  85. func (f *Queries) Uint64(key string) (uint64, error) {
  86. s, err := f.String(key)
  87. if err != nil {
  88. return 0, err
  89. }
  90. return strconv.ParseUint(s, 10, 64)
  91. }
  92. // Bool returns request form as bool
  93. func (f *Queries) Bool(key string) (bool, error) {
  94. s, err := f.String(key)
  95. if err != nil {
  96. return false, err
  97. }
  98. return strconv.ParseBool(s)
  99. }
  100. // Float32 returns request form as float32
  101. func (f *Queries) Float32(key string) (float32, error) {
  102. s, err := f.String(key)
  103. if err != nil {
  104. return 0, err
  105. }
  106. v, err := strconv.ParseFloat(s, 64)
  107. return float32(v), err
  108. }
  109. // Float64 returns request form as float64
  110. func (f *Queries) Float64(key string) (float64, error) {
  111. s, err := f.String(key)
  112. if err != nil {
  113. return 0, err
  114. }
  115. return strconv.ParseFloat(s, 64)
  116. }
  117. // MustString returns request form as string with default
  118. func (f *Queries) MustString(key string, defaults ...string) string {
  119. if v, ok := f.Values()[key]; ok {
  120. return v[0]
  121. }
  122. if len(defaults) > 0 {
  123. return defaults[0]
  124. }
  125. return ""
  126. }
  127. // MustStrings returns request form as strings with default
  128. func (f *Queries) MustStrings(key string, defaults ...[]string) []string {
  129. if v, ok := f.Values()[key]; ok {
  130. return v
  131. }
  132. if len(defaults) > 0 {
  133. return defaults[0]
  134. }
  135. return []string{}
  136. }
  137. // MustEscape returns request form as escaped string with default
  138. func (f *Queries) MustEscape(key string, defaults ...string) string {
  139. if v, ok := f.Values()[key]; ok {
  140. return template.HTMLEscapeString(v[0])
  141. }
  142. if len(defaults) > 0 {
  143. return defaults[0]
  144. }
  145. return ""
  146. }
  147. // MustInt returns request form as int with default
  148. func (f *Queries) MustInt(key string, defaults ...int) int {
  149. if v, ok := f.Values()[key]; ok {
  150. i, err := strconv.Atoi(v[0])
  151. if err == nil {
  152. return i
  153. }
  154. }
  155. if len(defaults) > 0 {
  156. return defaults[0]
  157. }
  158. return 0
  159. }
  160. // MustInt32 returns request form as int32 with default
  161. func (f *Queries) MustInt32(key string, defaults ...int32) int32 {
  162. if v, ok := f.Values()[key]; ok {
  163. i, err := strconv.ParseInt(v[0], 10, 32)
  164. if err == nil {
  165. return int32(i)
  166. }
  167. }
  168. if len(defaults) > 0 {
  169. return defaults[0]
  170. }
  171. return 0
  172. }
  173. // MustInt64 returns request form as int64 with default
  174. func (f *Queries) MustInt64(key string, defaults ...int64) int64 {
  175. if v, ok := f.Values()[key]; ok {
  176. i, err := strconv.ParseInt(v[0], 10, 64)
  177. if err == nil {
  178. return i
  179. }
  180. }
  181. if len(defaults) > 0 {
  182. return defaults[0]
  183. }
  184. return 0
  185. }
  186. // MustUint returns request form as uint with default
  187. func (f *Queries) MustUint(key string, defaults ...uint) uint {
  188. if v, ok := f.Values()[key]; ok {
  189. i, err := strconv.ParseUint(v[0], 10, 64)
  190. if err == nil {
  191. return uint(i)
  192. }
  193. }
  194. if len(defaults) > 0 {
  195. return defaults[0]
  196. }
  197. return 0
  198. }
  199. // MustUint32 returns request form as uint32 with default
  200. func (f *Queries) MustUint32(key string, defaults ...uint32) uint32 {
  201. if v, ok := f.Values()[key]; ok {
  202. i, err := strconv.ParseUint(v[0], 10, 32)
  203. if err == nil {
  204. return uint32(i)
  205. }
  206. }
  207. if len(defaults) > 0 {
  208. return defaults[0]
  209. }
  210. return 0
  211. }
  212. // MustUint64 returns request form as uint64 with default
  213. func (f *Queries) MustUint64(key string, defaults ...uint64) uint64 {
  214. if v, ok := f.Values()[key]; ok {
  215. i, err := strconv.ParseUint(v[0], 10, 64)
  216. if err == nil {
  217. return i
  218. }
  219. }
  220. if len(defaults) > 0 {
  221. return defaults[0]
  222. }
  223. return 0
  224. }
  225. // MustFloat32 returns request form as float32 with default
  226. func (f *Queries) MustFloat32(key string, defaults ...float32) float32 {
  227. if v, ok := f.Values()[key]; ok {
  228. i, err := strconv.ParseFloat(v[0], 32)
  229. if err == nil {
  230. return float32(i)
  231. }
  232. }
  233. if len(defaults) > 0 {
  234. return defaults[0]
  235. }
  236. return 0
  237. }
  238. // MustFloat64 returns request form as float64 with default
  239. func (f *Queries) MustFloat64(key string, defaults ...float64) float64 {
  240. if v, ok := f.Values()[key]; ok {
  241. i, err := strconv.ParseFloat(v[0], 64)
  242. if err == nil {
  243. return i
  244. }
  245. }
  246. if len(defaults) > 0 {
  247. return defaults[0]
  248. }
  249. return 0
  250. }
  251. // MustBool returns request form as bool with default
  252. func (f *Queries) MustBool(key string, defaults ...bool) bool {
  253. if v, ok := f.Values()[key]; ok {
  254. i, err := strconv.ParseBool(v[0])
  255. if err == nil {
  256. return i
  257. }
  258. }
  259. if len(defaults) > 0 {
  260. return defaults[0]
  261. }
  262. return false
  263. }
  264. // Query returns request form as string with default
  265. func (ctx *Context) Query(key string, defaults ...string) string {
  266. return (*Queries)(ctx.req).MustString(key, defaults...)
  267. }
  268. // QueryStrings returns request form as strings with default
  269. func (ctx *Context) QueryStrings(key string, defaults ...[]string) []string {
  270. return (*Queries)(ctx.req).MustStrings(key, defaults...)
  271. }
  272. // QueryEscape returns request form as escaped string with default
  273. func (ctx *Context) QueryEscape(key string, defaults ...string) string {
  274. return (*Queries)(ctx.req).MustEscape(key, defaults...)
  275. }
  276. // QueryInt returns request form as int with default
  277. func (ctx *Context) QueryInt(key string, defaults ...int) int {
  278. return (*Queries)(ctx.req).MustInt(key, defaults...)
  279. }
  280. // QueryInt32 returns request form as int32 with default
  281. func (ctx *Context) QueryInt32(key string, defaults ...int32) int32 {
  282. return (*Queries)(ctx.req).MustInt32(key, defaults...)
  283. }
  284. // QueryInt64 returns request form as int64 with default
  285. func (ctx *Context) QueryInt64(key string, defaults ...int64) int64 {
  286. return (*Queries)(ctx.req).MustInt64(key, defaults...)
  287. }
  288. // QueryUint returns request form as uint with default
  289. func (ctx *Context) QueryUint(key string, defaults ...uint) uint {
  290. return (*Queries)(ctx.req).MustUint(key, defaults...)
  291. }
  292. // QueryUint32 returns request form as uint32 with default
  293. func (ctx *Context) QueryUint32(key string, defaults ...uint32) uint32 {
  294. return (*Queries)(ctx.req).MustUint32(key, defaults...)
  295. }
  296. // QueryUint64 returns request form as uint64 with default
  297. func (ctx *Context) QueryUint64(key string, defaults ...uint64) uint64 {
  298. return (*Queries)(ctx.req).MustUint64(key, defaults...)
  299. }
  300. // QueryFloat32 returns request form as float32 with default
  301. func (ctx *Context) QueryFloat32(key string, defaults ...float32) float32 {
  302. return (*Queries)(ctx.req).MustFloat32(key, defaults...)
  303. }
  304. // FormFloat64 returns request form as float64 with default
  305. func (ctx *Context) QueryFloat64(key string, defaults ...float64) float64 {
  306. return (*Queries)(ctx.req).MustFloat64(key, defaults...)
  307. }
  308. // FormBool returns request form as bool with default
  309. func (ctx *Context) QueryBool(key string, defaults ...bool) bool {
  310. return (*Queries)(ctx.req).MustBool(key, defaults...)
  311. }