Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

supporting Hyphen"-" in Compile and Search #58

Open
b-entangled opened this issue Sep 22, 2020 · 5 comments
Open

supporting Hyphen"-" in Compile and Search #58

b-entangled opened this issue Sep 22, 2020 · 5 comments

Comments

@b-entangled
Copy link

b-entangled commented Sep 22, 2020

Hi,
I tried the below code, where we want to search jmes path having Hyphen "-", I'm getting the following error
SyntaxError: Unexpected token at the end of the expression: tNumber

From the code it looks like the Hyphen "-" is considered as a reference to get numeric values.

go-jmespath/lexer.go

Lines 164 to 166 in 3d4fd11

} else if r == '-' || (r >= '0' && r <= '9') {
t := lexer.consumeNumber()
tokens = append(tokens, t)

package main

import (
	"encoding/json"
	"fmt"
	"github.com/jmespath/go-jmespath"
)

func main() {
	var jsondata = []byte(`{"foo-bar": {"bar": "car"}}`)
	var data interface{}
	err := json.Unmarshal(jsondata, &data)
	fmt.Println(err)
	precompiled, err := jmespath.Compile("foo-bar")
	if err != nil{
      	    fmt.Println(err)
   	}
    	result, err := precompiled.Search(data)
	fmt.Printf("%T", result)
	fmt.Println(result, err)
}

Is it possible to pass values containing Hyphen "-"? jmespath.Compile("foo-bar")

Please let us know if there is any other way to do that, If not please consider our suggestion.
Opinion:-

Can we add a filter func in Lexer

go-jmespath/lexer.go

Lines 23 to 29 in 3d4fd11

// Lexer contains information about the expression being tokenized.
type Lexer struct {
expression string // The expression provided by the user.
currentPos int // The current position in the string.
lastWidth int // The width of the current rune. This
buf bytes.Buffer // Internal buffer used for building up values.
}

type SkipFilterFunc func(r rune) bool

// Lexer contains information about the expression being tokenized.
type Lexer struct {
	expression string       // The expression provided by the user.
	currentPos int          // The current position in the string.
	lastWidth  int          // The width of the current rune.  This
	buf        bytes.Buffer // Internal buffer used for building up values.
	skipFunc []SkipFilterFunc 
}

go-jmespath/lexer.go

Lines 388 to 392 in 3d4fd11

r := lexer.next()
if r < 0 || r > 128 || identifierTrailingBits[uint64(r)/64]&(1<<(uint64(r)%64)) == 0 {
lexer.back()
break
}

And we can call those filter func inside consuleUnquotedIdentifier

		if r < 0 || r > 128 || identifierTrailingBits[uint64(r)/64]&(1<<(uint64(r)%64)) == 0 {
			skip := false
			for _, skipFunc := range lexer.skipFunc{
				if ok := skipFunc(r); ok {
					skip = true
					break
				}
			}
			if skip{
				continue
			}
			lexer.back()
			break

		}

So I can support any key to be part of string and can override the existing feature.

@b-entangled b-entangled changed the title supporting Hypen "-" in Compile and Search supporting Hyphen"-" in Compile and Search Sep 22, 2020
@nevins-b
Copy link

nevins-b commented Oct 6, 2020

If this is like the python library what may work is jmespath.Compile("\"foo-bar\"")

edit: yep https://play.golang.org/p/8SezRZLbyMR

@weir-it-services
Copy link

weir-it-services commented Oct 21, 2020

For anyone else stumbling on this issue the following worked for me:

role_attribute_path: contains("custom:groups"[*], 'Admin-Users') && 'Admin' || contains("custom:groups"[*], 'Grafana-Editor') && 'Editor' || 'Viewer'

@ghost
Copy link

ghost commented Apr 11, 2021

Is there an update on this? The workaround seems to work but doesn't seem ideal, especially for complex queries.

@Robert-M-Muench
Copy link

Not being able to use field names with an - in a query is making problems quite often. Would be great to get it fixed.

@Robert-M-Muench
Copy link

If this is like the python library what may work is jmespath.Compile("\"foo-bar\"")
edit: yep https://play.golang.org/p/8SezRZLbyMR

Didn't recognize the answer... Yes, with escaping using hyphen in keys works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants