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

Marshal method for HCL Parser #152

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
39 changes: 18 additions & 21 deletions parsers/hcl/hcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
package hcl

import (
"errors"
"bytes"
"encoding/json"

"github.com/hashicorp/hcl"
"github.com/hashicorp/hcl/hcl/printer"
)

// HCL implements a Hashicorp HCL parser.
Expand Down Expand Up @@ -38,27 +40,22 @@ func (p *HCL) Unmarshal(b []byte) (map[string]interface{}, error) {

// Marshal marshals the given config map to HCL bytes.
func (p *HCL) Marshal(o map[string]interface{}) ([]byte, error) {
return nil, errors.New("HCL marshalling is not supported")
// TODO: Although this is the only way to do it, it's producing empty bytes.
// Needs investigation.
// The only way to generate HCL is from the HCL node structure.
// Turn the map into JSON, then parse it with the HCL lib to create its
// structure, and then, encode to HCL.
// j, err := json.Marshal(o)
// if err != nil {
// return nil, err
// }
// tree, err := hcl.Parse(string(j))
// if err != nil {
// return nil, err
// }
j, err := json.Marshal(o)
if err != nil {
return nil, err
}

tree, err := hcl.Parse(string(j))
if err != nil {
return nil, err
}

buf := new(bytes.Buffer)
if err = printer.Fprint(buf, tree); err != nil {
return nil, err
}

// var buf bytes.Buffer
// out := bufio.NewWriter(&buf)
// if err := printer.Fprint(out, tree.Node); err != nil {
// return nil, err
// }
// return buf.Bytes(), err
return buf.Bytes(), nil
}

// flattenHCL flattens an unmarshalled HCL structure where maps
Expand Down
38 changes: 33 additions & 5 deletions parsers/hcl/hcl_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hcl

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -108,20 +109,31 @@ func TestHCL_Unmarshal(t *testing.T) {
func TestHCL_Marshal(t *testing.T) {

hclParserWithFlatten := Parser(true)
// hclParserWithoutFlatten := Parser(false)

testCases := []struct {
name string
input map[string]interface{}
output []byte
isErr bool
function HCL
}{
{
name: "Empty HCL",
input: map[string]interface{}{},
isErr: true,
output: []byte(nil),
function: *hclParserWithFlatten,
},
{
name: "Valid HCL",
input: map[string]interface{}{
"resource": map[string]interface{}{
"aws_instance": map[string]interface{}{
"example": map[string]interface{}{
"ami": "abc123"}}}},
output: []byte(`"resource" "aws_instance" "example" {
"ami" = "abc123"
}`),
},
{
name: "Complex HCL",
input: map[string]interface{}{
Expand All @@ -138,17 +150,33 @@ func TestHCL_Marshal(t *testing.T) {
}},
}},
},
isErr: true,
output: []byte(`"resource" = {
"aws_instance" = {
"example" = {
"ami" = "abc123"

"count" = 2

"instance_type" = "t2.micro"

"lifecycle" = {
"create_before_destroy" = true
}
}
}
}`),
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
_, err := tc.function.Marshal(tc.input)
out, err := tc.function.Marshal(tc.input)
if tc.isErr {
assert.EqualError(t, err, "HCL marshalling is not supported")
assert.NotNil(t, err)
} else {
assert.Nil(t, err)
fmt.Printf("string(out): %v\n", string(out))
assert.Equal(t, tc.output, out)
}
})
}
Expand Down