handle empty json

This commit is contained in:
James Bardin 2020-10-19 09:31:37 -04:00
parent e3804810a9
commit 353937411d
2 changed files with 39 additions and 4 deletions

View File

@ -603,14 +603,17 @@ func (p *GRPCProvider) Close() error {
// Decode a DynamicValue from either the JSON or MsgPack encoding. // Decode a DynamicValue from either the JSON or MsgPack encoding.
func decodeDynamicValue(v *proto.DynamicValue, ty cty.Type) (cty.Value, error) { func decodeDynamicValue(v *proto.DynamicValue, ty cty.Type) (cty.Value, error) {
// always return a valid value // always return a valid value
var err error
res := cty.NullVal(ty) res := cty.NullVal(ty)
if v == nil { if v == nil {
return res, nil return res, nil
} }
if len(v.Msgpack) > 0 { switch {
return msgpack.Unmarshal(v.Msgpack, ty) case len(v.Msgpack) > 0:
res, err = msgpack.Unmarshal(v.Msgpack, ty)
case len(v.Json) > 0:
res, err = ctyjson.Unmarshal(v.Json, ty)
} }
return res, err
return ctyjson.Unmarshal(v.Json, ty)
} }

View File

@ -310,6 +310,38 @@ func TestGRPCProvider_ReadResourceJSON(t *testing.T) {
} }
} }
func TestGRPCProvider_ReadEmptyJSON(t *testing.T) {
client := mockProviderClient(t)
p := &GRPCProvider{
client: client,
}
client.EXPECT().ReadResource(
gomock.Any(),
gomock.Any(),
).Return(&proto.ReadResource_Response{
NewState: &proto.DynamicValue{
Json: []byte(``),
},
}, nil)
obj := cty.ObjectVal(map[string]cty.Value{
"attr": cty.StringVal("foo"),
})
resp := p.ReadResource(providers.ReadResourceRequest{
TypeName: "resource",
PriorState: obj,
})
checkDiags(t, resp.Diagnostics)
expected := cty.NullVal(obj.Type())
if !cmp.Equal(expected, resp.NewState, typeComparer, valueComparer, equateEmpty) {
t.Fatal(cmp.Diff(expected, resp.NewState, typeComparer, valueComparer, equateEmpty))
}
}
func TestGRPCProvider_PlanResourceChange(t *testing.T) { func TestGRPCProvider_PlanResourceChange(t *testing.T) {
client := mockProviderClient(t) client := mockProviderClient(t)
p := &GRPCProvider{ p := &GRPCProvider{