hashicorp / hashicorp/hcl

JSON parser fails to parse JSON generated from HCL

Open
#141 0 comments 3 reactions 0 assignees View on GitHub
v1
Dominant language
Go
Stars
5.8k
Forks
657
Avg merge
20h 36m
Merged PRs (30d)
6

Description

The following code fails:

``` go
package main

import (
"encoding/json"
"reflect"

"github.com/hashicorp/hcl"
)

var hclData = []byte(`match "first" {
type = "test"
value = "first"
}
match "second" {
type = "test"
value = "second"
match "inner" {
type = "test"
value = "third"
}
}
`)

type Data struct {
Matches []*Match `json:"match" hcl:"match"`
}
type Match struct {
Type string `json:"type" hcl:"type"`
Value string `json:"value" hcl:"value"`
Matches []*Match `json:"match,omitempty" hcl:"match"`
}

func main() {
var d, e *Data
err := hcl.Unmarshal(hclData, &d)
if err != nil {
panic("failed to unmarshal hclData: " + err.Error())
}
jData, err := json.MarshalIndent(d, "", " ")
if err != nil {
panic("failed to marshal into jData: " + err.Error())
}

err = hcl.Decode(&e, string(jData))
if err != nil {
panic("failed to parse jData: " + err.Error())
}
if !reflect.DeepEqual(d, e) {
jd, _ := json.Marshal(d)
je, _ := json.Marshal(e)
panic("parsed data differs: \n" + string(jd) + "\n" + string(je) + "\n")
}
}
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by running the provided main program and trace hcl.Unmarshal and hcl.Decode with the nested match data. Compare the JSON produced by json.MarshalIndent with the decoded structure; done means decoding that JSON succeeds and preserves the original nested Data value.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, json
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.