Skip to content

Commit 2ef09d1

Browse files
authored
Merge pull request #524 from hashicorp/f-json-is-json
json: Functions for determining if an expression or body is a JSON one
2 parents 8c86d68 + 8fc6794 commit 2ef09d1

1 file changed

Lines changed: 54 additions & 0 deletions

File tree

json/is.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package json
2+
3+
import (
4+
"github.com/hashicorp/hcl/v2"
5+
)
6+
7+
// IsJSONExpression returns true if and only if the given expression is one
8+
// that originated in a JSON document.
9+
//
10+
// Applications aiming to be syntax-agnostic should not use this function and
11+
// should instead use the normal expression evaluation or static analysis
12+
// APIs.
13+
//
14+
// However, JSON expressions do have a unique behavior whereby they interpret
15+
// the source JSON differently depending on the hcl.EvalContext value passed
16+
// to the Value method -- in particular, a nil hcl.EvalContext returns
17+
// literal strings rather than interpreting them as HCL template syntax --
18+
// and so in exceptional cases an application may wish to rely on that behavior
19+
// in situations where it specifically knows the expression originated in JSON,
20+
// in case it needs to do some non-standard handling of the expression in that
21+
// case.
22+
//
23+
// Caution: The normal HCL API allows for HCL expression implementations that
24+
// wrap other HCL expression implementations. This function will return false
25+
// if given an expression of some other type that encapsulates a JSON
26+
// expression, even if the wrapper implementation would in principle preserve
27+
// the special evaluation behavior of the wrapped expression.
28+
func IsJSONExpression(maybeJSONExpr hcl.Expression) bool {
29+
_, ok := maybeJSONExpr.(*expression)
30+
return ok
31+
}
32+
33+
// IsJSONBody returns true if and only if the given body is one that originated
34+
// in a JSON document.
35+
//
36+
// Applications aiming to be syntax-agnostic should not use this function and
37+
// should instead use the normal schema-driven or "just attributes' decoding
38+
// APIs.
39+
//
40+
// Howeer, JSON expressions do have a unique behavior whereby various different
41+
// source JSON shapes can be interpreted in different ways depending on the
42+
// given schema, and so in exceptional cases an application may need to
43+
// perform some deeper analysis first in order to distinguish variants of
44+
// different physical structure.
45+
//
46+
// Caution: The normal HCL API allows for HCL body implementations that wrap
47+
// other HCL body implementations. This function will return false if given an
48+
// expression of some other type that encapsulates a JSON body, even if
49+
// the wrapper implementation would in principle preserve the special
50+
// decoding behavior of the wrapped body.
51+
func IsJSONBody(maybeJSONBody hcl.Body) bool {
52+
_, ok := maybeJSONBody.(*body)
53+
return ok
54+
}

0 commit comments

Comments
 (0)