Skip to content

Commit 142cf61

Browse files
committed
adjuat-iten-walkier
Signed-off-by: yaacov <kobi.zamir@gmail.com>
1 parent c0875b9 commit 142cf61

File tree

7 files changed

+35
-47
lines changed

7 files changed

+35
-47
lines changed

v6/Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
# Compiler and tools
33
#------------------------------------------------------------------------------
44
GO = go
5+
GOLINT = $(shell which golangci-lint 2>/dev/null || echo $(HOME)/go/bin/golangci-lint)
56

67
#------------------------------------------------------------------------------
78
# Tools and flags
89
#------------------------------------------------------------------------------
9-
GOLINT = golangci-lint
1010
GO_TEST_FLAGS = -v -race
1111
GO_TEST_COVERAGE_FLAGS = -coverprofile=coverage.out
1212

@@ -47,6 +47,7 @@ help:
4747
@echo " format - Format the code using go fmt"
4848
@echo " clean - Remove built binary files and coverage output"
4949
@echo " clean-all - Remove all built and generated files including parser files"
50+
@echo " install-tools - Install development tools (golangci-lint)"
5051

5152
parser-gen:
5253
cd pkg/parser && ./build.sh
@@ -87,3 +88,6 @@ clean:
8788

8889
clean-all:
8990
rm -rf pkg/tsl/tsl_parser.tab.* pkg/tsl/lex.yy.c $(GO_BIN) $(GO_PARSER_BIN) $(GO_MEM_BIN) coverage.out
91+
92+
install-tools:
93+
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest

