Skip to content

Commit

Permalink
handle nils in mem example
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacov committed Feb 12, 2019
1 parent 1d63f4b commit 92ca002
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 15 deletions.
22 changes: 16 additions & 6 deletions cmd/tsl_mem/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,22 @@ var Books = []Book{}
func prepareCollection() (err error) {
// Insert new books into the table.
for _, b := range model.Books {
Books = append(Books, Book{
"title": b.(model.Book).Title,
"author": b.(model.Book).Author,
"spec.pages": b.(model.Book).Spec.Pages,
"spec.rating": b.(model.Book).Spec.Rating,
})
// Create a new book.
newBook := Book{
"title": b.(model.Book).Title,
"author": b.(model.Book).Author,
}

// Add optional parameters.
if b.(model.Book).Spec.Pages > 0 {
newBook["spec.pages"] = b.(model.Book).Spec.Pages
}
if b.(model.Book).Spec.Rating > 0 {
newBook["spec.rating"] = b.(model.Book).Spec.Rating
}

// Insert new book to the books arra.
Books = append(Books, newBook)
}

return
Expand Down
95 changes: 86 additions & 9 deletions cmd/tsl_mem/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ func handleIdent(n tsl.Node, book Book) tsl.Node {
Func: tsl.StringOp,
Left: book[l.Left.(string)].(string),
}
case float64:
n.Left = tsl.Node{
Func: tsl.StringOp,
Left: book[l.Left.(string)].(float64),
}
case int:
n.Left = tsl.Node{
Func: tsl.NumberOp,
Expand All @@ -46,19 +51,56 @@ func handleIdent(n tsl.Node, book Book) tsl.Node {
default:
n.Left = tsl.Node{
Func: tsl.NumberOp,
Left: book[l.Left.(string)].(float64),
Left: nil,
}
}

return n
}

func handleNullOp(n tsl.Node, book Book) (bool, error) {
var left interface{}
var right interface{}

r, ok := n.Right.(tsl.Node)
if ok {
right = r.Left
} else {
right = nil
}
l, ok := n.Left.(tsl.Node)
if ok {
left = l.Left
} else {
left = nil
}

switch n.Func {
case tsl.EqOp:
return left == right, nil
case tsl.NotEqOp:
return left != right, nil
case tsl.IsNilOp:
return left == nil, nil
case tsl.IsNotNilOp:
return left != nil, nil
}

return false, fmt.Errorf("not supported null operator")
}

func handleStringOp(n tsl.Node, book Book) (bool, error) {
l := n.Left.(tsl.Node)
r := n.Right.(tsl.Node)

left := l.Left.(string)
right := r.Left.(string)
left, ok := l.Left.(string)
if !ok {
return handleNullOp(n, book)
}
right, ok := r.Left.(string)
if !ok {
return handleNullOp(n, book)
}

switch n.Func {
case tsl.EqOp:
Expand All @@ -73,6 +115,10 @@ func handleStringOp(n tsl.Node, book Book) (bool, error) {
return left > right, nil
case tsl.GteOp:
return left >= right, nil
case tsl.IsNilOp:
return false, nil
case tsl.IsNotNilOp:
return true, nil
case tsl.RegexOp:
var valid = regexp.MustCompile(right)
return valid.MatchString(left), nil
Expand All @@ -88,8 +134,14 @@ func handleNumberOp(n tsl.Node, book Book) (bool, error) {
l := n.Left.(tsl.Node)
r := n.Right.(tsl.Node)

left := l.Left.(float64)
right := r.Left.(float64)
left, ok := l.Left.(float64)
if !ok {
return handleNullOp(n, book)
}
right, ok := r.Left.(float64)
if !ok {
return handleNullOp(n, book)
}

switch n.Func {
case tsl.EqOp:
Expand All @@ -104,6 +156,10 @@ func handleNumberOp(n tsl.Node, book Book) (bool, error) {
return left > right, nil
case tsl.GteOp:
return left >= right, nil
case tsl.IsNilOp:
return false, nil
case tsl.IsNotNilOp:
return true, nil
}

return false, fmt.Errorf("not supported number operator")
Expand All @@ -113,8 +169,14 @@ func handleStringArrayOp(n tsl.Node, book Book) (bool, error) {
l := n.Left.(tsl.Node)
r := n.Right.(tsl.Node)

left := l.Left.(string)
right := r.Right.([]tsl.Node)
left, ok := l.Left.(string)
if !ok {
return handleNullOp(n, book)
}
right, ok := r.Right.([]tsl.Node)
if !ok {
return handleNullOp(n, book)
}

switch n.Func {
case tsl.BetweenOp:
Expand Down Expand Up @@ -146,8 +208,14 @@ func handleNumberArrayOp(n tsl.Node, book Book) (bool, error) {
l := n.Left.(tsl.Node)
r := n.Right.(tsl.Node)

left := l.Left.(float64)
right := r.Right.([]tsl.Node)
left, ok := l.Left.(float64)
if !ok {
return handleNullOp(n, book)
}
right, ok := r.Right.([]tsl.Node)
if !ok {
return handleNullOp(n, book)
}

switch n.Func {
case tsl.BetweenOp:
Expand Down Expand Up @@ -219,6 +287,15 @@ func Walk(n tsl.Node, book Book) (bool, error) {
return handleNumberArrayOp(n, book)
}
}
case tsl.IsNotNilOp, tsl.IsNilOp:
l := n.Left.(tsl.Node)

switch l.Func {
case tsl.IdentOp:
return Walk(handleIdent(n, book), book)
default:
return handleNullOp(n, book)
}
case tsl.AndOp, tsl.OrOp:
return handleLogicalOp(n, book)
}
Expand Down

0 comments on commit 92ca002

Please sign in to comment.