Skip to content

Commit

Permalink
tftypes: Clarified ValueFromJSON error messaging with object attrib…
Browse files Browse the repository at this point in the history
…ute key issues (#214)

Reference: #211
  • Loading branch information
bflad committed Jul 28, 2022
1 parent 71dfab6 commit b351d0b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .changelog/214.txt
@@ -0,0 +1,3 @@
```release-note:bug
tftypes: Clarified `ValueFromJSON` error messaging with object attribute key issues
```
7 changes: 3 additions & 4 deletions tftypes/value_json.go
Expand Up @@ -453,15 +453,15 @@ func jsonUnmarshalObject(buf []byte, attrTypes map[string]Type, p *AttributePath

vals := map[string]Value{}
for dec.More() {
innerPath := p.WithElementKeyValue(NewValue(String, UnknownValue))
tok, err := dec.Token()
if err != nil {
return Value{}, innerPath.NewErrorf("error reading token: %w", err)
return Value{}, p.NewErrorf("error reading object attribute key token: %w", err)
}
key, ok := tok.(string)
if !ok {
return Value{}, innerPath.NewErrorf("object attribute key was %T, not string", tok)
return Value{}, p.NewErrorf("object attribute key was %T with value %v, not string", tok, tok)
}
innerPath := p.WithAttributeName(key)
attrType, ok := attrTypes[key]
if !ok {
if opts.IgnoreUndefinedAttributes {
Expand All @@ -472,7 +472,6 @@ func jsonUnmarshalObject(buf []byte, attrTypes map[string]Type, p *AttributePath

return Value{}, innerPath.NewErrorf("unsupported attribute %q", key)
}
innerPath = p.WithAttributeName(key)

var rawVal json.RawMessage
err = dec.Decode(&rawVal)
Expand Down
36 changes: 31 additions & 5 deletions tftypes/value_json_test.go
@@ -1,6 +1,7 @@
package tftypes

import (
"fmt"
"math/big"
"testing"

Expand All @@ -10,9 +11,10 @@ import (
func TestValueFromJSON(t *testing.T) {
t.Parallel()
type testCase struct {
value Value
typ Type
json string
value Value
typ Type
json string
expectedError error
}
tests := map[string]testCase{
// Primitives
Expand Down Expand Up @@ -211,6 +213,30 @@ func TestValueFromJSON(t *testing.T) {
},
json: `{}`,
},
"object-attribute-key-token-error": {
value: Value{},
typ: Object{
AttributeTypes: map[string]Type{},
},
json: `{{}}`,
expectedError: AttributePathError{
Path: NewAttributePath(),
err: fmt.Errorf("error reading object attribute key token: invalid character '{'"),
},
},
"object-attribute-key-missing-error": {
value: Value{},
typ: Object{
AttributeTypes: map[string]Type{
"test": String,
},
},
json: `{"not-test": "test-value"}`,
expectedError: AttributePathError{
Path: NewAttributePath().WithAttributeName("not-test"),
err: fmt.Errorf("unsupported attribute \"not-test\""),
},
},
"object-of-bool_number": {
value: NewValue(Object{
AttributeTypes: map[string]Type{
Expand Down Expand Up @@ -371,8 +397,8 @@ func TestValueFromJSON(t *testing.T) {
t.Run(name, func(t *testing.T) {
t.Parallel()
val, err := ValueFromJSON([]byte(test.json), test.typ)
if err != nil {
t.Fatalf("unexpected error unmarshaling: %s", err)
if diff := cmp.Diff(test.expectedError, err); diff != "" {
t.Errorf("unexpected error difference: %s", diff)
}
if diff := cmp.Diff(test.value, val); diff != "" {
t.Errorf("Unexpected results (-wanted +got): %s", diff)
Expand Down

0 comments on commit b351d0b

Please sign in to comment.