8 // LiteralNode represents a single literal value, such as "foo" or
9 // 42 or 3.14159. Based on the Type, the Value can be safely cast.
10 type LiteralNode struct {
16 // NewLiteralNode returns a new literal node representing the given
17 // literal Go value, which must correspond to one of the primitive types
18 // supported by HIL. Lists and maps cannot currently be constructed via
21 // If an inappropriately-typed value is provided, this function will
22 // return an error. The main intended use of this function is to produce
23 // "synthetic" literals from constants in code, where the value type is
24 // well known at compile time. To easily store these in global variables,
25 // see also MustNewLiteralNode.
26 func NewLiteralNode(value interface{}, pos Pos) (*LiteralNode, error) {
27 goType := reflect.TypeOf(value)
30 switch goType.Kind() {
40 return nil, fmt.Errorf("unsupported literal node type: %T", value)
50 // MustNewLiteralNode wraps NewLiteralNode and panics if an error is
51 // returned, thus allowing valid literal nodes to be easily assigned to
53 func MustNewLiteralNode(value interface{}, pos Pos) *LiteralNode {
54 node, err := NewLiteralNode(value, pos)
61 func (n *LiteralNode) Accept(v Visitor) Node {
65 func (n *LiteralNode) Pos() Pos {
69 func (n *LiteralNode) GoString() string {
70 return fmt.Sprintf("*%#v", *n)
73 func (n *LiteralNode) String() string {
74 return fmt.Sprintf("Literal(%s, %v)", n.Typex, n.Value)
77 func (n *LiteralNode) Type(Scope) (Type, error) {
81 // IsUnknown returns true either if the node's value is itself unknown
82 // of if it is a collection containing any unknown elements, deeply.
83 func (n *LiteralNode) IsUnknown() bool {
84 return IsUnknown(Variable{