supporting Hyphen"-" in Compile and Search

Aperta
#58 5 commenti 3 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
go
Ambito
compilers

Direzione di ricerca

Inizia da lexer.go, in particolare dalla gestione dei token intorno alle righe 164-166 e da consumeUnquotedIdentifier intorno alle righe 388-392, quindi segui come viene analizzato jmespath.Compile("foo-bar") prima dell’esecuzione di Search. Conferma il modo previsto per gestire le chiavi JSON contenenti trattini e aggiungi la copertura per l’espressione segnalata e la ricerca risultante.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.
https://github.com/jmespath/go-jmespath/blob/3d4fd11601ddca248480565884e34e393313cd62/lexer.go#L164-L166

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
https://github.com/jmespath/go-jmespath/blob/3d4fd11601ddca248480565884e34e393313cd62/lexer.go#L23-L29

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 
}

https://github.com/jmespath/go-jmespath/blob/3d4fd11601ddca248480565884e34e393313cd62/lexer.go#L388-L392
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.

Lingua principale
Go
Stelle
623
Fork
105
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di jmespath/go-jmespath

Tutte le issue di jmespath/go-jmespath

Issue simili

Altre issue su Go

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.