|
| 1 | +/* |
| 2 | + * Copyright 2024 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 decoder |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding" |
| 21 | + "errors" |
| 22 | + "reflect" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/cloudwego/hertz/pkg/common/test/assert" |
| 26 | + "github.com/cloudwego/hertz/pkg/protocol" |
| 27 | + "github.com/cloudwego/hertz/pkg/route/param" |
| 28 | +) |
| 29 | + |
| 30 | +type testTextUnmarshaler struct { |
| 31 | + Value string |
| 32 | +} |
| 33 | + |
| 34 | +func (t *testTextUnmarshaler) UnmarshalText(text []byte) error { |
| 35 | + t.Value = string(text) |
| 36 | + return nil |
| 37 | +} |
| 38 | + |
| 39 | +var _ encoding.TextUnmarshaler = (*testTextUnmarshaler)(nil) |
| 40 | + |
| 41 | +func TestStringToValue(t *testing.T) { |
| 42 | + tests := []struct { |
| 43 | + name string |
| 44 | + elemType reflect.Type |
| 45 | + text string |
| 46 | + config *DecodeConfig |
| 47 | + expectValue interface{} |
| 48 | + expectError bool |
| 49 | + }{ |
| 50 | + { |
| 51 | + name: "string type", |
| 52 | + elemType: reflect.TypeOf(""), |
| 53 | + text: "test string", |
| 54 | + expectValue: "test string", |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "int type", |
| 58 | + elemType: reflect.TypeOf(0), |
| 59 | + text: "42", |
| 60 | + expectValue: 42, |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "bool type", |
| 64 | + elemType: reflect.TypeOf(false), |
| 65 | + text: "true", |
| 66 | + expectValue: true, |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "float type", |
| 70 | + elemType: reflect.TypeOf(0.0), |
| 71 | + text: "3.14", |
| 72 | + expectValue: 3.14, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "text unmarshaler", |
| 76 | + elemType: reflect.TypeOf(testTextUnmarshaler{}), |
| 77 | + text: "custom text", |
| 78 | + expectValue: testTextUnmarshaler{Value: "custom text"}, |
| 79 | + }, |
| 80 | + { |
| 81 | + name: "invalid int", |
| 82 | + elemType: reflect.TypeOf(0), |
| 83 | + text: "not an int", |
| 84 | + expectError: true, |
| 85 | + }, |
| 86 | + { |
| 87 | + name: "struct type", |
| 88 | + elemType: reflect.TypeOf(struct{ Name string }{}), |
| 89 | + text: `{"Name":"test"}`, |
| 90 | + expectValue: struct{ Name string }{Name: "test"}, |
| 91 | + }, |
| 92 | + { |
| 93 | + name: "struct type err", |
| 94 | + elemType: reflect.TypeOf(struct{ Name string }{}), |
| 95 | + text: `{"Name":1}`, |
| 96 | + expectError: true, |
| 97 | + }, |
| 98 | + { |
| 99 | + name: "list type", |
| 100 | + elemType: reflect.TypeOf([]int{}), |
| 101 | + expectValue: *new([]int), |
| 102 | + }, |
| 103 | + { |
| 104 | + name: "map type", |
| 105 | + elemType: reflect.TypeOf(map[string]interface{}{}), |
| 106 | + text: `{"key":"value"}`, |
| 107 | + expectValue: map[string]interface{}{"key": "value"}, |
| 108 | + }, |
| 109 | + { |
| 110 | + name: "unsupported type", |
| 111 | + elemType: reflect.TypeOf(complex64(0)), |
| 112 | + expectError: true, |
| 113 | + }, |
| 114 | + { |
| 115 | + name: "custom type unmarshal func", |
| 116 | + elemType: reflect.TypeOf(testTextUnmarshaler{}), |
| 117 | + text: "custom func", |
| 118 | + config: &DecodeConfig{ |
| 119 | + TypeUnmarshalFuncs: map[reflect.Type]CustomizeDecodeFunc{ |
| 120 | + reflect.TypeOf(testTextUnmarshaler{}): func(req *protocol.Request, params param.Params, text string) (reflect.Value, error) { |
| 121 | + return reflect.ValueOf(testTextUnmarshaler{Value: "from custom func"}), nil |
| 122 | + }, |
| 123 | + }, |
| 124 | + }, |
| 125 | + expectValue: testTextUnmarshaler{Value: "from custom func"}, |
| 126 | + }, |
| 127 | + { |
| 128 | + name: "custom type unmarshal func err", |
| 129 | + elemType: reflect.TypeOf(testTextUnmarshaler{}), |
| 130 | + config: &DecodeConfig{ |
| 131 | + TypeUnmarshalFuncs: map[reflect.Type]CustomizeDecodeFunc{ |
| 132 | + reflect.TypeOf(testTextUnmarshaler{}): func(req *protocol.Request, params param.Params, text string) (reflect.Value, error) { |
| 133 | + return reflect.Value{}, errors.New("err") |
| 134 | + }, |
| 135 | + }, |
| 136 | + }, |
| 137 | + expectError: true, |
| 138 | + }, |
| 139 | + } |
| 140 | + |
| 141 | + for _, tt := range tests { |
| 142 | + t.Run(tt.name, func(t *testing.T) { |
| 143 | + req := &protocol.Request{} |
| 144 | + params := param.Params{} |
| 145 | + config := tt.config |
| 146 | + if config == nil { |
| 147 | + config = &DecodeConfig{} |
| 148 | + } |
| 149 | + val, err := stringToValue(tt.elemType, tt.text, req, params, config) |
| 150 | + if tt.expectError { |
| 151 | + assert.NotNil(t, err) |
| 152 | + return |
| 153 | + } |
| 154 | + assert.Nil(t, err) |
| 155 | + assert.DeepEqual(t, tt.expectValue, val.Interface()) |
| 156 | + }) |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +func TestTryTextUnmarshaler(t *testing.T) { |
| 161 | + tests := []struct { |
| 162 | + name string |
| 163 | + value interface{} |
| 164 | + text string |
| 165 | + expected bool |
| 166 | + }{ |
| 167 | + { |
| 168 | + name: "text unmarshaler", |
| 169 | + value: &testTextUnmarshaler{}, |
| 170 | + text: "test text", |
| 171 | + expected: true, |
| 172 | + }, |
| 173 | + { |
| 174 | + name: "non text unmarshaler", |
| 175 | + value: &struct{}{}, |
| 176 | + text: "test text", |
| 177 | + expected: false, |
| 178 | + }, |
| 179 | + { |
| 180 | + name: "nil value", |
| 181 | + value: nil, |
| 182 | + text: "test text", |
| 183 | + expected: false, |
| 184 | + }, |
| 185 | + } |
| 186 | + |
| 187 | + for _, tt := range tests { |
| 188 | + t.Run(tt.name, func(t *testing.T) { |
| 189 | + var v reflect.Value |
| 190 | + if tt.value != nil { |
| 191 | + v = reflect.ValueOf(tt.value) |
| 192 | + } else { |
| 193 | + v = reflect.ValueOf(&tt.value).Elem() |
| 194 | + } |
| 195 | + |
| 196 | + result := tryTextUnmarshaler(v, tt.text) |
| 197 | + assert.DeepEqual(t, tt.expected, result) |
| 198 | + |
| 199 | + if tt.expected && tt.value != nil { |
| 200 | + // Verify the value was actually set |
| 201 | + unmarshaler := tt.value.(*testTextUnmarshaler) |
| 202 | + assert.DeepEqual(t, tt.text, unmarshaler.Value) |
| 203 | + } |
| 204 | + }) |
| 205 | + } |
| 206 | +} |
0 commit comments