Skip to content

Commit 69fc554

Browse files
committed
fix: parsing any to slice and array
It was fixed that Any was not parsed in slice or array. In addition, the behavior of parsing null strings has been changed. It is now expected to be a string. Closes TNTP-4101
1 parent ec4a4ff commit 69fc554

5 files changed

Lines changed: 42 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
66
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
77

8+
## [Unreleased]
9+
10+
### Changed
11+
12+
- `StringToMapConverter` and `StringToSliceConverter` parse "null" strings to strings.
13+
14+
### Fixed
15+
16+
- `GetAnyConverter`: don't parse any to slices and arrays.
17+
818
## [v1.0.0] - 2024-10-09
919

1020
The release updates `go-tarantool` connector from `v1` to `v2`.

converter.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@ func (StringToDatetimeConverter) Convert(src string) (any, error) {
219219
return datetime.MakeDatetime(tm)
220220
}
221221

222+
var errFailToConvertStringToMap = errors.New("can't convert string to map")
223+
222224
// StringToMapConverter is a converter from string to map.
223225
// Only `json` is supported now.
224226
type StringToMapConverter struct{}
@@ -235,9 +237,14 @@ func (StringToMapConverter) Convert(src string) (any, error) {
235237
if err != nil {
236238
return nil, err
237239
}
238-
return result, nil
240+
if _, ok := result.(map[string]any); ok {
241+
return result, nil
242+
}
243+
return nil, errFailToConvertStringToMap
239244
}
240245

246+
var errFailToConvertStringToSlice = errors.New("can't convert string to slice")
247+
241248
// StringToSliceConverter is a converter from string to slice.
242249
// Only `json` is supported now.
243250
type StringToSliceConverter struct{}
@@ -254,7 +261,10 @@ func (StringToSliceConverter) Convert(src string) (any, error) {
254261
if err != nil {
255262
return nil, err
256263
}
257-
return result, nil
264+
if _, ok := result.([]any); ok {
265+
return result, nil
266+
}
267+
return nil, errFailToConvertStringToSlice
258268
}
259269

260270
// StringToBinaryConverter is a converter from string to binary.

converter_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func HelperTestConverter[S any, T any](
2525
t *testing.T,
2626
mp tupleconv.Converter[S, T],
2727
cases []convCase[S, T]) {
28+
t.Helper()
2829
for _, tc := range cases {
2930
t.Run(fmt.Sprintln(tc.value), func(t *testing.T) {
3031
result, err := mp.Convert(tc.value)
@@ -379,7 +380,6 @@ func TestMakeSequenceConverter(t *testing.T) {
379380
{value: "-10", expected: int64(-10)},
380381
{value: "2.5", expected: 2.5},
381382
{value: "{}", expected: map[string]any{}},
382-
{value: "null", expected: nil}, // As `json`.
383383

384384
// Error.
385385
{value: "12-13-14", isErr: true},

tt.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ func (fac StringToTTConvFactory) GetAnyConverter() Converter[string, any] {
179179
fac.GetDatetimeConverter(),
180180
fac.GetUUIDConverter(),
181181
fac.GetIntervalConverter(),
182+
fac.GetArrayConverter(),
183+
fac.GetMapConverter(),
182184
fac.GetStringConverter(),
183185
})
184186
}

tt_test.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ func TestStringToTTConvFactory(t *testing.T) {
208208
},
209209
},
210210
},
211-
{value: "null", expected: nil},
211+
{value: "null", isErr: true},
212212

213213
// Nullable.
214214
{value: "null", isNullable: true, expected: nil},
@@ -300,6 +300,22 @@ func TestStringToTTConvFactory(t *testing.T) {
300300
Year: -11, Month: -1110, Nsec: 1, Adjust: 2,
301301
},
302302
},
303+
{
304+
value: "[1, 2, 3, 4]",
305+
expected: []any{float64(1), float64(2), float64(3), float64(4)},
306+
},
307+
{
308+
value: `{"1": [1,2,3], "2": {"a":4} }`,
309+
expected: map[string]any{
310+
"1": []any{float64(1), float64(2), float64(3)},
311+
"2": map[string]any{
312+
"a": float64(4),
313+
},
314+
},
315+
},
316+
{value: "null", expected: "null"},
317+
{value: "nil", expected: "nil"},
318+
{value: "NULL", expected: "NULL"},
303319
},
304320
tupleconv.TypeScalar: {
305321
{value: "1`e2", expected: 100.0},

0 commit comments

Comments
 (0)