supporting Hyphen"-" in Compile and Search

Đang mở
#58 5 bình luận 3 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
35/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
go
Lĩnh vực
compilers

Hướng nghiên cứu

Bắt đầu với lexer.go, đặc biệt là phần xử lý token quanh các dòng 164-166 và consumeUnquotedIdentifier quanh các dòng 388-392, sau đó theo dõi cách jmespath.Compile("foo-bar") được phân tích trước khi Search chạy. Xác nhận cách dự kiến để xử lý các khóa JSON chứa dấu gạch ngang và bổ sung coverage cho biểu thức được báo cáo cùng lookup tạo ra.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Go
Star
623
Fork
105
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của jmespath/go-jmespath

Tất cả issue của jmespath/go-jmespath

Issue tương tự

Thêm issue về Go

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.