Skip to content

Commit b2b095c

Browse files
committed
0.0.7 release
1 parent 30e1621 commit b2b095c

45 files changed

Lines changed: 240 additions & 92 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cmd/dfl.js/main.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ type Node struct {
3333
FunctionMap dfl.FunctionMap
3434
}
3535

36+
func (n Node) Pretty() string {
37+
return n.Node.Dfl(GO_DFL_DEFAULT_QUOTES[1:], true, 0)
38+
}
39+
3640
func (n Node) Compile() *js.Object {
3741
return js.MakeWrapper(Node{
3842
Node: n.Node.Compile(),
@@ -49,7 +53,7 @@ func (n Node) Evaluate(options *js.Object) interface{} {
4953

5054
result, err := n.Node.Evaluate(ctx, n.FunctionMap, GO_DFL_DEFAULT_QUOTES[1:])
5155
if err != nil {
52-
console.Log(err.Error())
56+
console.Error(err.Error())
5357
return false
5458
}
5559
return result
@@ -91,7 +95,7 @@ func EvaluateBool(s string, options *js.Object) bool {
9195

9296
result, err := dfl.EvaluateBool(root, ctx, dfl.NewFuntionMapWithDefaults(), GO_DFL_DEFAULT_QUOTES[1:])
9397
if err != nil {
94-
console.Log(err.Error())
98+
console.Error(err.Error())
9599
return false
96100
}
97101

@@ -101,7 +105,7 @@ func EvaluateBool(s string, options *js.Object) bool {
101105
func EvaluateInt(s string, options *js.Object) int {
102106
root, err := dfl.Parse(s)
103107
if err != nil {
104-
console.Log(err.Error())
108+
console.Error(err.Error())
105109
return 0
106110
}
107111

@@ -114,7 +118,7 @@ func EvaluateInt(s string, options *js.Object) int {
114118

115119
result, err := dfl.EvaluateInt(root, ctx, dfl.NewFuntionMapWithDefaults(), GO_DFL_DEFAULT_QUOTES[1:])
116120
if err != nil {
117-
console.Log(err.Error())
121+
console.Error(err.Error())
118122
return 0
119123
}
120124

@@ -124,7 +128,7 @@ func EvaluateInt(s string, options *js.Object) int {
124128
func EvaluateFloat64(s string, options *js.Object) float64 {
125129
root, err := dfl.Parse(s)
126130
if err != nil {
127-
console.Log(err.Error())
131+
console.Error(err.Error())
128132
return 0.0
129133
}
130134

@@ -137,7 +141,7 @@ func EvaluateFloat64(s string, options *js.Object) float64 {
137141

138142
result, err := dfl.EvaluateFloat64(root, ctx, dfl.NewFuntionMapWithDefaults(), GO_DFL_DEFAULT_QUOTES[1:])
139143
if err != nil {
140-
console.Log(err.Error())
144+
console.Error(err.Error())
141145
return 0.0
142146
}
143147

@@ -147,7 +151,7 @@ func EvaluateFloat64(s string, options *js.Object) float64 {
147151
func EvaluateString(s string, options *js.Object) string {
148152
root, err := dfl.Parse(s)
149153
if err != nil {
150-
console.Log(err.Error())
154+
console.Error(err.Error())
151155
return ""
152156
}
153157

@@ -158,9 +162,9 @@ func EvaluateString(s string, options *js.Object) string {
158162
ctx[key] = options.Get(key).Interface()
159163
}
160164

161-
result, err := dfl.EvaluateString(root, ctx, dfl.NewFuntionMapWithDefaults() GO_DFL_DEFAULT_QUOTES[1:])
165+
result, err := dfl.EvaluateString(root, ctx, dfl.NewFuntionMapWithDefaults(), GO_DFL_DEFAULT_QUOTES[1:])
162166
if err != nil {
163-
console.Log(err.Error())
167+
console.Error(err.Error())
164168
return ""
165169
}
166170

cmd/dfl/main.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
package main
2727

2828
import (
29-
//"bufio"
30-
//"bytes"
3129
"flag"
3230
"fmt"
3331
"os"
@@ -55,9 +53,11 @@ func main() {
5553
var load_env bool
5654
var verbose bool
5755
var version bool
56+
var pretty bool
5857
var help bool
5958

6059
flag.StringVar(&filter_text, "f", "", "The DFL expression to evaulate")
60+
flag.BoolVar(&pretty, "pretty", false, "Prints pretty version of expression to stdout")
6161

6262
flag.BoolVar(&load_env, "env", false, "Load environment variables")
6363
flag.BoolVar(&verbose, "verbose", false, "Provide verbose output")
@@ -123,7 +123,12 @@ func main() {
123123
os.Exit(1)
124124
}
125125

126+
if pretty {
127+
fmt.Println("# Pretty Version \n" + root.Dfl(dfl.DefaultQuotes[1:], true, 0) + "\n")
128+
}
129+
126130
if verbose {
131+
127132
fmt.Println("******************* Context *******************")
128133
out, err := yaml.Marshal(ctx)
129134
if err != nil {
@@ -132,17 +137,16 @@ func main() {
132137
os.Exit(1)
133138
}
134139
fmt.Println(string(out))
135-
}
136140

137-
if verbose {
138141
fmt.Println("******************* Parsed *******************")
139-
out, err := yaml.Marshal(root.Map())
142+
out, err = yaml.Marshal(root.Map())
140143
if err != nil {
141144
fmt.Println("Error marshaling expression to yaml.")
142145
fmt.Println(err)
143146
os.Exit(1)
144147
}
145148
fmt.Println(string(out))
149+
146150
}
147151

148152
root = root.Compile()
@@ -157,7 +161,7 @@ func main() {
157161
}
158162
fmt.Println(string(out))
159163

160-
fmt.Println(GO_DFL_DEFAULT_QUOTES[0] + root.Dfl(GO_DFL_DEFAULT_QUOTES[1:]) + GO_DFL_DEFAULT_QUOTES[0])
164+
fmt.Println(GO_DFL_DEFAULT_QUOTES[0] + root.Dfl(GO_DFL_DEFAULT_QUOTES[1:], false, 0) + GO_DFL_DEFAULT_QUOTES[0])
161165
}
162166

163167
result, err := root.Evaluate(ctx, dfl.NewFuntionMapWithDefaults(), GO_DFL_DEFAULT_QUOTES[1:])

dfl/Add.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ type Add struct {
1313
}
1414

1515
// Dfl returns the DFL representation of this node as a string
16-
func (a Add) Dfl(quotes []string, pretty bool) string {
17-
return "(" + a.Left.Dfl(quotes, pretty) + " + " + a.Right.Dfl(quotes, pretty) + ")"
16+
func (a Add) Dfl(quotes []string, pretty bool, tabs int) string {
17+
return a.BinaryOperator.Dfl("+", quotes, pretty, tabs)
1818
}
1919

2020
// Map returns a map representation of this node

dfl/After.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ type After struct {
1313
*TemporalBinaryOperator // Extends the TemporalBinaryOperator struct
1414
}
1515

16-
func (a After) Dfl(quotes []string, pretty bool) string {
17-
return "(" + a.Left.Dfl(quotes, pretty) + " after " + a.Right.Dfl(quotes, pretty) + ")"
16+
func (a After) Dfl(quotes []string, pretty bool, tabs int) string {
17+
return a.BinaryOperator.Dfl("after", quotes, pretty, tabs)
1818
}
1919

2020
func (a After) Map() map[string]interface{} {

dfl/And.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ type And struct {
1616
*BinaryOperator
1717
}
1818

19-
func (a And) Dfl(quotes []string, pretty bool) string {
20-
return "(" + a.Left.Dfl(quotes, pretty) + " and " + a.Right.Dfl(quotes, pretty) + ")"
19+
func (a And) Dfl(quotes []string, pretty bool, tabs int) string {
20+
return a.BinaryOperator.Dfl("and", quotes, pretty, tabs)
2121
}
2222

2323
func (a And) Map() map[string]interface{} {
@@ -69,5 +69,5 @@ func (a And) Evaluate(ctx interface{}, funcs FunctionMap, quotes []string) (inte
6969
return rv.(bool), nil
7070
}
7171
}
72-
return false, errors.New("Error evaluating expression " + a.Dfl(quotes, false))
72+
return false, errors.New("Error evaluating expression " + a.Dfl(quotes, false, 0))
7373
}

dfl/Array.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ type Array struct {
1616
Nodes []Node
1717
}
1818

19-
func (a Array) Dfl(quotes []string, pretty bool) string {
19+
func (a Array) Dfl(quotes []string, pretty bool, tabs int) string {
2020
str := "["
2121
for i, x := range a.Nodes {
2222
if i > 0 {
2323
str += ", "
2424
}
25-
str += x.Dfl(quotes, pretty)
25+
str += x.Dfl(quotes, pretty, tabs)
2626
}
2727
str = str + "]"
2828
return str

dfl/Attribute.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,21 @@
77

88
package dfl
99

10+
import (
11+
"strings"
12+
)
13+
1014
// Attribute is a Node representing the value of an attribute in the context map.
1115
// Attributes start with a "@" and follow with the name or full path into the object if multiple levels deep.
1216
// For example, @a and @a.b.c.d. You can also use a null-safe operator, e.g., @a?.b?.c?.d
1317
type Attribute struct {
1418
Name string
1519
}
1620

17-
func (a Attribute) Dfl(quotes []string, pretty bool) string {
21+
func (a Attribute) Dfl(quotes []string, pretty bool, tabs int) string {
22+
if pretty {
23+
return strings.Repeat(" ", tabs) + "@" + a.Name
24+
}
1825
return "@" + a.Name
1926
}
2027

dfl/Before.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ type Before struct {
1313
*TemporalBinaryOperator // Extends the TemporalBinaryOperator struct
1414
}
1515

16-
func (b Before) Dfl(quotes []string, pretty bool) string {
17-
return "(" + b.Left.Dfl(quotes, pretty) + " before " + b.Right.Dfl(quotes, pretty) + ")"
16+
func (b Before) Dfl(quotes []string, pretty bool, tabs int) string {
17+
return b.BinaryOperator.Dfl("before", quotes, pretty, tabs)
1818
}
1919

2020
func (b Before) Map() map[string]interface{} {

dfl/BinaryOperator.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,38 @@
77

88
package dfl
99

10+
import (
11+
"strings"
12+
)
13+
1014
// BinaryOperator is a DFL Node that represents the binary operator of a left value and right value.
1115
// This struct functions as an embedded struct for many comparator operations.
1216
type BinaryOperator struct {
1317
Left Node
1418
Right Node
1519
}
1620

21+
func (bo BinaryOperator) Dfl(operator string, quotes []string, pretty bool, tabs int) string {
22+
if pretty {
23+
switch bo.Left.(type) {
24+
case *Literal:
25+
switch bo.Left.(*Literal).Value.(type) {
26+
case string, int, []byte, Null:
27+
return strings.Repeat(" ", tabs) + "(" + bo.Left.Dfl(quotes, false, tabs) + " " + operator + " " + bo.Right.Dfl(quotes, false, tabs) + ")"
28+
}
29+
}
30+
switch bo.Right.(type) {
31+
case *Literal:
32+
switch bo.Right.(*Literal).Value.(type) {
33+
case string, int, []byte, Null:
34+
return strings.Repeat(" ", tabs) + "(" + bo.Left.Dfl(quotes, false, tabs) + " " + operator + " " + bo.Right.Dfl(quotes, false, tabs) + ")"
35+
}
36+
}
37+
return strings.Repeat(" ", tabs) + "(\n" + bo.Left.Dfl(quotes, pretty, tabs+1) + " " + operator + " " + "\n" + bo.Right.Dfl(quotes, pretty, tabs+1) + "\n" + strings.Repeat(" ", tabs) + ")"
38+
}
39+
return "(" + bo.Left.Dfl(quotes, pretty, tabs) + " " + operator + " " + bo.Right.Dfl(quotes, pretty, tabs) + ")"
40+
}
41+
1742
// EvaluateLeftAndRight evaluates the value of the left node and right node given a context map (ctx) and function map (funcs).
1843
// Returns a 3 value tuple of left value, right value, and error.
1944
func (bo BinaryOperator) EvaluateLeftAndRight(ctx interface{}, funcs FunctionMap, quotes []string) (interface{}, interface{}, error) {

dfl/Coalesce.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ type Coalesce struct {
1616
*BinaryOperator
1717
}
1818

19-
func (c Coalesce) Dfl(quotes []string, pretty bool) string {
20-
return "(" + c.Left.Dfl(quotes, pretty) + " ?: " + c.Right.Dfl(quotes, pretty) + ")"
19+
func (c Coalesce) Dfl(quotes []string, pretty bool, tabs int) string {
20+
return "(" + c.Left.Dfl(quotes, pretty, tabs) + " ?: " + c.Right.Dfl(quotes, pretty, tabs) + ")"
2121
}
2222

2323
func (c Coalesce) Map() map[string]interface{} {
@@ -44,14 +44,14 @@ func (c Coalesce) Compile() Node {
4444
func (c Coalesce) Evaluate(ctx interface{}, funcs FunctionMap, quotes []string) (interface{}, error) {
4545
lv, err := c.Left.Evaluate(ctx, funcs, quotes)
4646
if err != nil {
47-
return lv, errors.Wrap(err, "Error evaluating left value of coalesce: "+c.Left.Dfl(quotes, false))
47+
return lv, errors.Wrap(err, "Error evaluating left value of coalesce: "+c.Left.Dfl(quotes, false, 0))
4848
}
4949

5050
switch lv.(type) {
5151
case Null:
5252
rv, err := c.Right.Evaluate(ctx, funcs, quotes)
5353
if err != nil {
54-
return rv, errors.Wrap(err, "Error evaluating right value of Coalesce: "+c.Left.Dfl(quotes, false))
54+
return rv, errors.Wrap(err, "Error evaluating right value of Coalesce: "+c.Left.Dfl(quotes, false, 0))
5555
}
5656
return rv, nil
5757
}

0 commit comments

Comments
 (0)