v6/cmd/tsl_mem/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func main() {
5757
defer tree.Free()
5858

5959
// Walk the TSL tree and replace identifiers.
60-
newTree, _, err := ident.Walk(tree, checkColumnName)
60+
newTree, err := ident.Walk(tree, checkColumnName)
6161
check(err)
6262
defer newTree.Free()
6363

v6/cmd/tsl_parser/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func main() {
4141

4242
// Setup the input.
4343
inputPtr := flag.String("i", "", "the tsl string to parse (e.g. \"title = 'kitty'\")")
44-
outputPtr := flag.String("o", "json", "output format [json/yaml/prettyjson/sql/dot]")
44+
outputPtr := flag.String("o", "json", "output format [json/yaml/sql/dot]")
4545
tablePtr := flag.String("t", "table_name", "table name for SQL output")
4646
flag.Parse()
4747

v6/pkg/parser/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ The parser code is automatically generated using the `go generate` command. The
1818

1919
To stop autogeneration, remove or comment this line from `pkg/tsl/tsl.go`.
2020

21+
```go
22+
// //go:generate bash ../parser/build.sh
23+
```
24+
2125
## Requirements
2226

2327
To generate the parser code, you need:

v6/pkg/tsl/tsl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

16-
////go:generate bash ../parser/build.sh
16+
// //go:generate bash ../parser/build.sh
1717

1818
package tsl
1919

v6/pkg/walkers/ident/walk.go

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -44,49 +44,39 @@ import (
4444
// }
4545
//
4646
// // If not found return string as is, and an error.
47-
// return s, fmt.Errorf("column not found")
47+
// return nil, fmt.Errorf("column not found")
4848
// }
4949
//
5050
// // Check and replace user identifiers with the SQL table column names,
51-
// // and get a list of all identifiers used in the tree.
51+
// // and return the new tree.
5252
// //
5353
// // If the input tree contains: `spec.pages > 100 AND author = "Joe"`,
54-
// // the identifiers list will contain: ["spec.pages", "author"]
55-
// //
5654
// // the new tree will contain: `pages > 100 AND author = "Joe"`
57-
// newTree, identifiers, err = ident.Walk(tree, check)
55+
// newTree, err = ident.Walk(tree, check)
5856
// defer newTree.Free()
59-
func Walk(n *tsl.TSLNode, check func(s string) (string, error)) (*tsl.TSLNode, []string, error) {
57+
func Walk(n *tsl.TSLNode, check func(s string) (string, error)) (*tsl.TSLNode, error) {
6058
if n == nil {
61-
return nil, nil, nil
59+
return nil, nil
6260
}
6361

6462
// Create a deep copy of the input tree to avoid mutations
6563
treeCopy := n.Clone()
6664
if treeCopy == nil {
67-
return nil, nil, fmt.Errorf("failed to clone input tree")
65+
return nil, fmt.Errorf("failed to clone input tree")
6866
}
6967

70-
identifiers := make(map[string]bool)
71-
newTree, err := walkAndReplace(treeCopy, check, identifiers)
68+
newTree, err := walkAndReplace(treeCopy, check)
7269
if err != nil {
7370
treeCopy.Free() // Clean up the copy if there's an error
74-
return nil, nil, err
75-
}
76-
77-
// Convert map to slice
78-
identList := make([]string, 0, len(identifiers))
79-
for ident := range identifiers {
80-
identList = append(identList, ident)
71+
return nil, err
8172
}
8273

83-
return newTree, identList, nil
74+
return newTree, nil
8475
}
8576

8677
// processIdentifier handles the common logic for processing identifier nodes
87-
func processIdentifier(n *tsl.TSLNode, identifiers map[string]bool, check func(s string) (string, error)) (*tsl.TSLNode, error) {
78+
func processIdentifier(n *tsl.TSLNode, check func(s string) (string, error)) (*tsl.TSLNode, error) {
8879
ident := n.Value().(string)
89-
identifiers[ident] = true
9080

9181
newIdent, err := check(ident)
9282
if err != nil {
@@ -96,7 +86,7 @@ func processIdentifier(n *tsl.TSLNode, identifiers map[string]bool, check func(s
9686
return tsl.ParseTSL(newIdent)
9787
}
9888

99-
func walkAndReplace(n *tsl.TSLNode, check func(s string) (string, error), identifiers map[string]bool) (*tsl.TSLNode, error) {
89+
func walkAndReplace(n *tsl.TSLNode, check func(s string) (string, error)) (*tsl.TSLNode, error) {
10090
if n == nil {
10191
return nil, nil
10292
}
@@ -108,27 +98,27 @@ func walkAndReplace(n *tsl.TSLNode, check func(s string) (string, error), identi
10898
// Process both sides of the binary expression
10999
left := op.Left
110100
if left.Type() == tsl.KindIdentifier {
111-
newNode, err := processIdentifier(left, identifiers, check)
101+
newNode, err := processIdentifier(left, check)
112102
if err != nil {
113103
return nil, err
114104
}
115105
n.AttachLeft(newNode)
116106
} else {
117-
_, err := walkAndReplace(left, check, identifiers)
107+
_, err := walkAndReplace(left, check)
118108
if err != nil {
119109
return nil, err
120110
}
121111
}
122112

123113
right := op.Right
124114
if right.Type() == tsl.KindIdentifier {
125-
newNode, err := processIdentifier(right, identifiers, check)
115+
newNode, err := processIdentifier(right, check)
126116
if err != nil {
127117
return nil, err
128118
}
129119
n.AttachRight(newNode)
130120
} else {
131-
_, err := walkAndReplace(right, check, identifiers)
121+
_, err := walkAndReplace(right, check)
132122
if err != nil {
133123
return nil, err
134124
}
@@ -142,13 +132,13 @@ func walkAndReplace(n *tsl.TSLNode, check func(s string) (string, error), identi
142132
// Process the operand
143133
right := op.Right
144134
if right.Type() == tsl.KindIdentifier {
145-
newNode, err := processIdentifier(right, identifiers, check)
135+
newNode, err := processIdentifier(right, check)
146136
if err != nil {
147137
return nil, err
148138
}
149139
n.AttachChild(newNode)
150140
} else {
151-
_, err := walkAndReplace(right, check, identifiers)
141+
_, err := walkAndReplace(right, check)
152142
if err != nil {
153143
return nil, err
154144
}
@@ -157,7 +147,7 @@ func walkAndReplace(n *tsl.TSLNode, check func(s string) (string, error), identi
157147
return n, nil
158148

159149
case tsl.KindIdentifier:
160-
return processIdentifier(n, identifiers, check)
150+
return processIdentifier(n, check)
161151

162152
default:
163153
return n, nil

v6/pkg/walkers/ident/walk_test.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package ident
1717

1818
import (
1919
"fmt"
20-
"sort"
2120
"testing"
2221

2322
. "github.com/onsi/ginkgo"
@@ -59,11 +58,9 @@ var _ = Describe("Walk", func() {
5958
Expect(err).ToNot(HaveOccurred())
6059
defer tree.Free()
6160

62-
newTree, idents, err := Walk(tree, check)
61+
newTree, err := Walk(tree, check)
6362
Expect(err).ToNot(HaveOccurred())
6463
defer newTree.Free()
65-
Expect(idents).To(HaveLen(1))
66-
Expect(idents).To(ContainElement("name"))
6764

6865
// Verify the replacement
6966
Expect(newTree.Type()).To(Equal(tsl.KindBinaryExpr))
@@ -76,11 +73,9 @@ var _ = Describe("Walk", func() {
7673
Expect(err).ToNot(HaveOccurred())
7774
defer tree.Free()
7875

79-
newTree, idents, err := Walk(tree, check)
76+
newTree, err := Walk(tree, check)
8077
Expect(err).ToNot(HaveOccurred())
8178
defer newTree.Free()
82-
sort.Strings(idents)
83-
Expect(idents).To(Equal([]string{"age", "city"}))
8479

8580
// Verify the replacement in the new tree
8681
op := newTree.Value().(tsl.TSLExpressionOp)
@@ -95,12 +90,9 @@ var _ = Describe("Walk", func() {
9590
Expect(err).ToNot(HaveOccurred())
9691
defer tree.Free()
9792

98-
newTree, idents, err := Walk(tree, check)
93+
newTree, err := Walk(tree, check)
9994
Expect(err).ToNot(HaveOccurred())
10095
defer newTree.Free()
101-
sort.Strings(idents)
102-
Expect(idents).To(Equal([]string{"age", "city", "country", "name"}))
103-
10496
// Tree structure should be preserved, just identifiers replaced
10597
Expect(newTree.Type()).To(Equal(tsl.KindBinaryExpr))
10698
})
@@ -110,7 +102,7 @@ var _ = Describe("Walk", func() {
110102
Expect(err).ToNot(HaveOccurred())
111103
defer tree.Free()
112104

113-
newTree, _, err := Walk(tree, check)
105+
newTree, err := Walk(tree, check)
114106
if err == nil {
115107
defer newTree.Free()
116108
}
@@ -123,11 +115,9 @@ var _ = Describe("Walk", func() {
123115
Expect(err).ToNot(HaveOccurred())
124116
defer tree.Free()
125117

126-
newTree, idents, err := Walk(tree, check)
118+
newTree, err := Walk(tree, check)
127119
Expect(err).ToNot(HaveOccurred())
128120
defer newTree.Free()
129-
sort.Strings(idents)
130-
Expect(idents).To(Equal([]string{"spec.pages", "spec.rating"}))
131121

132122
// Verify the replacements
133123
op := newTree.Value().(tsl.TSLExpressionOp)

0 commit comments

Comments
 (0)