import (
"encoding/json"
"fmt"
- "math/big"
"github.com/hashicorp/hcl2/hcl"
+ "github.com/zclconf/go-cty/cty"
)
func parseFileContent(buf []byte, filename string) (node, hcl.Diagnostics) {
return wrapInvalid(nil, hcl.Diagnostics{
{
Severity: hcl.DiagError,
- Summary: "Missing attribute value",
+ Summary: "Missing JSON value",
Detail: "A JSON value must start with a brace, a bracket, a number, a string, or a keyword.",
Subject: &tok.Range,
},
if !ok {
return nil, diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
- Summary: "Invalid object attribute name",
- Detail: "A JSON object attribute name must be a string",
+ Summary: "Invalid object property name",
+ Detail: "A JSON object property name must be a string",
Subject: keyNode.StartRange().Ptr(),
})
}
// Possible confusion with native HCL syntax.
return nil, diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
- Summary: "Missing attribute value colon",
+ Summary: "Missing property value colon",
Detail: "JSON uses a colon as its name/value delimiter, not an equals sign.",
Subject: &colon.Range,
})
return nil, diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
- Summary: "Missing attribute value colon",
- Detail: "A colon must appear between an object attribute's name and its value.",
+ Summary: "Missing property value colon",
+ Detail: "A colon must appear between an object property's name and its value.",
Subject: &colon.Range,
})
}
return nil, diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Trailing comma in object",
- Detail: "JSON does not permit a trailing comma after the final attribute in an object.",
+ Detail: "JSON does not permit a trailing comma after the final property in an object.",
Subject: &comma.Range,
})
}
return nil, diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Missing attribute seperator comma",
- Detail: "A comma must appear between each attribute declaration in an object.",
+ Detail: "A comma must appear between each property definition in an object.",
Subject: p.Peek().Range.Ptr(),
})
}
return nil, diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Trailing comma in array",
- Detail: "JSON does not permit a trailing comma after the final attribute in an array.",
+ Detail: "JSON does not permit a trailing comma after the final value in an array.",
Subject: &comma.Range,
})
}
}
}
- f, _, err := big.ParseFloat(string(num), 10, 512, big.ToNearestEven)
+ // We want to guarantee that we parse numbers the same way as cty (and thus
+ // native syntax HCL) would here, so we'll use the cty parser even though
+ // in most other cases we don't actually introduce cty concepts until
+ // decoding time. We'll unwrap the parsed float immediately afterwards, so
+ // the cty value is just a temporary helper.
+ nv, err := cty.ParseNumberVal(string(num))
if err != nil {
// Should never happen if above passed, since JSON numbers are a subset
- // of what big.Float can parse...
+ // of what cty can parse...
return nil, hcl.Diagnostics{
{
Severity: hcl.DiagError,
}
return &numberVal{
- Value: f,
+ Value: nv.AsBigFloat(),
SrcRange: tok.Range,
}, nil
}