Skip to content

Commit

Permalink
#12 🔥 add a lot of the code to the factory, add additional operator
Browse files Browse the repository at this point in the history
  • Loading branch information
thegodenage committed Mar 20, 2024
1 parent 0307412 commit 4de53b6
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 2 deletions.
6 changes: 6 additions & 0 deletions internal/rule/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ type CustomCompiler struct {

var _ Compiler = (*CustomCompiler)(nil)

func NewCustomCompiler() *CustomCompiler {
return &CustomCompiler{
builder: newPredicateBuilder(),
}
}

func (c *CustomCompiler) Compile(name, value string) (*Predicate, error) {
if err := validateInput(name, value); err != nil {
return nil, fmt.Errorf("validate name and value: %w", err)
Expand Down
78 changes: 76 additions & 2 deletions internal/rule/factory.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,85 @@
package rule

import (
"fmt"
"reflect"
)

var (
matches = []match{}

adjustableNodes = []reflect.Type{
reflect.TypeOf(gt{}),
}
)

type NodeAdjuster interface {
AdjustNode(nd node, tokensMatch []Token) (node, error)
}

type expressionTreeFactory struct {
nodeAdjuster NodeAdjuster
}

var _ ExpressionTreeFactory = (*expressionTreeFactory)(nil)

func (e *expressionTreeFactory) CreateExpressionTree(tokens []Token) (expressionTree, error) {
//TODO implement me
panic("implement me")
var (
nodes []node
tokenMatch []Token
)
for _, token := range tokens {
tokenMatch = append(tokenMatch, token)

for _, m := range matches {
if m.isMatching(tokenMatch) {
if isAdjustableNode(m.node) {
nd, err := e.nodeAdjuster.AdjustNode(m.node, tokenMatch)
if err != nil {
return nil, fmt.Errorf("adjust node %s: %w", reflect.TypeOf(m.node).Name(), err)
}

nodes = append(nodes, nd)

tokenMatch = []Token{}

continue
}

}
}
}

return nil, nil
}

type match struct {
tokens []Token
node node
}

func (m *match) isMatching(tokens []Token) bool {
if len(m.tokens) != len(tokens) {
return false
}

for i, token := range m.tokens {
if tokens[i].Name != token.Name {
return false
}
}

return true
}

func isAdjustableNode(nd node) bool {
nodeType := reflect.TypeOf(nd)

for _, adjustableNode := range adjustableNodes {
if nodeType.Name() == adjustableNode.Name() {
return true
}
}

return false
}
36 changes: 36 additions & 0 deletions internal/rule/factory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package rule

import "testing"

func Test_isAdjustableNode(t *testing.T) {
type args struct {
nd node
}
tests := []struct {
name string
args args
want bool
}{
{
name: "type isn't adjustable, false returned",
args: args{
nd: and{},
},
want: false,
},
{
name: "type is adjustable, true returned",
args: args{
nd: gt{},
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isAdjustableNode(tt.args.nd); got != tt.want {
t.Errorf("isAdjustableNode() = %v, want %v", got, tt.want)
}
})
}
}
9 changes: 9 additions & 0 deletions internal/rule/tokenizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var (
tokenMoreThan = ">"
tokenLessThan = "<"
tokenSingleApostrophe = "'"
tokenOr = "||"
specialCharacters = []string{
tokenLParen,
tokenRParen,
Expand All @@ -27,6 +28,14 @@ var (
tokenMoreThan,
tokenLessThan,
tokenSingleApostrophe,
tokenOr,
}

mathematicalOperators = []string{
tokenDoubleAmpersand,
tokenMoreThan,
tokenLessThan,
tokenOr,
}

methodLen = "LEN"
Expand Down

0 comments on commit 4de53b6

Please sign in to comment.