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.

392 lines
9.2KB

  1. // Copyright 2015 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. "errors"
  7. "html/template"
  8. "strconv"
  9. )
  10. type (
  11. param struct {
  12. Name string
  13. Value string
  14. }
  15. // Params defines params of http request
  16. Params []param
  17. )
  18. var _ Set = &Params{}
  19. // Get returns request form as string
  20. func (p *Params) Get(key string) string {
  21. if len(key) == 0 {
  22. return ""
  23. }
  24. if key[0] != ':' && key[0] != '*' {
  25. key = ":" + key
  26. }
  27. for _, v := range *p {
  28. if v.Name == key {
  29. return v.Value
  30. }
  31. }
  32. return ""
  33. }
  34. // String returns request form as string
  35. func (p *Params) String(key string) (string, error) {
  36. if len(key) == 0 {
  37. return "", errors.New("not exist")
  38. }
  39. if key[0] != ':' && key[0] != '*' {
  40. key = ":" + key
  41. }
  42. for _, v := range *p {
  43. if v.Name == key {
  44. return v.Value, nil
  45. }
  46. }
  47. return "", errors.New("not exist")
  48. }
  49. // Strings returns request form as slice of string
  50. func (p *Params) Strings(key string) ([]string, error) {
  51. if len(key) == 0 {
  52. return nil, errors.New("not exist")
  53. }
  54. if key[0] != ':' && key[0] != '*' {
  55. key = ":" + key
  56. }
  57. var s = make([]string, 0)
  58. for _, v := range *p {
  59. if v.Name == key {
  60. s = append(s, v.Value)
  61. }
  62. }
  63. if len(s) > 0 {
  64. return s, nil
  65. }
  66. return nil, errors.New("not exist")
  67. }
  68. // Escape returns request form as escaped string
  69. func (p *Params) Escape(key string) (string, error) {
  70. if len(key) == 0 {
  71. return "", errors.New("not exist")
  72. }
  73. if key[0] != ':' && key[0] != '*' {
  74. key = ":" + key
  75. }
  76. for _, v := range *p {
  77. if v.Name == key {
  78. return template.HTMLEscapeString(v.Value), nil
  79. }
  80. }
  81. return "", errors.New("not exist")
  82. }
  83. // Int returns request form as int
  84. func (p *Params) Int(key string) (int, error) {
  85. return strconv.Atoi(p.Get(key))
  86. }
  87. // Int32 returns request form as int32
  88. func (p *Params) Int32(key string) (int32, error) {
  89. v, err := strconv.ParseInt(p.Get(key), 10, 32)
  90. return int32(v), err
  91. }
  92. // Int64 returns request form as int64
  93. func (p *Params) Int64(key string) (int64, error) {
  94. return strconv.ParseInt(p.Get(key), 10, 64)
  95. }
  96. // Uint returns request form as uint
  97. func (p *Params) Uint(key string) (uint, error) {
  98. v, err := strconv.ParseUint(p.Get(key), 10, 64)
  99. return uint(v), err
  100. }
  101. // Uint32 returns request form as uint32
  102. func (p *Params) Uint32(key string) (uint32, error) {
  103. v, err := strconv.ParseUint(p.Get(key), 10, 32)
  104. return uint32(v), err
  105. }
  106. // Uint64 returns request form as uint64
  107. func (p *Params) Uint64(key string) (uint64, error) {
  108. return strconv.ParseUint(p.Get(key), 10, 64)
  109. }
  110. // Bool returns request form as bool
  111. func (p *Params) Bool(key string) (bool, error) {
  112. return strconv.ParseBool(p.Get(key))
  113. }
  114. // Float32 returns request form as float32
  115. func (p *Params) Float32(key string) (float32, error) {
  116. v, err := strconv.ParseFloat(p.Get(key), 32)
  117. return float32(v), err
  118. }
  119. // Float64 returns request form as float64
  120. func (p *Params) Float64(key string) (float64, error) {
  121. return strconv.ParseFloat(p.Get(key), 64)
  122. }
  123. // MustString returns request form as slice of string with default
  124. func (p *Params) MustString(key string, defaults ...string) string {
  125. if len(key) == 0 {
  126. return ""
  127. }
  128. if key[0] != ':' && key[0] != '*' {
  129. key = ":" + key
  130. }
  131. for _, v := range *p {
  132. if v.Name == key {
  133. return v.Value
  134. }
  135. }
  136. if len(defaults) > 0 {
  137. return defaults[0]
  138. }
  139. return ""
  140. }
  141. // MustStrings returns request form as slice of string with default
  142. func (p *Params) MustStrings(key string, defaults ...[]string) []string {
  143. if len(key) == 0 {
  144. return []string{}
  145. }
  146. if key[0] != ':' && key[0] != '*' {
  147. key = ":" + key
  148. }
  149. var s = make([]string, 0)
  150. for _, v := range *p {
  151. if v.Name == key {
  152. s = append(s, v.Value)
  153. }
  154. }
  155. if len(s) > 0 {
  156. return s
  157. }
  158. if len(defaults) > 0 {
  159. return defaults[0]
  160. }
  161. return []string{}
  162. }
  163. // MustEscape returns request form as escaped string with default
  164. func (p *Params) MustEscape(key string, defaults ...string) string {
  165. if len(key) == 0 {
  166. return ""
  167. }
  168. if key[0] != ':' && key[0] != '*' {
  169. key = ":" + key
  170. }
  171. for _, v := range *p {
  172. if v.Name == key {
  173. return template.HTMLEscapeString(v.Value)
  174. }
  175. }
  176. if len(defaults) > 0 {
  177. return defaults[0]
  178. }
  179. return ""
  180. }
  181. // MustInt returns request form as int with default
  182. func (p *Params) MustInt(key string, defaults ...int) int {
  183. v, err := strconv.Atoi(p.Get(key))
  184. if len(defaults) > 0 && err != nil {
  185. return defaults[0]
  186. }
  187. return v
  188. }
  189. // MustInt32 returns request form as int32 with default
  190. func (p *Params) MustInt32(key string, defaults ...int32) int32 {
  191. r, err := strconv.ParseInt(p.Get(key), 10, 32)
  192. if len(defaults) > 0 && err != nil {
  193. return defaults[0]
  194. }
  195. return int32(r)
  196. }
  197. // MustInt64 returns request form as int64 with default
  198. func (p *Params) MustInt64(key string, defaults ...int64) int64 {
  199. r, err := strconv.ParseInt(p.Get(key), 10, 64)
  200. if len(defaults) > 0 && err != nil {
  201. return defaults[0]
  202. }
  203. return r
  204. }
  205. // MustUint returns request form as uint with default
  206. func (p *Params) MustUint(key string, defaults ...uint) uint {
  207. v, err := strconv.ParseUint(p.Get(key), 10, 64)
  208. if len(defaults) > 0 && err != nil {
  209. return defaults[0]
  210. }
  211. return uint(v)
  212. }
  213. // MustUint32 returns request form as uint32 with default
  214. func (p *Params) MustUint32(key string, defaults ...uint32) uint32 {
  215. r, err := strconv.ParseUint(p.Get(key), 10, 32)
  216. if len(defaults) > 0 && err != nil {
  217. return defaults[0]
  218. }
  219. return uint32(r)
  220. }
  221. // MustUint64 returns request form as uint64 with default
  222. func (p *Params) MustUint64(key string, defaults ...uint64) uint64 {
  223. r, err := strconv.ParseUint(p.Get(key), 10, 64)
  224. if len(defaults) > 0 && err != nil {
  225. return defaults[0]
  226. }
  227. return r
  228. }
  229. // MustFloat32 returns request form as float32 with default
  230. func (p *Params) MustFloat32(key string, defaults ...float32) float32 {
  231. r, err := strconv.ParseFloat(p.Get(key), 32)
  232. if len(defaults) > 0 && err != nil {
  233. return defaults[0]
  234. }
  235. return float32(r)
  236. }
  237. // MustFloat64 returns request form as float64 with default
  238. func (p *Params) MustFloat64(key string, defaults ...float64) float64 {
  239. r, err := strconv.ParseFloat(p.Get(key), 64)
  240. if len(defaults) > 0 && err != nil {
  241. return defaults[0]
  242. }
  243. return r
  244. }
  245. // MustBool returns request form as bool with default
  246. func (p *Params) MustBool(key string, defaults ...bool) bool {
  247. r, err := strconv.ParseBool(p.Get(key))
  248. if len(defaults) > 0 && err != nil {
  249. return defaults[0]
  250. }
  251. return r
  252. }
  253. // Param returns request form as string with default
  254. func (ctx *Context) Param(key string, defaults ...string) string {
  255. return ctx.Params().MustString(key, defaults...)
  256. }
  257. // ParamStrings returns request form as slice of string with default
  258. func (ctx *Context) ParamStrings(key string, defaults ...[]string) []string {
  259. return ctx.Params().MustStrings(key, defaults...)
  260. }
  261. // ParamEscape returns request form as escaped string with default
  262. func (ctx *Context) ParamEscape(key string, defaults ...string) string {
  263. return ctx.Params().MustEscape(key, defaults...)
  264. }
  265. // ParamInt returns request form as int with default
  266. func (ctx *Context) ParamInt(key string, defaults ...int) int {
  267. return ctx.Params().MustInt(key, defaults...)
  268. }
  269. // ParamInt32 returns request form as int32 with default
  270. func (ctx *Context) ParamInt32(key string, defaults ...int32) int32 {
  271. return ctx.Params().MustInt32(key, defaults...)
  272. }
  273. // ParamInt64 returns request form as int64 with default
  274. func (ctx *Context) ParamInt64(key string, defaults ...int64) int64 {
  275. return ctx.Params().MustInt64(key, defaults...)
  276. }
  277. // ParamUint returns request form as uint with default
  278. func (ctx *Context) ParamUint(key string, defaults ...uint) uint {
  279. return ctx.Params().MustUint(key, defaults...)
  280. }
  281. // ParamUint32 returns request form as uint32 with default
  282. func (ctx *Context) ParamUint32(key string, defaults ...uint32) uint32 {
  283. return ctx.Params().MustUint32(key, defaults...)
  284. }
  285. // ParamUint64 returns request form as uint64 with default
  286. func (ctx *Context) ParamUint64(key string, defaults ...uint64) uint64 {
  287. return ctx.Params().MustUint64(key, defaults...)
  288. }
  289. // ParamFloat32 returns request form as float32 with default
  290. func (ctx *Context) ParamFloat32(key string, defaults ...float32) float32 {
  291. return ctx.Params().MustFloat32(key, defaults...)
  292. }
  293. // ParamFloat64 returns request form as float64 with default
  294. func (ctx *Context) ParamFloat64(key string, defaults ...float64) float64 {
  295. return ctx.Params().MustFloat64(key, defaults...)
  296. }
  297. // ParamBool returns request form as bool with default
  298. func (ctx *Context) ParamBool(key string, defaults ...bool) bool {
  299. return ctx.Params().MustBool(key, defaults...)
  300. }
  301. // Set sets key/value to params
  302. func (p *Params) Set(key, value string) {
  303. if len(key) == 0 {
  304. return
  305. }
  306. if key[0] != ':' && key[0] != '*' {
  307. key = ":" + key
  308. }
  309. for i, v := range *p {
  310. if v.Name == key {
  311. (*p)[i].Value = value
  312. return
  313. }
  314. }
  315. *p = append(*p, param{key, value})
  316. }
  317. // Paramer defines an interface to get params
  318. type Paramer interface {
  319. SetParams([]param)
  320. }
  321. // SetParams implemented Paramer
  322. func (p *Params) SetParams(params []param) {
  323. *p = params
  324. }
  325. // Param returns params handle to operate param
  326. func Param() HandlerFunc {
  327. return func(ctx *Context) {
  328. if action := ctx.Action(); action != nil {
  329. if p, ok := action.(Paramer); ok {
  330. p.SetParams(*ctx.Params())
  331. }
  332. }
  333. ctx.Next()
  334. }
  335. }