Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Changed

- `StringToMapConverter` and `StringToSliceConverter` parse "null" strings to strings.

### Fixed

- `GetAnyConverter`: don't parse any to slices and arrays.

## [v1.0.0] - 2024-10-09

The release updates `go-tarantool` connector from `v1` to `v2`.
Expand Down
10 changes: 10 additions & 0 deletions converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ func (StringToDatetimeConverter) Convert(src string) (any, error) {
return datetime.MakeDatetime(tm)
}

var errFailToConvertStringToMap = errors.New("can't convert string to map")

// StringToMapConverter is a converter from string to map.
// Only `json` is supported now.
type StringToMapConverter struct{}
Expand All @@ -235,9 +237,14 @@ func (StringToMapConverter) Convert(src string) (any, error) {
if err != nil {
return nil, err
}
if _, ok := result.(map[string]any); !ok {
return nil, errFailToConvertStringToMap
}
return result, nil
}

var errFailToConvertStringToSlice = errors.New("can't convert string to slice")

// StringToSliceConverter is a converter from string to slice.
// Only `json` is supported now.
type StringToSliceConverter struct{}
Expand All @@ -254,6 +261,9 @@ func (StringToSliceConverter) Convert(src string) (any, error) {
if err != nil {
return nil, err
}
if _, ok := result.([]any); !ok {
return nil, errFailToConvertStringToSlice
}
return result, nil
}

Expand Down
2 changes: 1 addition & 1 deletion converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func HelperTestConverter[S any, T any](
t *testing.T,
mp tupleconv.Converter[S, T],
cases []convCase[S, T]) {
t.Helper()
for _, tc := range cases {
t.Run(fmt.Sprintln(tc.value), func(t *testing.T) {
result, err := mp.Convert(tc.value)
Expand Down Expand Up @@ -379,7 +380,6 @@ func TestMakeSequenceConverter(t *testing.T) {
{value: "-10", expected: int64(-10)},
{value: "2.5", expected: 2.5},
{value: "{}", expected: map[string]any{}},
{value: "null", expected: nil}, // As `json`.

// Error.
{value: "12-13-14", isErr: true},
Expand Down
14 changes: 11 additions & 3 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func upTarantool() (func(), error) {
})
if err != nil {
test_helpers.StopTarantoolWithCleanup(inst)
return nil, nil
return nil, err
}

cleanup := func() {
Expand Down Expand Up @@ -235,7 +235,11 @@ func ExampleMap_insertMappedTuples() {
}
defer cleanupTarantool()

conn, _ := tarantool.Connect(context.Background(), dialer, opts)
conn, err := tarantool.Connect(context.Background(), dialer, opts)
if err != nil {
fmt.Printf("can't connect to tarantool: %v\n", err)
return
}
defer conn.Close()

var spaceFmtResp [][]tupleconv.SpaceField
Expand Down Expand Up @@ -313,7 +317,11 @@ func Example_ttEncoder() {
tupleEncoder := tupleconv.MakeMapper([]tupleconv.Converter[any, string]{}).
WithDefaultConverter(converter)

conn, _ := tarantool.Connect(context.Background(), dialer, opts)
conn, err := tarantool.Connect(context.Background(), dialer, opts)
if err != nil {
fmt.Printf("can't connect to tarantool: %v\n", err)
return
}
defer conn.Close()

req := tarantool.NewSelectRequest("finances")
Expand Down
2 changes: 2 additions & 0 deletions tt.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ func (fac StringToTTConvFactory) GetAnyConverter() Converter[string, any] {
fac.GetDatetimeConverter(),
fac.GetUUIDConverter(),
fac.GetIntervalConverter(),
fac.GetArrayConverter(),
fac.GetMapConverter(),
fac.GetStringConverter(),
})
}
Expand Down
18 changes: 17 additions & 1 deletion tt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func TestStringToTTConvFactory(t *testing.T) {
},
},
},
{value: "null", expected: nil},
{value: "null", isErr: true},

// Nullable.
{value: "null", isNullable: true, expected: nil},
Expand Down Expand Up @@ -300,6 +300,22 @@ func TestStringToTTConvFactory(t *testing.T) {
Year: -11, Month: -1110, Nsec: 1, Adjust: 2,
},
},
{
value: "[1, 2, 3, 4]",
expected: []any{float64(1), float64(2), float64(3), float64(4)},
},
{
value: `{"1": [1,2,3], "2": {"a":4} }`,
expected: map[string]any{
"1": []any{float64(1), float64(2), float64(3)},
"2": map[string]any{
"a": float64(4),
},
},
},
{value: "null", expected: "null"},
{value: "nil", expected: "nil"},
{value: "NULL", expected: "NULL"},
},
tupleconv.TypeScalar: {
{value: "1`e2", expected: 100.0},
Expand Down
Loading