-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecode.go
More file actions
167 lines (150 loc) · 5.04 KB
/
decode.go
File metadata and controls
167 lines (150 loc) · 5.04 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* Copyright 2025 The Go-Spring Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package formutil
import (
"encoding/base64"
"strconv"
"github.com/go-spring/stdlib/errutil"
"github.com/go-spring/stdlib/jsonflow"
"github.com/go-spring/stdlib/mathutil"
)
// DecodeBool decodes a boolean value from form values.
func DecodeBool(key string, values []string) (bool, error) {
if len(values) > 1 {
return false, errutil.Explain(nil, "too many values for form field %s", key)
}
return strconv.ParseBool(values[0])
}
// DecodeBoolPtr decodes a boolean value and returns a pointer to it.
func DecodeBoolPtr(key string, values []string) (*bool, error) {
b, err := DecodeBool(key, values)
if err != nil {
return nil, err
}
return new(b), nil
}
// DecodeInt decodes a signed integer value from form values.
func DecodeInt[T ~int64 | ~int32 | ~int16 | ~int8 | ~int](key string, values []string) (T, error) {
if len(values) > 1 {
return 0, errutil.Explain(nil, "too many values for form field %s", key)
}
i, err := strconv.ParseInt(values[0], 10, 64)
if err != nil {
return 0, err
}
if mathutil.OverflowInt[T](i) {
return 0, errutil.Explain(nil, "overflow for form field %s", key)
}
return T(i), nil
}
// DecodeIntPtr decodes a signed integer value and returns a pointer to it.
func DecodeIntPtr[T ~int64 | ~int32 | ~int16 | ~int8 | ~int](key string, values []string) (*T, error) {
i, err := DecodeInt[T](key, values)
if err != nil {
return nil, err
}
return new(i), nil
}
// DecodeUint decodes an unsigned integer value from form values.
func DecodeUint[T ~uint64 | ~uint32 | ~uint16 | ~uint8 | ~uint](key string, values []string) (T, error) {
if len(values) > 1 {
return 0, errutil.Explain(nil, "too many values for form field %s", key)
}
u, err := strconv.ParseUint(values[0], 10, 64)
if err != nil {
return 0, err
}
if mathutil.OverflowUint[T](u) {
return 0, errutil.Explain(nil, "overflow for form field %s", key)
}
return T(u), nil
}
// DecodeUintPtr decodes an unsigned integer value and returns a pointer to it.
func DecodeUintPtr[T ~uint64 | ~uint32 | ~uint16 | ~uint8 | ~uint](key string, values []string) (*T, error) {
u, err := DecodeUint[T](key, values)
if err != nil {
return nil, err
}
return new(u), nil
}
// DecodeFloat decodes a floating-point value from form values.
func DecodeFloat[T ~float64 | ~float32](key string, values []string) (T, error) {
if len(values) > 1 {
return 0, errutil.Explain(nil, "too many values for form field %s", key)
}
f, err := strconv.ParseFloat(values[0], 64)
if err != nil {
return 0, err
}
if mathutil.OverflowFloat[T](f) {
return 0, errutil.Explain(nil, "overflow for form field %s", key)
}
return T(f), nil
}
// DecodeFloatPtr decodes a floating-point value and returns a pointer to it.
func DecodeFloatPtr[T ~float64 | ~float32](key string, values []string) (*T, error) {
f, err := DecodeFloat[T](key, values)
if err != nil {
return nil, err
}
return new(f), nil
}
// DecodeString decodes a string value from form values.
func DecodeString(key string, values []string) (string, error) {
if len(values) > 1 {
return "", errutil.Explain(nil, "too many values for form field %s", key)
}
return values[0], nil
}
// DecodeStringPtr decodes a string value and returns a pointer to it.
func DecodeStringPtr(key string, values []string) (*string, error) {
if len(values) > 1 {
return nil, errutil.Explain(nil, "too many values for form field %s", key)
}
return new(values[0]), nil
}
// DecodeBytes decodes a base64-encoded string into a byte slice.
func DecodeBytes(key string, values []string) ([]byte, error) {
if len(values) > 1 {
return nil, errutil.Explain(nil, "too many values for form field %s", key)
}
return base64.StdEncoding.DecodeString(values[0])
}
// DecodeJSON decodes a JSON-encoded value into the target type.
func DecodeJSON[T any](key string, values []string) (T, error) {
var v T
if len(values) > 1 {
return v, errutil.Explain(nil, "too many values for form field %s", key)
}
if err := jsonflow.Unmarshal([]byte(values[0]), &v); err != nil {
return v, err
}
return v, nil
}
// Decoder defines a generic decoder function for a single form value.
type Decoder[T any] func(key string, values []string) (T, error)
// DecodeList decodes multiple form values into a slice using the provided decoder.
func DecodeList[T any](key string, values []string, fn Decoder[T]) ([]T, error) {
arr := make([]T, 0, len(values))
for i := range len(values) {
v, err := fn(key, values[i:i+1])
if err != nil {
return nil, err
}
arr = append(arr, v)
}
return arr, nil
}