Added more tests for decoding JSON->maps
Make TestDecode_flattenedJSON table driven to add more test cases as reproduced in terraform.
This commit is contained in:
parent
d3228f113d
commit
769aa72436
203
decoder_test.go
203
decoder_test.go
@ -6,6 +6,7 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
"github.com/hashicorp/hcl/hcl/ast"
|
"github.com/hashicorp/hcl/hcl/ast"
|
||||||
"github.com/hashicorp/hcl/testhelper"
|
"github.com/hashicorp/hcl/testhelper"
|
||||||
)
|
)
|
||||||
@ -852,21 +853,46 @@ func TestDecode_topLevelKeys(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDecode_structureFlattened(t *testing.T) {
|
func TestDecode_flattenedJSON(t *testing.T) {
|
||||||
jsonA := `
|
// make sure we can also correctly extract a Name key too
|
||||||
|
type V struct {
|
||||||
|
Name string `hcl:",key"`
|
||||||
|
Description string
|
||||||
|
Default map[string]string
|
||||||
|
}
|
||||||
|
type Vars struct {
|
||||||
|
Variable []*V
|
||||||
|
}
|
||||||
|
|
||||||
|
cases := []struct {
|
||||||
|
JSON string
|
||||||
|
Out interface{}
|
||||||
|
Expected interface{}
|
||||||
|
}{
|
||||||
|
{ // Nested object, no sibling keys
|
||||||
|
JSON: `
|
||||||
{
|
{
|
||||||
"var_1": {
|
"var_name": {
|
||||||
"default": {
|
"default": {
|
||||||
"key1": "a",
|
"key1": "a",
|
||||||
"key2": "b"
|
"key2": "b"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
`
|
`,
|
||||||
|
Out: &[]*V{},
|
||||||
|
Expected: &[]*V{
|
||||||
|
&V{
|
||||||
|
Name: "var_name",
|
||||||
|
Default: map[string]string{"key1": "a", "key2": "b"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
jsonB := `
|
{ // Nested object with a sibling key (this worked previously)
|
||||||
|
JSON: `
|
||||||
{
|
{
|
||||||
"var_2": {
|
"var_name": {
|
||||||
"description": "Described",
|
"description": "Described",
|
||||||
"default": {
|
"default": {
|
||||||
"key1": "a",
|
"key1": "a",
|
||||||
@ -874,51 +900,132 @@ func TestDecode_structureFlattened(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
`
|
`,
|
||||||
|
Out: &[]*V{},
|
||||||
// make sure we can also correctly extract the Name key
|
Expected: &[]*V{
|
||||||
type V struct {
|
&V{
|
||||||
Name string `hcl:",key"`
|
Name: "var_name",
|
||||||
Description string
|
Description: "Described",
|
||||||
Default map[string]string
|
Default: map[string]string{"key1": "a", "key2": "b"},
|
||||||
}
|
},
|
||||||
|
},
|
||||||
var vA, vB []*V
|
|
||||||
|
|
||||||
err := Decode(&vA, jsonA)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("err: %s", err)
|
|
||||||
}
|
|
||||||
if len(vA) != 1 {
|
|
||||||
t.Fatal("failed to decode jsonA")
|
|
||||||
}
|
|
||||||
|
|
||||||
err = Decode(&vB, jsonB)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("err: %s", err)
|
|
||||||
}
|
|
||||||
if len(vB) != 1 {
|
|
||||||
t.Fatal("failed to decode jsonB")
|
|
||||||
}
|
|
||||||
|
|
||||||
expectedA := []*V{
|
|
||||||
&V{
|
|
||||||
Name: "var_1",
|
|
||||||
Default: map[string]string{"key1": "a", "key2": "b"},
|
|
||||||
},
|
},
|
||||||
}
|
|
||||||
expectedB := []*V{
|
{ // Multiple nested objects, one with a sibling key
|
||||||
&V{
|
JSON: `
|
||||||
Name: "var_2",
|
{
|
||||||
Description: "Described",
|
"variable": {
|
||||||
Default: map[string]string{"key1": "a", "key2": "b"},
|
"var_1": {
|
||||||
|
"default": {
|
||||||
|
"key1": "a",
|
||||||
|
"key2": "b"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"var_2": {
|
||||||
|
"description": "Described",
|
||||||
|
"default": {
|
||||||
|
"key1": "a",
|
||||||
|
"key2": "b"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
Out: &Vars{},
|
||||||
|
Expected: &Vars{
|
||||||
|
Variable: []*V{
|
||||||
|
&V{
|
||||||
|
Name: "var_1",
|
||||||
|
Default: map[string]string{"key1": "a", "key2": "b"},
|
||||||
|
},
|
||||||
|
&V{
|
||||||
|
Name: "var_2",
|
||||||
|
Description: "Described",
|
||||||
|
Default: map[string]string{"key1": "a", "key2": "b"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
{ // Nested object to maps
|
||||||
|
JSON: `
|
||||||
|
{
|
||||||
|
"variable": {
|
||||||
|
"var_name": {
|
||||||
|
"description": "Described",
|
||||||
|
"default": {
|
||||||
|
"key1": "a",
|
||||||
|
"key2": "b"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
Out: &[]map[string]interface{}{},
|
||||||
|
Expected: &[]map[string]interface{}{
|
||||||
|
{
|
||||||
|
"variable": []map[string]interface{}{
|
||||||
|
{
|
||||||
|
"var_name": []map[string]interface{}{
|
||||||
|
{
|
||||||
|
"description": "Described",
|
||||||
|
"default": []map[string]interface{}{
|
||||||
|
{
|
||||||
|
"key1": "a",
|
||||||
|
"key2": "b",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
{ // Nested object to maps without a sibling key should decode the same as above
|
||||||
|
JSON: `
|
||||||
|
{
|
||||||
|
"variable": {
|
||||||
|
"var_name": {
|
||||||
|
"default": {
|
||||||
|
"key1": "a",
|
||||||
|
"key2": "b"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
Out: &[]map[string]interface{}{},
|
||||||
|
Expected: &[]map[string]interface{}{
|
||||||
|
{
|
||||||
|
"variable": []map[string]interface{}{
|
||||||
|
{
|
||||||
|
"var_name": []map[string]interface{}{
|
||||||
|
{
|
||||||
|
"default": []map[string]interface{}{
|
||||||
|
{
|
||||||
|
"key1": "a",
|
||||||
|
"key2": "b",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
if !reflect.DeepEqual(vA, expectedA) {
|
for i, tc := range cases {
|
||||||
t.Fatalf("\nexpected: %#v\ngot: %#v\n", expectedA[0], vA[0])
|
err := Decode(tc.Out, tc.JSON)
|
||||||
}
|
if err != nil {
|
||||||
if !reflect.DeepEqual(vB, expectedB) {
|
t.Fatalf("[%d] err: %s", i, err)
|
||||||
t.Fatalf("\nexpected: %#v\ngot: %#v\n", expectedB[0], vB[0])
|
}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(tc.Out, tc.Expected) {
|
||||||
|
t.Fatalf("[%d]\ngot: %s\nexpected: %s\n", i, spew.Sdump(tc.Out), spew.Sdump(tc.Expected))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user