Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions pkg/dfl/Add.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,18 @@ func (a Add) Sql(pretty bool, tabs int) string {
return a.BinaryOperator.Sql("+", pretty, tabs)
}

// Map returns a map representation of this node.
func (a Add) Map() map[string]interface{} {
return a.BinaryOperator.Map("+", a.Left, a.Right)
return map[string]interface{}{
"@type": "+",
"@value": map[string]interface{}{
"left": a.BinaryOperator.Left.Map(),
"right": a.BinaryOperator.Right.Map(),
},
}
}

func (a Add) MarshalMap() (interface{}, error) {
return a.Map(), nil
}

// Compile returns a compiled version of this node.
Expand Down
9 changes: 8 additions & 1 deletion pkg/dfl/Array.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,17 @@ func (a Array) Sql(pretty bool, tabs int) string {

func (a Array) Map() map[string]interface{} {
return map[string]interface{}{
"nodes": a.Nodes,
"@type": "array",
"@value": map[string]interface{}{
"nodes": a.Nodes,
},
}
}

func (a Array) MarshalMap() (interface{}, error) {
return a.Map(), nil
}

// Compile returns a compiled version of this node.
// If all the values of an Set are literals, returns a single Literal with the corresponding array as its value.
// Otherwise returns the original node..
Expand Down
12 changes: 11 additions & 1 deletion pkg/dfl/Assign.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,17 @@ func (a Assign) Sql(pretty bool, tabs int) string {
}

func (a Assign) Map() map[string]interface{} {
return a.BinaryOperator.Map("assign", a.Left, a.Right)
return map[string]interface{}{
"@type": ":=",
"@value": map[string]interface{}{
"left": a.BinaryOperator.Left.Map(),
"right": a.BinaryOperator.Right.Map(),
},
}
}

func (a Assign) MarshalMap() (interface{}, error) {
return a.Map(), nil
}

// Compile returns a compiled version of this node.
Expand Down
25 changes: 19 additions & 6 deletions pkg/dfl/AssignAdd.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,32 @@ func (a AssignAdd) Sql(pretty bool, tabs int) string {
}

func (a AssignAdd) Map() map[string]interface{} {
return a.BinaryOperator.Map("assignadd", a.Left, a.Right)
return map[string]interface{}{
"@type": "+=",
"@value": map[string]interface{}{
"left": a.BinaryOperator.Left.Map(),
"right": a.BinaryOperator.Right.Map(),
},
}
}

func (a AssignAdd) MarshalMap() (interface{}, error) {
return a.Map(), nil
}

// Compile returns a compiled version of this node.
func (a AssignAdd) Compile() Node {
left := a.Left.Compile()
right := a.Right.Compile()
return &AssignAdd{&BinaryOperator{Left: left, Right: right}}
return &AssignAdd{
&BinaryOperator{
Left: a.Left.Compile(),
Right: a.Right.Compile(),
},
}
}

func (a AssignAdd) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error) {
switch left := a.Left.(type) {
case Attribute:
case *Attribute:
vars, lv, rv, err := a.EvaluateLeftAndRight(vars, ctx, funcs, quotes)
if err != nil {
return vars, 0, err
Expand Down Expand Up @@ -110,7 +123,7 @@ func (a AssignAdd) Evaluate(vars map[string]interface{}, ctx interface{}, funcs
path = pair[1]
}
return vars, ctx, nil
case Variable:
case *Variable:
vars, lv, rv, err := a.EvaluateLeftAndRight(vars, ctx, funcs, quotes)
if err != nil {
return vars, 0, err
Expand Down
4 changes: 2 additions & 2 deletions pkg/dfl/AssignMultiply.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (a AssignMultiply) Compile() Node {

func (a AssignMultiply) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error) {
switch left := a.Left.(type) {
case Attribute:
case *Attribute:
vars, lv, rv, err := a.EvaluateLeftAndRight(vars, ctx, funcs, quotes)
if err != nil {
return vars, 0, err
Expand Down Expand Up @@ -110,7 +110,7 @@ func (a AssignMultiply) Evaluate(vars map[string]interface{}, ctx interface{}, f
path = pair[1]
}
return vars, ctx, nil
case Variable:
case *Variable:
vars, lv, rv, err := a.EvaluateLeftAndRight(vars, ctx, funcs, quotes)
if err != nil {
return vars, 0, err
Expand Down
4 changes: 2 additions & 2 deletions pkg/dfl/AssignSubtract.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (a AssignSubtract) Compile() Node {

func (a AssignSubtract) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error) {
switch left := a.Left.(type) {
case Attribute:
case *Attribute:
vars, lv, rv, err := a.EvaluateLeftAndRight(vars, ctx, funcs, quotes)
if err != nil {
return vars, 0, err
Expand Down Expand Up @@ -107,7 +107,7 @@ func (a AssignSubtract) Evaluate(vars map[string]interface{}, ctx interface{}, f
path = pair[1]
}
return vars, ctx, nil
case Variable:
case *Variable:
vars, lv, rv, err := a.EvaluateLeftAndRight(vars, ctx, funcs, quotes)
if err != nil {
return vars, 0, err
Expand Down
11 changes: 9 additions & 2 deletions pkg/dfl/Attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,19 @@ func (a Attribute) Sql(pretty bool, tabs int) string {

func (a Attribute) Map() map[string]interface{} {
return map[string]interface{}{
"attribute": syntax.AttributePrefix + a.Name,
"@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}
return &Attribute{Name: a.Name}
}

func (a Attribute) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/dfl/BinaryOperator.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
// BinaryOperator is a DFL Node that represents the binary operator of a left value and right value.
// This struct functions as an embedded struct for many comparator operations.
type BinaryOperator struct {
Left Node
Right Node
Left Node `map:"left"`
Right Node `map:"right"`
}

func (bo BinaryOperator) Builder(operator string, quotes []string, tabs int) builder.Builder {
Expand Down
70 changes: 44 additions & 26 deletions pkg/dfl/Dictionary.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,40 @@ import (

// Dictionary is a Node representing a dictionary of key value pairs.
type Dictionary struct {
Nodes map[Node]Node
//Nodes map[Node]Node
Items []Item
}

func NewDictionary(m map[string]interface{}) *Dictionary {
nodes := map[Node]Node{}
items := make([]Item, 0)
for k, v := range m {
if d, ok := v.(map[string]interface{}); ok {
nodes[&Literal{Value: k}] = NewDictionary(d)
items = append(items, Item{
Key: &Literal{Value: k},
Value: NewDictionary(d),
})
} else {
nodes[&Literal{Value: k}] = &Literal{Value: v}
items = append(items, Item{
Key: &Literal{Value: k},
Value: &Literal{Value: v},
})
}
}
return &Dictionary{Nodes: nodes}
return &Dictionary{Items: items}
}

// Len returns the length of the underlying array.
func (d Dictionary) Len() int {
return len(d.Nodes)
return len(d.Items)
}

func (d Dictionary) Dfl(quotes []string, pretty bool, tabs int) string {
if len(d.Nodes) == 0 {
if len(d.Items) == 0 {
return "{}"
}
values := make([]string, 0)
for k, v := range d.Nodes {
values = append(values, k.Dfl(quotes, pretty, tabs+1)+": "+v.Dfl(quotes, pretty, tabs+1))
for _, i := range d.Items {
values = append(values, i.Key.Dfl(quotes, pretty, tabs+1)+": "+i.Value.Dfl(quotes, pretty, tabs+1))
}
if pretty {
return "{" + "\n" + FormatList(values, ",", pretty, tabs+1) + "\n" + strings.Repeat(DefaultTab, tabs) + "}"
Expand All @@ -51,41 +58,52 @@ func (d Dictionary) Dfl(quotes []string, pretty bool, tabs int) string {
// Sql returns the SQL representation of this node as a string
func (d Dictionary) Sql(pretty bool, tabs int) string {
str := SqlQuote + SqlArrayPrefix
i := 0
for k, v := range d.Nodes {
for i, item := range d.Items {
if i > 0 {
str += ", "
}
str += k.Sql(pretty, tabs) + ":" + v.Sql(pretty, tabs)
str += item.Key.Sql(pretty, tabs) + ":" + item.Value.Sql(pretty, tabs)
i += 1
}
str = str + SqlArraySuffix + SqlQuote + "::json"
return str
}

func (d Dictionary) Map() map[string]interface{} {
items := []map[string]interface{}{}
for _, item := range d.Items {
items = append(items, item.Map())
}
return map[string]interface{}{
"nodes": d.Nodes,
"@type": "dictionary",
"@value": items,
}
}

func (d Dictionary) MarshalMap() (interface{}, error) {
return d.Map(), nil
}

// Compile returns a compiled version of this node.
func (d Dictionary) Compile() Node {
nodes := map[Node]Node{}
for k, v := range d.Nodes {
nodes[k.Compile()] = v.Compile()
items := make([]Item, 0)
for _, i := range d.Items {
items = append(items, Item{
Key: i.Key.Compile(),
Value: i.Value.Compile(),
})
}
return Dictionary{Nodes: nodes}
return &Dictionary{Items: items}
}

func (d Dictionary) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error) {
values := map[interface{}]interface{}{}
for k, v := range d.Nodes {
_, keyValue, err := k.Evaluate(vars, ctx, funcs, quotes)
for _, i := range d.Items {
_, keyValue, err := i.Key.Evaluate(vars, ctx, funcs, quotes)
if err != nil {
return vars, values, err
}
_, valueValue, err := v.Evaluate(vars, ctx, funcs, quotes)
_, valueValue, err := i.Value.Evaluate(vars, ctx, funcs, quotes)
if err != nil {
return vars, values, err
}
Expand All @@ -96,11 +114,11 @@ func (d Dictionary) Evaluate(vars map[string]interface{}, ctx interface{}, funcs

func (d Dictionary) Attributes() []string {
set := make(map[string]struct{})
for k, v := range d.Nodes {
for _, x := range k.Attributes() {
for _, i := range d.Items {
for _, x := range i.Key.Attributes() {
set[x] = struct{}{}
}
for _, x := range v.Attributes() {
for _, x := range i.Value.Attributes() {
set[x] = struct{}{}
}
}
Expand All @@ -113,11 +131,11 @@ func (d Dictionary) Attributes() []string {

func (d Dictionary) Variables() []string {
set := make(map[string]struct{})
for k, v := range d.Nodes {
for _, x := range k.Variables() {
for _, i := range d.Items {
for _, x := range i.Key.Variables() {
set[x] = struct{}{}
}
for _, x := range v.Variables() {
for _, x := range i.Value.Variables() {
set[x] = struct{}{}
}
}
Expand Down
7 changes: 6 additions & 1 deletion pkg/dfl/ErrorEvaluate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

package dfl

import (
"fmt"
)

// ErrorEvaluate is an error returned when an error occurs during evaluation of a Node.
type ErrorEvaluate struct {
Node Node // the name of the Function
Expand All @@ -15,5 +19,6 @@ type ErrorEvaluate struct {

// Error returns the error as a string.
func (e ErrorEvaluate) Error() string {
return "error evaluating expression " + e.Node.Dfl(e.Quotes, false, 0)
//return fmt.Sprintf("error evaluating %s (%q)", Name(e.Node), e.Node.Dfl(e.Quotes, false, 0))
return fmt.Sprintf("error evaluating %s (%#v)", Name(e.Node), e.Node.Map())
}
2 changes: 1 addition & 1 deletion pkg/dfl/FormatSql.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func FormatSql(value interface{}, pretty bool, tabs int) string {
// Just format sets as slices.
return FormatSql(value.Slice(true), pretty, tabs)
case Null:
return value.Sql()
return value.Sql(pretty, tabs)
}

return fmt.Sprint(value)
Expand Down
19 changes: 15 additions & 4 deletions pkg/dfl/Function.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,12 @@ func (f Function) Sql(pretty bool, tabs int) string {
}

func (f Function) Compile() Node {
return f
return &Function{
Name: f.Name,
MultiOperator: &MultiOperator{
Arguments: f.MultiOperator.Arguments,
},
}
}

func (f Function) Map() map[string]interface{} {
Expand All @@ -89,12 +94,18 @@ func (f Function) Map() map[string]interface{} {
arguments = append(arguments, a.Map())
}
return map[string]interface{}{
"op": "function",
"name": f.Name,
"arguments": arguments,
"@type": "function",
"@value": map[string]interface{}{
"name": f.Name,
"arguments": arguments,
},
}
}

func (f Function) MarshalMap() (interface{}, error) {
return f.Map(), nil
}

func (f Function) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error) {
if fn, ok := funcs[f.Name]; ok {
values := make([]interface{}, 0, len(f.Arguments))
Expand Down
Loading