/
path_params.go
91 lines (72 loc) · 2.38 KB
/
path_params.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package vial
import (
"fmt"
"regexp"
"github.com/pkg/errors"
"github.com/google/uuid"
)
var (
wrongPathParamType = "Path parameter is not of type %s"
pathParamDoesNotExist = "Path parameter with key '%s' does not exist"
)
var (
wrongPathParamTypeRegex = regexp.MustCompile(
fmt.Sprintf(wrongPathParamType, `\w+`),
)
pathParamDoesNotExistRegex = regexp.MustCompile(
fmt.Sprintf(pathParamDoesNotExist, `[^']*`),
)
)
// IsWrongPathParamType checks if an error is caused by the type requested not
// being the correct type.
func IsWrongPathParamType(err error) bool {
return wrongPathParamTypeRegex.MatchString(err.Error())
}
// IsPathParamDoesNotExist checks if an error is caused by the param not
// existing.
func IsPathParamDoesNotExist(err error) bool {
return pathParamDoesNotExistRegex.MatchString(err.Error())
}
// PathParams is a convenience wrapper around a map holding coerced path
// values.
type PathParams map[string]interface{}
// String retrieves and coerces a string.
func (self PathParams) String(key string) (string, error) {
if in, ok := self[key]; ok {
if s, ok := in.(string); ok {
return s, nil
}
return "", errors.New(fmt.Sprintf(wrongPathParamType, "string"))
}
return "", errors.New(fmt.Sprintf(pathParamDoesNotExist, key))
}
// Float retrieves and coerces a float.
func (self PathParams) Float(key string) (float64, error) {
if in, ok := self[key]; ok {
if f, ok := in.(float64); ok {
return f, nil
}
return 0, errors.New(fmt.Sprintf(wrongPathParamType, "float"))
}
return 0, errors.New(fmt.Sprintf(pathParamDoesNotExist, key))
}
// Int retrieves and coerces a int.
func (self PathParams) Int(key string) (int, error) {
if in, ok := self[key]; ok {
if i, ok := in.(int); ok {
return i, nil
}
return 0, errors.New(fmt.Sprintf(wrongPathParamType, "int"))
}
return 0, errors.New(fmt.Sprintf(pathParamDoesNotExist, key))
}
// UUID retrieves and coerces a UUID.
func (self PathParams) UUID(key string) (uuid.UUID, error) {
if in, ok := self[key]; ok {
if u, ok := in.(uuid.UUID); ok {
return u, nil
}
return uuid.UUID{}, errors.New(fmt.Sprintf(wrongPathParamType, "UUID"))
}
return uuid.UUID{}, errors.New(fmt.Sprintf(pathParamDoesNotExist, key))
}