Skip to content

Instantly share code, notes, and snippets.

@cornfeedhobo
Created November 19, 2016 16:18
Show Gist options
  • Save cornfeedhobo/491149b99bf863142780f4bb94a5841a to your computer and use it in GitHub Desktop.
Save cornfeedhobo/491149b99bf863142780f4bb94a5841a to your computer and use it in GitHub Desktop.
HCL parsing strangeness
// Copyright © 2016 cornfeedhobo
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package main
import (
"fmt"
"testing"
"github.com/hashicorp/hcl"
"github.com/stretchr/testify/assert"
)
var exHCL []byte = []byte(`
a "a_type1" {
attr = "val"
z "z_type1" {
attr = "val"
}
b "b_type1" {
attr = "val"
z "z_type2" {
attr = "val"
}
z "z_type3" {
attr = "val"
}
}
}
`)
func TestGoodJSON(t *testing.T) {
type Z struct {
Type string `hcl:",key"`
Attr string `hcl:"attr"`
}
type B struct {
Type string `hcl:",key"`
Attr string `hcl:"attr"`
Zs []Z `hcl:"z"`
}
type A struct {
Type string `hcl:",key"`
Attr string `hcl:"attr"`
Zs []Z `hcl:"z"`
Bs []B `hcl:"b"`
}
type Example struct {
As []A `hcl:"a"`
}
var outHCL Example
if err := hcl.Unmarshal(exHCL, &outHCL); err != nil {
fmt.Println(err)
t.Fail()
}
exJSON := []byte(`
{
"a": {
"a_type1": {
"b": {
"b_type1": {
"attr": "val",
"z": {
"z_type2": {
"attr": "val"
},
"z_type3": {
"attr": "val"
}
}
}
},
"z": {
"z_type1": {
"attr": "val"
}
},
"attr": "val"
}
}
}
`)
var outJSON Example
if err := hcl.Unmarshal(exJSON, &outJSON); err != nil {
fmt.Println(err)
t.Fail()
}
assert.Equal(t, outHCL, outJSON, "These should be equal")
}
func TestBadJSON(t *testing.T) {
type Z struct {
Type string `hcl:",key"`
}
type B struct {
Type string `hcl:",key"`
Zs []Z `hcl:"z"`
}
type A struct {
Type string `hcl:",key"`
Zs []Z `hcl:"z"`
Bs []B `hcl:"b"`
}
type Example struct {
As []A `hcl:"a"`
}
var outHCL Example
if err := hcl.Unmarshal(exHCL, &outHCL); err != nil {
fmt.Println(err)
t.Fail()
}
exJSON := []byte(`
{
"a": {
"a_type1": {
"b": {
"b_type1": {
"z": {
"z_type2": {},
"z_type3": {}
}
}
},
"z": {
"z_type1": {}
}
}
}
}
`)
var outJSON Example
if err := hcl.Unmarshal(exJSON, &outJSON); err != nil {
fmt.Println(err)
t.Fail()
}
assert.Equal(t, outHCL, outJSON, "These should be equal")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment