From 3ab215ed6300b93255211684cbc4e821e27b66c1 Mon Sep 17 00:00:00 2001 From: Vladimir Hodakov Date: Sat, 19 Oct 2019 01:00:37 +0400 Subject: [PATCH] Remove dead or unused code --- eval.go | 6 ++--- expression.go | 1 - expression_states.go | 12 +-------- lexer.go | 28 +------------------- lexer_test.go | 45 -------------------------------- path_states.go | 9 ------- path_test.go | 9 ++++--- queue.go | 4 +-- stack.go | 61 -------------------------------------------- 9 files changed, 12 insertions(+), 163 deletions(-) diff --git a/eval.go b/eval.go index 563b998..56203da 100644 --- a/eval.go +++ b/eval.go @@ -198,8 +198,8 @@ func pathMatchOp(q *query, e *Eval, i *Item) queryStateFn { if len(q.operators)+q.start >= curLocation { current, _ := e.location.peek() nextOp := q.operators[abs(q.loc()-q.start)] - if itemMatchOperator(current, i, nextOp) { - q.pos += 1 + if itemMatchOperator(current, nextOp) { + q.pos++ if nextOp.whereClauseBytes != nil && len(nextOp.whereClause) > 0 { bucket := exprBucket{ @@ -312,7 +312,7 @@ func (b *exprBucket) evaluate() (bool, error) { return res_bool, nil } -func itemMatchOperator(loc interface{}, i *Item, op *operator) bool { +func itemMatchOperator(loc interface{}, op *operator) bool { topBytes, isKey := loc.([]byte) topInt, isIndex := loc.(int) if isKey { diff --git a/expression.go b/expression.go index fd6d192..621ba13 100644 --- a/expression.go +++ b/expression.go @@ -16,7 +16,6 @@ const ( exprErrorValueNotFound = "Value for %q not found" exprErrorBadValue = "Bad value %q for type %q" exprErrorPathValueNotScalar = "Path value must be scalar value" - exprErrorBadOperandType = "Operand type expected to be %q for operation %q" ) type exprErrorBadTypeComparison struct { diff --git a/expression_states.go b/expression_states.go index 81ab06d..0fdf167 100644 --- a/expression_states.go +++ b/expression_states.go @@ -11,7 +11,6 @@ const ( exprNull exprString - exprOperators exprOpEq exprOpNeq exprOpNot @@ -251,7 +250,7 @@ func takeNumeric(l lexer) { func takePath(l lexer) { inQuotes := false - var prev int = 0 + prev := 0 // capture until end of path - ugly takeLoop: for { @@ -276,12 +275,3 @@ takeLoop: prev = cur } } - -func lexExprEnd(l lexer, state *intStack) stateFn { - cur := l.take() - if cur != eof { - return l.errorf("Expected EOF but received %#U", cur) - } - l.emit(exprEOF) - return nil -} diff --git a/lexer.go b/lexer.go index bc7aebf..2cd995a 100644 --- a/lexer.go +++ b/lexer.go @@ -33,6 +33,7 @@ type lexer interface { reset() } +// nolint:structcheck type lex struct { initialState stateFn currentStateFn stateFn @@ -50,33 +51,6 @@ func newLex(initial stateFn) lex { } } -func (i *Item) clone() *Item { - ic := Item{ - typ: i.typ, - pos: i.pos, - val: make([]byte, len(i.val)), - } - copy(ic.val, i.val) - return &ic -} - -func itemsDescription(items []Item, nameMap map[int]string) []string { - vals := make([]string, len(items)) - for i, item := range items { - vals[i] = itemDescription(&item, nameMap) - } - return vals -} - -func itemDescription(item *Item, nameMap map[int]string) string { - var found bool - val, found := nameMap[item.typ] - if !found { - return string(item.val) - } - return val -} - func typesDescription(types []int, nameMap map[int]string) []string { vals := make([]string, len(types)) for i, val := range types { diff --git a/lexer_test.go b/lexer_test.go index 6627f76..b5bbfe2 100644 --- a/lexer_test.go +++ b/lexer_test.go @@ -3,9 +3,7 @@ package jsonpath import ( "bytes" "encoding/json" - "fmt" "io/ioutil" - "math" "os" "strings" "testing" @@ -207,21 +205,6 @@ func benchmarkStdUnmarshal(input []byte, b *testing.B) { } } -func benchmarkStdUnmarshalJSONLarge(b *testing.B) { - input, err := ioutil.ReadFile("large.test") - if err != nil { - b.SkipNow() - } - b.ResetTimer() - for n := 0; n < b.N; n++ { - var x interface{} - err := json.Unmarshal(input, &x) - if err != nil { - b.Error(err) - } - } -} - func BenchmarkStdUnmarshalLarge(b *testing.B) { input, err := ioutil.ReadFile("large.test") if err != nil { @@ -279,31 +262,3 @@ type lexTest struct { input string tokenTypes []int } - -func i(tokenType int) Item { - return Item{tokenType, 0, []byte{}} -} - -func typeIsEqual(i1, i2 []Item, printError bool) bool { - for k := 0; k < int(math.Max(float64(len(i1)), float64(len(i2)))); k++ { - if k < len(i1) { - if i1[k].typ == jsonError && printError { - fmt.Println(string(i1[k].val)) - } - } else if k < len(i2) { - if i2[k].typ == jsonError && printError { - fmt.Println(string(i2[k].val)) - } - } - - if k >= len(i1) || k >= len(i2) { - return false - } - - if i1[k].typ != i2[k].typ { - return false - } - } - - return true -} diff --git a/path_states.go b/path_states.go index b1e82de..73fcdf8 100644 --- a/path_states.go +++ b/path_states.go @@ -194,15 +194,6 @@ func lexPathIndexRangeSecond(l lexer, state *intStack) stateFn { } } -func lexPathArrayClose(l lexer, state *intStack) stateFn { - cur := l.take() - if cur != ']' { - return l.errorf("Expected ] instead of %#U", cur) - } - l.emit(pathBracketRight) - return lexPathAfterKey -} - func lexPathAfterValue(l lexer, state *intStack) stateFn { cur := l.take() if cur != eof { diff --git a/path_test.go b/path_test.go index 360c901..ebf9701 100644 --- a/path_test.go +++ b/path_test.go @@ -27,13 +27,14 @@ var optests = []optest{ func TestQueryOperators(t *testing.T) { as := assert.New(t) - for _, t := range optests { - path, err := parsePath(t.path) + for _, optest := range optests { + t.Log(optest.name) + path, err := parsePath(optest.path) as.NoError(err) - as.EqualValues(len(t.expected), len(path.operators)) + as.EqualValues(len(optest.expected), len(path.operators)) - for x, op := range t.expected { + for x, op := range optest.expected { as.EqualValues(pathTokenNames[op], pathTokenNames[path.operators[x].typ]) } } diff --git a/queue.go b/queue.go index 67566b7..d7cdf49 100644 --- a/queue.go +++ b/queue.go @@ -15,7 +15,7 @@ func newResults() *Results { func (q *Results) push(n *Result) { if q.head == q.tail && q.count > 0 { - nodes := make([]*Result, len(q.nodes)*2, len(q.nodes)*2) + nodes := make([]*Result, len(q.nodes)*2) copy(nodes, q.nodes[q.head:]) copy(nodes[len(q.nodes)-q.head:], q.nodes[:q.head]) q.head = 0 @@ -37,7 +37,7 @@ func (q *Results) Pop() *Result { return node } -func (q *Results) peek() *Result { +func (q *Results) Peek() *Result { if q.count == 0 { return nil } diff --git a/stack.go b/stack.go index 58818a4..e3545cc 100644 --- a/stack.go +++ b/stack.go @@ -37,67 +37,6 @@ func (s *intStack) peek() (int, bool) { return v, true } -func (s *intStack) clone() *intStack { - d := intStack{ - values: make([]int, s.len()), - } - copy(d.values, s.values) - return &d -} - -func (s *intStack) toArray() []int { - return s.values -} - -// Result Stack - -type resultStack struct { - values []Result -} - -func newResultStack() *resultStack { - return &resultStack{ - values: make([]Result, 0), - } -} - -func (s *resultStack) len() int { - return len(s.values) -} - -func (s *resultStack) push(r Result) { - s.values = append(s.values, r) -} - -func (s *resultStack) pop() (Result, bool) { - if s.len() == 0 { - return Result{}, false - } - v, _ := s.peek() - s.values = s.values[:len(s.values)-1] - return v, true -} - -func (s *resultStack) peek() (Result, bool) { - if s.len() == 0 { - return Result{}, false - } - v := s.values[len(s.values)-1] - return v, true -} - -func (s *resultStack) clone() *resultStack { - d := resultStack{ - values: make([]Result, s.len()), - } - copy(d.values, s.values) - return &d -} - -func (s *resultStack) toArray() []Result { - return s.values -} - // Interface Stack type stack struct {