2023-11-15 09:19:51 +00:00
|
|
|
package convert
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2023-12-22 13:20:45 +00:00
|
|
|
"github.com/databricks/cli/libs/dyn"
|
2024-03-25 11:01:09 +00:00
|
|
|
assert "github.com/databricks/cli/libs/dyn/dynassert"
|
2023-11-15 09:19:51 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func assertFromTypedToTypedEqual[T any](t *testing.T, src T) {
|
2023-12-22 13:20:45 +00:00
|
|
|
nv, err := FromTyped(src, dyn.NilValue)
|
2023-11-15 09:19:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var dst T
|
|
|
|
err = ToTyped(&dst, nv)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, src, dst)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAdditional(t *testing.T) {
|
|
|
|
type StructType struct {
|
|
|
|
Str string `json:"str"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Tmp struct {
|
|
|
|
MapToPointer map[string]*string `json:"map_to_pointer"`
|
|
|
|
SliceOfPointer []*string `json:"slice_of_pointer"`
|
|
|
|
NestedStruct StructType `json:"nested_struct"`
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("nil", func(t *testing.T) {
|
|
|
|
assertFromTypedToTypedEqual(t, Tmp{})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("empty map", func(t *testing.T) {
|
|
|
|
assertFromTypedToTypedEqual(t, Tmp{
|
|
|
|
MapToPointer: map[string]*string{},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2024-01-31 14:25:13 +00:00
|
|
|
t.Run("map with empty string value", func(t *testing.T) {
|
|
|
|
s := ""
|
|
|
|
assertFromTypedToTypedEqual(t, Tmp{
|
|
|
|
MapToPointer: map[string]*string{
|
|
|
|
"key": &s,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2023-11-15 09:19:51 +00:00
|
|
|
t.Run("map with nil value", func(t *testing.T) {
|
|
|
|
assertFromTypedToTypedEqual(t, Tmp{
|
|
|
|
MapToPointer: map[string]*string{
|
|
|
|
"key": nil,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("empty slice", func(t *testing.T) {
|
|
|
|
assertFromTypedToTypedEqual(t, Tmp{
|
|
|
|
SliceOfPointer: []*string{},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("slice with nil value", func(t *testing.T) {
|
|
|
|
assertFromTypedToTypedEqual(t, Tmp{
|
|
|
|
SliceOfPointer: []*string{nil},
|
|
|
|
})
|
|
|
|
})
|
2024-05-21 11:53:00 +00:00
|
|
|
|
|
|
|
t.Run("pointer to a empty string", func(t *testing.T) {
|
|
|
|
s := ""
|
|
|
|
assertFromTypedToTypedEqual(t, &s)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("nil pointer", func(t *testing.T) {
|
|
|
|
var s *string
|
|
|
|
assertFromTypedToTypedEqual(t, s)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("pointer to struct with scalar values", func(t *testing.T) {
|
|
|
|
s := ""
|
|
|
|
type foo struct {
|
|
|
|
A string `json:"a"`
|
|
|
|
B int `json:"b"`
|
|
|
|
C bool `json:"c"`
|
|
|
|
D *string `json:"d"`
|
|
|
|
}
|
|
|
|
assertFromTypedToTypedEqual(t, &foo{
|
|
|
|
A: "a",
|
|
|
|
B: 1,
|
|
|
|
C: true,
|
|
|
|
D: &s,
|
|
|
|
})
|
|
|
|
assertFromTypedToTypedEqual(t, &foo{
|
|
|
|
A: "",
|
|
|
|
B: 0,
|
|
|
|
C: false,
|
|
|
|
D: nil,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("map with scalar values", func(t *testing.T) {
|
|
|
|
assertFromTypedToTypedEqual(t, map[string]string{
|
|
|
|
"a": "a",
|
|
|
|
"b": "b",
|
|
|
|
"c": "",
|
|
|
|
})
|
|
|
|
assertFromTypedToTypedEqual(t, map[string]int{
|
|
|
|
"a": 1,
|
|
|
|
"b": 0,
|
|
|
|
"c": 2,
|
|
|
|
})
|
|
|
|
})
|
2023-11-15 09:19:51 +00:00
|
|
|
}
|