-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAttribute.go
More file actions
68 lines (56 loc) · 1.73 KB
/
Attribute.go
File metadata and controls
68 lines (56 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// =================================================================
//
// Copyright (C) 2019 Spatial Current, Inc. - All Rights Reserved
// Released as open source under the MIT License. See LICENSE file.
//
// =================================================================
package dfl
import (
"strings"
"github.com/spatialcurrent/go-dfl/pkg/dfl/syntax"
)
// Attribute is a Node representing the value of an attribute in the context map.
// Attributes start with a "@" and follow with the name or full path into the object if multiple levels deep.
// For example, @a and @a.b.c.d. You can also use a null-safe operator, e.g., @a?.b?.c?.d
type Attribute struct {
Name string
}
func (a Attribute) Dfl(quotes []string, pretty bool, tabs int) string {
if pretty {
return strings.Repeat(" ", tabs) + syntax.AttributePrefix + a.Name
}
return syntax.AttributePrefix + a.Name
}
func (a Attribute) Sql(pretty bool, tabs int) string {
if pretty {
return strings.Repeat(" ", tabs) + a.Name
}
return a.Name
}
func (a Attribute) Map() map[string]interface{} {
return map[string]interface{}{
"@type": "attribute",
"@value": map[string]interface{}{
"name": a.Name,
},
}
}
func (a Attribute) MarshalMap() (interface{}, error) {
return a.Map(), nil
}
func (a Attribute) Compile() Node {
return &Attribute{Name: a.Name}
}
func (a Attribute) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error) {
if len(a.Name) == 0 {
return vars, ctx, nil
}
ctx, err := Extract(a.Name, ctx, vars, ctx, funcs, quotes)
return vars, ctx, err
}
func (a Attribute) Attributes() []string {
return []string{a.Name}
}
func (a Attribute) Variables() []string {
return []string{}
}