/
params.go
109 lines (98 loc) · 3.87 KB
/
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package ginutil
import (
"fmt"
"math/bits"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"github.com/iver-wharf/wharf-core/v2/pkg/problem"
)
// RequireParamString tries to read the named path parameter from the request
// and checks that it's not empty.
//
// If it fails, it will write out a problem response using
// WriteProblem with the status code 400 (Bad Request).
func RequireParamString(c *gin.Context, paramName string) (string, bool) {
return requireString(c, paramName, c.Param(paramName))
}
// RequireQueryString tries to read the named query parameter from the request
// and checks that it's not empty.
//
// If it fails, it will write out a problem response using
// WriteProblem with the status code 400 (Bad Request).
func RequireQueryString(c *gin.Context, queryName string) (string, bool) {
return requireString(c, queryName, c.Query(queryName))
}
func requireString(c *gin.Context, paramName string, paramValue string) (string, bool) {
if paramValue == "" {
WriteProblem(c, problem.Response{
Type: "/prob/api/missing-param-string",
Title: "Missing string value.",
Status: http.StatusBadRequest,
Detail: fmt.Sprintf("A string value (text) was expected on parameter %q, but it was either omitted or empty.", paramName),
Instance: fmt.Sprintf("%s#%s", c.Request.RequestURI, paramName),
})
return "", false
}
return paramValue, true
}
// ParseParamUint tries to read the named path parameter from the request and
// parse it to an uint.
//
// If it fails, it will write out a problem response using
// WriteProblemError with the status code 400 (Bad Request).
func ParseParamUint(c *gin.Context, paramName string) (uint, bool) {
return parseUint(c, paramName, c.Param(paramName))
}
// ParseParamInt tries to read the named path parameter from the request and
// parse it to an int.
//
// If it fails, it will write out a problem response using
// WriteProblemError with the status code 400 (Bad Request).
func ParseParamInt(c *gin.Context, paramName string) (int, bool) {
return parseInt(c, paramName, c.Param(paramName))
}
// ParseQueryUint tries to read the named query parameter from the request and
// parse it to an uint.
//
// If it fails, it will write out a problem response using
// WriteProblemError with the status code 400 (Bad Request).
func ParseQueryUint(c *gin.Context, queryName string) (uint, bool) {
return parseUint(c, queryName, c.Query(queryName))
}
// ParseQueryInt tries to read the named query parameter from the request and
// parse it to an int.
//
// If it fails, it will write out a problem response using
// WriteProblemError with the status code 400 (Bad Request).
func ParseQueryInt(c *gin.Context, queryName string) (int, bool) {
return parseInt(c, queryName, c.Query(queryName))
}
func parseUint(c *gin.Context, paramName, paramValue string) (uint, bool) {
value, err := strconv.ParseUint(paramValue, 10, bits.UintSize)
if err != nil {
WriteProblemError(c, err, problem.Response{
Type: "/prob/api/invalid-param-uint",
Title: "Invalid positive integer value.",
Status: http.StatusBadRequest,
Detail: fmt.Sprintf("Failed to interpret parameter %q with value %q as an unsigned (positive) integer.", paramName, paramValue),
Instance: fmt.Sprintf("%s#%s", c.Request.RequestURI, paramName),
})
return 0, false
}
return uint(value), true
}
func parseInt(c *gin.Context, paramName, paramValue string) (int, bool) {
value, err := strconv.ParseInt(paramValue, 10, bits.UintSize)
if err != nil {
WriteProblemError(c, err, problem.Response{
Type: "/prob/api/invalid-param-int",
Title: "Invalid integer value.",
Status: http.StatusBadRequest,
Detail: fmt.Sprintf("Failed to interpret parameter %q with value %q as a signed (positive or negative) integer.", paramName, paramValue),
Instance: fmt.Sprintf("%s#%s", c.Request.RequestURI, paramValue),
})
return 0, false
}
return int(value), true
}