Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tftypes: Clarified ValueFromJSON error messaging with object attribute key issues #214

Merged
merged 3 commits into from Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -435,20 +435,19 @@ 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 {
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