|
| 1 | +/* |
| 2 | + * Copyright 2025 CloudWeGo Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package binding |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "reflect" |
| 22 | + "strconv" |
| 23 | +) |
| 24 | + |
| 25 | +type baseDecoder struct { |
| 26 | + *fieldInfo |
| 27 | + |
| 28 | + decodeValue func(rv reflect.Value, s string) error |
| 29 | +} |
| 30 | + |
| 31 | +func newBaseDecoder(fi *fieldInfo) fieldDecoder { |
| 32 | + decoder := &baseDecoder{fieldInfo: fi} |
| 33 | + fn := getBaseDecodeByKind(fi.fieldType.Kind()) |
| 34 | + if fn == nil { |
| 35 | + // Use method that has access to jsonUnmarshal |
| 36 | + fn = decoder.decodeJSONValue |
| 37 | + } |
| 38 | + decoder.decodeValue = fn |
| 39 | + return decoder |
| 40 | +} |
| 41 | + |
| 42 | +func (d *baseDecoder) Decode(req RequestContext, rv reflect.Value) (bool, error) { |
| 43 | + v, ok, err := d.FetchBindValue(req) |
| 44 | + if !ok || err != nil { |
| 45 | + return false, err |
| 46 | + } |
| 47 | + |
| 48 | + f := d.FieldSetter(rv) |
| 49 | + rv = f.Value() |
| 50 | + |
| 51 | + s := b2s(v) |
| 52 | + if err := d.decodeValue(rv, s); err != nil { |
| 53 | + f.Reset() |
| 54 | + return false, fmt.Errorf("unable to decode '%s' as %s: %w", s, d.fieldType.String(), err) |
| 55 | + } |
| 56 | + return true, nil |
| 57 | +} |
| 58 | + |
| 59 | +// use slice for better performance, |
| 60 | +var type2decoder = [...]func(rv reflect.Value, s string) error{ |
| 61 | + reflect.Bool: decodeBool, |
| 62 | + reflect.Uint: decodeUint, |
| 63 | + reflect.Uint8: decodeUint8, |
| 64 | + reflect.Uint16: decodeUint16, |
| 65 | + reflect.Uint32: decodeUint32, |
| 66 | + reflect.Uint64: decodeUint64, |
| 67 | + reflect.Int: decodeInt, |
| 68 | + reflect.Int8: decodeInt8, |
| 69 | + reflect.Int16: decodeInt16, |
| 70 | + reflect.Int32: decodeInt32, |
| 71 | + reflect.Int64: decodeInt64, |
| 72 | + reflect.String: decodeString, |
| 73 | + reflect.Float32: decodeFloat32, |
| 74 | + reflect.Float64: decodeFloat64, |
| 75 | +} |
| 76 | + |
| 77 | +func getBaseDecodeByKind(k reflect.Kind) (ret func(rv reflect.Value, s string) error) { |
| 78 | + if int(k) >= len(type2decoder) { |
| 79 | + return nil |
| 80 | + } |
| 81 | + return type2decoder[k] |
| 82 | +} |
| 83 | + |
| 84 | +// decodeJSONValue is a method on baseDecoder that uses the configured JSON unmarshal function |
| 85 | +func (d *baseDecoder) decodeJSONValue(rv reflect.Value, s string) error { |
| 86 | + return d.jsonUnmarshal(s2b(s), rv.Addr().Interface()) |
| 87 | +} |
| 88 | + |
| 89 | +func decodeBool(rv reflect.Value, s string) error { |
| 90 | + v, err := strconv.ParseBool(s) |
| 91 | + if err == nil { |
| 92 | + *(*bool)(rvUnsafePointer(&rv)) = v |
| 93 | + } |
| 94 | + return err |
| 95 | + |
| 96 | +} |
| 97 | + |
| 98 | +func decodeUint(rv reflect.Value, s string) error { |
| 99 | + v, err := strconv.ParseUint(s, 10, 0) |
| 100 | + if err == nil { |
| 101 | + *(*uint)(rvUnsafePointer(&rv)) = uint(v) |
| 102 | + } |
| 103 | + return err |
| 104 | + |
| 105 | +} |
| 106 | + |
| 107 | +func decodeUint8(rv reflect.Value, s string) error { |
| 108 | + v, err := strconv.ParseUint(s, 10, 8) |
| 109 | + if err == nil { |
| 110 | + *(*uint8)(rvUnsafePointer(&rv)) = uint8(v) |
| 111 | + } |
| 112 | + return err |
| 113 | + |
| 114 | +} |
| 115 | + |
| 116 | +func decodeUint16(rv reflect.Value, s string) error { |
| 117 | + v, err := strconv.ParseUint(s, 10, 16) |
| 118 | + if err == nil { |
| 119 | + *(*uint16)(rvUnsafePointer(&rv)) = uint16(v) |
| 120 | + } |
| 121 | + return err |
| 122 | +} |
| 123 | + |
| 124 | +func decodeUint32(rv reflect.Value, s string) error { |
| 125 | + v, err := strconv.ParseUint(s, 10, 32) |
| 126 | + if err == nil { |
| 127 | + *(*uint32)(rvUnsafePointer(&rv)) = uint32(v) |
| 128 | + } |
| 129 | + return err |
| 130 | + |
| 131 | +} |
| 132 | + |
| 133 | +func decodeUint64(rv reflect.Value, s string) error { |
| 134 | + v, err := strconv.ParseUint(s, 10, 64) |
| 135 | + if err == nil { |
| 136 | + *(*uint64)(rvUnsafePointer(&rv)) = v |
| 137 | + } |
| 138 | + return err |
| 139 | + |
| 140 | +} |
| 141 | + |
| 142 | +func decodeInt(rv reflect.Value, s string) error { |
| 143 | + v, err := strconv.Atoi(s) |
| 144 | + if err == nil { |
| 145 | + *(*int)(rvUnsafePointer(&rv)) = v |
| 146 | + } |
| 147 | + return err |
| 148 | + |
| 149 | +} |
| 150 | + |
| 151 | +func decodeInt8(rv reflect.Value, s string) error { |
| 152 | + v, err := strconv.ParseInt(s, 10, 8) |
| 153 | + if err == nil { |
| 154 | + *(*int8)(rvUnsafePointer(&rv)) = int8(v) |
| 155 | + } |
| 156 | + return err |
| 157 | + |
| 158 | +} |
| 159 | + |
| 160 | +func decodeInt16(rv reflect.Value, s string) error { |
| 161 | + v, err := strconv.ParseInt(s, 10, 16) |
| 162 | + if err == nil { |
| 163 | + *(*int16)(rvUnsafePointer(&rv)) = int16(v) |
| 164 | + } |
| 165 | + return err |
| 166 | +} |
| 167 | + |
| 168 | +func decodeInt32(rv reflect.Value, s string) error { |
| 169 | + v, err := strconv.ParseInt(s, 10, 32) |
| 170 | + if err == nil { |
| 171 | + *(*int32)(rvUnsafePointer(&rv)) = int32(v) |
| 172 | + } |
| 173 | + return err |
| 174 | +} |
| 175 | + |
| 176 | +func decodeInt64(rv reflect.Value, s string) error { |
| 177 | + v, err := strconv.ParseInt(s, 10, 64) |
| 178 | + if err == nil { |
| 179 | + *(*int64)(rvUnsafePointer(&rv)) = v |
| 180 | + } |
| 181 | + return err |
| 182 | +} |
| 183 | + |
| 184 | +func decodeString(rv reflect.Value, s string) error { |
| 185 | + *(*string)(rvUnsafePointer(&rv)) = s |
| 186 | + return nil |
| 187 | +} |
| 188 | + |
| 189 | +func decodeFloat32(rv reflect.Value, s string) error { |
| 190 | + v, err := strconv.ParseFloat(s, 32) |
| 191 | + if err == nil { |
| 192 | + *(*float32)(rvUnsafePointer(&rv)) = float32(v) |
| 193 | + } |
| 194 | + return err |
| 195 | +} |
| 196 | + |
| 197 | +func decodeFloat64(rv reflect.Value, s string) error { |
| 198 | + v, err := strconv.ParseFloat(s, 64) |
| 199 | + if err == nil { |
| 200 | + *(*float64)(rvUnsafePointer(&rv)) = v |
| 201 | + } |
| 202 | + return err |
| 203 | +} |
0 commit comments