2017-05-18 14:57:04 +00:00
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
2019-09-09 23:08:19 +00:00
|
|
|
"github.com/hashicorp/hcl/v2"
|
2017-05-18 14:57:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Parse attempts to parse the given buffer as JSON and, if successful, returns
|
2018-01-27 18:51:26 +00:00
|
|
|
// a hcl.File for the HCL configuration represented by it.
|
2017-05-18 14:57:04 +00:00
|
|
|
//
|
|
|
|
// This is not a generic JSON parser. Instead, it deals only with the profile
|
2018-01-27 18:51:26 +00:00
|
|
|
// of JSON used to express HCL configuration.
|
2017-05-18 14:57:04 +00:00
|
|
|
//
|
2017-05-20 20:32:12 +00:00
|
|
|
// The returned file is valid only if the returned diagnostics returns false
|
|
|
|
// from its HasErrors method. If HasErrors returns true, the file represents
|
|
|
|
// the subset of data that was able to be parsed, which may be none.
|
2017-09-11 23:40:37 +00:00
|
|
|
func Parse(src []byte, filename string) (*hcl.File, hcl.Diagnostics) {
|
2017-05-18 14:57:04 +00:00
|
|
|
rootNode, diags := parseFileContent(src, filename)
|
2018-02-17 18:26:58 +00:00
|
|
|
|
|
|
|
switch rootNode.(type) {
|
|
|
|
case *objectVal, *arrayVal:
|
|
|
|
// okay
|
|
|
|
default:
|
2017-09-11 23:40:37 +00:00
|
|
|
diags = diags.Append(&hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
2017-05-18 14:57:04 +00:00
|
|
|
Summary: "Root value must be object",
|
2018-02-17 18:26:58 +00:00
|
|
|
Detail: "The root value in a JSON-based configuration must be either a JSON object or a JSON array of objects.",
|
2017-05-18 14:57:04 +00:00
|
|
|
Subject: rootNode.StartRange().Ptr(),
|
|
|
|
})
|
2018-02-17 18:26:58 +00:00
|
|
|
|
|
|
|
// Since we've already produced an error message for this being
|
|
|
|
// invalid, we'll return an empty placeholder here so that trying to
|
|
|
|
// extract content from our root body won't produce a redundant
|
|
|
|
// error saying the same thing again in more general terms.
|
2017-09-11 23:40:37 +00:00
|
|
|
fakePos := hcl.Pos{
|
2017-05-20 20:32:12 +00:00
|
|
|
Byte: 0,
|
|
|
|
Line: 1,
|
|
|
|
Column: 1,
|
|
|
|
}
|
2017-09-11 23:40:37 +00:00
|
|
|
fakeRange := hcl.Range{
|
2017-05-20 20:32:12 +00:00
|
|
|
Filename: filename,
|
|
|
|
Start: fakePos,
|
|
|
|
End: fakePos,
|
2017-05-18 14:57:04 +00:00
|
|
|
}
|
2017-05-20 20:32:12 +00:00
|
|
|
rootNode = &objectVal{
|
2018-02-17 18:26:58 +00:00
|
|
|
Attrs: []*objectAttr{},
|
2017-05-20 20:32:12 +00:00
|
|
|
SrcRange: fakeRange,
|
|
|
|
OpenRange: fakeRange,
|
|
|
|
}
|
|
|
|
}
|
2018-02-17 18:26:58 +00:00
|
|
|
|
2017-09-11 23:40:37 +00:00
|
|
|
file := &hcl.File{
|
2017-05-20 20:32:12 +00:00
|
|
|
Body: &body{
|
2018-02-17 18:26:58 +00:00
|
|
|
val: rootNode,
|
2017-05-20 20:32:12 +00:00
|
|
|
},
|
|
|
|
Bytes: src,
|
2018-02-17 18:26:58 +00:00
|
|
|
Nav: navigation{rootNode},
|
2017-05-18 14:57:04 +00:00
|
|
|
}
|
|
|
|
return file, diags
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseFile is a convenience wrapper around Parse that first attempts to load
|
|
|
|
// data from the given filename, passing the result to Parse if successful.
|
|
|
|
//
|
|
|
|
// If the file cannot be read, an error diagnostic with nil context is returned.
|
2017-09-11 23:40:37 +00:00
|
|
|
func ParseFile(filename string) (*hcl.File, hcl.Diagnostics) {
|
2017-05-18 14:57:04 +00:00
|
|
|
f, err := os.Open(filename)
|
|
|
|
if err != nil {
|
2017-09-11 23:40:37 +00:00
|
|
|
return nil, hcl.Diagnostics{
|
2017-05-18 14:57:04 +00:00
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Severity: hcl.DiagError,
|
2017-05-18 14:57:04 +00:00
|
|
|
Summary: "Failed to open file",
|
|
|
|
Detail: fmt.Sprintf("The file %q could not be opened.", filename),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
src, err := ioutil.ReadAll(f)
|
|
|
|
if err != nil {
|
2017-09-11 23:40:37 +00:00
|
|
|
return nil, hcl.Diagnostics{
|
2017-05-18 14:57:04 +00:00
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Severity: hcl.DiagError,
|
2017-05-18 14:57:04 +00:00
|
|
|
Summary: "Failed to read file",
|
|
|
|
Detail: fmt.Sprintf("The file %q was opened, but an error occured while reading it.", filename),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Parse(src, filename)
|
|
|
|
}
|