-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#12 🔥 add a lot of the code to the factory, add additional operator
- Loading branch information
1 parent
0307412
commit 4de53b6
Showing
4 changed files
with
127 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters