Skip to content

Commit

Permalink
improve the dynamic converter to distinguish null list and empty list (
Browse files Browse the repository at this point in the history
  • Loading branch information
ms-henglu committed Apr 28, 2024
1 parent 1dc49e4 commit c33ba00
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 9 deletions.
6 changes: 3 additions & 3 deletions internal/services/dynamic/dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func ToJSON(d types.Dynamic) ([]byte, error) {
}

func attrListToJSON(in []attr.Value) ([]json.RawMessage, error) {
var l []json.RawMessage
l := make([]json.RawMessage, 0)
for _, v := range in {
vv, err := attrValueToJSON(v)
if err != nil {
Expand Down Expand Up @@ -103,7 +103,7 @@ func attrListFromJSON(b []byte, etyp attr.Type) ([]attr.Value, error) {
if err := json.Unmarshal(b, &l); err != nil {
return nil, err
}
var vals []attr.Value
vals := make([]attr.Value, 0)
for _, b := range l {
val, err := attrValueFromJSON(b, etyp)
if err != nil {
Expand Down Expand Up @@ -200,7 +200,7 @@ func attrValueFromJSON(b []byte, typ attr.Type) (attr.Value, error) {
if len(l) != len(typ.ElemTypes) {
return nil, fmt.Errorf("tuple element size not match: json=%d, type=%d", len(l), len(typ.ElemTypes))
}
var vals []attr.Value
vals := make([]attr.Value, 0)
for i, b := range l {
val, err := attrValueFromJSON(b, typ.ElemTypes[i])
if err != nil {
Expand Down
74 changes: 68 additions & 6 deletions internal/services/dynamic/dynamic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,18 @@ func TestToJSON(t *testing.T) {
"list": types.ListType{
ElemType: types.BoolType,
},
"list_empty": types.ListType{
ElemType: types.BoolType,
},
"list_null": types.ListType{
ElemType: types.BoolType,
},
"set": types.SetType{
ElemType: types.BoolType,
},
"set_empty": types.SetType{
ElemType: types.BoolType,
},
"set_null": types.SetType{
ElemType: types.BoolType,
},
Expand All @@ -42,6 +48,9 @@ func TestToJSON(t *testing.T) {
types.StringType,
},
},
"tuple_empty": types.TupleType{
ElemTypes: []attr.Type{},
},
"tuple_null": types.TupleType{
ElemTypes: []attr.Type{
types.BoolType,
Expand All @@ -51,6 +60,9 @@ func TestToJSON(t *testing.T) {
"map": types.MapType{
ElemType: types.BoolType,
},
"map_empty": types.MapType{
ElemType: types.BoolType,
},
"map_null": types.MapType{
ElemType: types.BoolType,
},
Expand All @@ -60,6 +72,9 @@ func TestToJSON(t *testing.T) {
"string": types.StringType,
},
},
"object_empty": types.ObjectType{
AttrTypes: map[string]attr.Type{},
},
"object_null": types.ObjectType{
AttrTypes: map[string]attr.Type{
"bool": types.BoolType,
Expand All @@ -85,15 +100,17 @@ func TestToJSON(t *testing.T) {
types.BoolValue(false),
},
),
"list_null": types.ListNull(types.BoolType),
"list_empty": types.ListValueMust(types.BoolType, []attr.Value{}),
"list_null": types.ListNull(types.BoolType),
"set": types.SetValueMust(
types.BoolType,
[]attr.Value{
types.BoolValue(true),
types.BoolValue(false),
},
),
"set_null": types.SetNull(types.BoolType),
"set_empty": types.SetValueMust(types.BoolType, []attr.Value{}),
"set_null": types.SetNull(types.BoolType),
"tuple": types.TupleValueMust(
[]attr.Type{
types.BoolType,
Expand All @@ -104,6 +121,10 @@ func TestToJSON(t *testing.T) {
types.StringValue("a"),
},
),
"tuple_empty": types.TupleValueMust(
[]attr.Type{},
[]attr.Value{},
),
"tuple_null": types.TupleNull(
[]attr.Type{
types.BoolType,
Expand All @@ -116,7 +137,8 @@ func TestToJSON(t *testing.T) {
"a": types.BoolValue(true),
},
),
"map_null": types.MapNull(types.BoolType),
"map_empty": types.MapValueMust(types.BoolType, map[string]attr.Value{}),
"map_null": types.MapNull(types.BoolType),
"object": types.ObjectValueMust(
map[string]attr.Type{
"bool": types.BoolType,
Expand All @@ -127,6 +149,10 @@ func TestToJSON(t *testing.T) {
"string": types.StringValue("a"),
},
),
"object_empty": types.ObjectValueMust(
map[string]attr.Type{},
map[string]attr.Value{},
),
"object_null": types.ObjectNull(
map[string]attr.Type{
"bool": types.BoolType,
Expand All @@ -150,19 +176,24 @@ func TestToJSON(t *testing.T) {
"number": 1.23,
"number_null": null,
"list": [true, false],
"list_empty": [],
"list_null": null,
"set": [true, false],
"set_empty": [],
"set_null": null,
"tuple": [true, "a"],
"tuple_empty": [],
"tuple_null": null,
"map": {
"a": true
},
"map_empty": {},
"map_null": null,
"object": {
"bool": true,
"string": "a"
},
"object_empty": {},
"object_null": null
}`

Expand Down Expand Up @@ -192,19 +223,24 @@ func TestFromJSON(t *testing.T) {
"number": 1.23,
"number_null": null,
"list": [true, false],
"list_empty": [],
"list_null": null,
"set": [true, false],
"set_empty": [],
"set_null": null,
"tuple": [true, "a"],
"tuple_empty": [],
"tuple_null": null,
"map": {
"a": true
},
"map_empty": {},
"map_null": null,
"object": {
"bool": true,
"string": "a"
},
"object_empty": {},
"object_null": null
}`,
expect: types.DynamicValue(
Expand All @@ -223,12 +259,18 @@ func TestFromJSON(t *testing.T) {
"list": types.ListType{
ElemType: types.BoolType,
},
"list_empty": types.ListType{
ElemType: types.BoolType,
},
"list_null": types.ListType{
ElemType: types.BoolType,
},
"set": types.SetType{
ElemType: types.BoolType,
},
"set_empty": types.SetType{
ElemType: types.BoolType,
},
"set_null": types.SetType{
ElemType: types.BoolType,
},
Expand All @@ -238,6 +280,9 @@ func TestFromJSON(t *testing.T) {
types.StringType,
},
},
"tuple_empty": types.TupleType{
ElemTypes: []attr.Type{},
},
"tuple_null": types.TupleType{
ElemTypes: []attr.Type{
types.BoolType,
Expand All @@ -247,6 +292,9 @@ func TestFromJSON(t *testing.T) {
"map": types.MapType{
ElemType: types.BoolType,
},
"map_empty": types.MapType{
ElemType: types.BoolType,
},
"map_null": types.MapType{
ElemType: types.BoolType,
},
Expand All @@ -256,6 +304,9 @@ func TestFromJSON(t *testing.T) {
"string": types.StringType,
},
},
"object_empty": types.ObjectType{
AttrTypes: map[string]attr.Type{},
},
"object_null": types.ObjectType{
AttrTypes: map[string]attr.Type{
"bool": types.BoolType,
Expand All @@ -281,15 +332,17 @@ func TestFromJSON(t *testing.T) {
types.BoolValue(false),
},
),
"list_null": types.ListNull(types.BoolType),
"list_empty": types.ListValueMust(types.BoolType, []attr.Value{}),
"list_null": types.ListNull(types.BoolType),
"set": types.SetValueMust(
types.BoolType,
[]attr.Value{
types.BoolValue(true),
types.BoolValue(false),
},
),
"set_null": types.SetNull(types.BoolType),
"set_empty": types.SetValueMust(types.BoolType, []attr.Value{}),
"set_null": types.SetNull(types.BoolType),
"tuple": types.TupleValueMust(
[]attr.Type{
types.BoolType,
Expand All @@ -300,6 +353,10 @@ func TestFromJSON(t *testing.T) {
types.StringValue("a"),
},
),
"tuple_empty": types.TupleValueMust(
[]attr.Type{},
[]attr.Value{},
),
"tuple_null": types.TupleNull(
[]attr.Type{
types.BoolType,
Expand All @@ -312,7 +369,8 @@ func TestFromJSON(t *testing.T) {
"a": types.BoolValue(true),
},
),
"map_null": types.MapNull(types.BoolType),
"map_empty": types.MapValueMust(types.BoolType, map[string]attr.Value{}),
"map_null": types.MapNull(types.BoolType),
"object": types.ObjectValueMust(
map[string]attr.Type{
"bool": types.BoolType,
Expand All @@ -323,6 +381,10 @@ func TestFromJSON(t *testing.T) {
"string": types.StringValue("a"),
},
),
"object_empty": types.ObjectValueMust(
map[string]attr.Type{},
map[string]attr.Value{},
),
"object_null": types.ObjectNull(
map[string]attr.Type{
"bool": types.BoolType,
Expand Down

0 comments on commit c33ba00

Please sign in to comment.