Skip to content

Commit

Permalink
Merge pull request #3 from gojekfarm/doc
Browse files Browse the repository at this point in the history
Update documentation
  • Loading branch information
pradithya authored Apr 21, 2022
2 parents bb91a19 + 7da2198 commit 16a0ed9
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/jsonpath.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
name: Go ${{ matrix.go }} test
strategy:
matrix:
go: ["1.16", "1.17"]
go: ["1.16", "1.17", "1.18"]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
Expand Down
6 changes: 6 additions & 0 deletions jsonpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

var ErrGetFromNullObj = errors.New("get attribute from null object")

// JosnPathLookup compile a jsonpath `jpath` and perform lookup on `obj`
func JsonPathLookup(obj interface{}, jpath string) (interface{}, error) {
c, err := Compile(jpath)
if err != nil {
Expand All @@ -21,6 +22,7 @@ func JsonPathLookup(obj interface{}, jpath string) (interface{}, error) {
return c.Lookup(obj)
}

// Compiled is a compiled jsonpath representation
type Compiled struct {
path string
steps []step
Expand All @@ -32,6 +34,7 @@ type step struct {
args interface{}
}

// MustCompile will compile a json path `jpath` and panic if compilation failed
func MustCompile(jpath string) *Compiled {
c, err := Compile(jpath)
if err != nil {
Expand All @@ -40,6 +43,7 @@ func MustCompile(jpath string) *Compiled {
return c
}

// Compile compile a jsonpath `jpath` and return the compiled version of the jsonpath or error
func Compile(jpath string) (*Compiled, error) {
tokens, err := tokenize(jpath)
if err != nil {
Expand All @@ -63,10 +67,12 @@ func Compile(jpath string) (*Compiled, error) {
return &res, nil
}

// String return string representation of the compiled jsonpath
func (c *Compiled) String() string {
return fmt.Sprintf("Compiled lookup: %s", c.path)
}

// Lookup perform lookup using the compiled jsonpath on json `obj`
func (c *Compiled) Lookup(obj interface{}) (interface{}, error) {
var err error
for _, s := range c.steps {
Expand Down
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ A golang implementation of JsonPath syntax.
follow the majority rules in http://goessner.net/articles/JsonPath/
but also with some minor differences.

this library is till bleeding edge, so use it at your own risk. :D
This library is forked from https://github.com/oliveagle/jsonpath with additional bugfixes and features.

**Golang Version Required**: 1.5+

Get Started
------------

```bash
go get github.com/oliveagle/jsonpath
go get github.com/gojekfarm/jsonpath
```

example code:
Expand Down Expand Up @@ -111,4 +111,4 @@ example json path syntax.
| $.store.book[:].price | [8.9.5, 12.99, 8.9.9, 22.99] |
| $.store.book[?(@.author =~ /(?i).*REES/)].author | "Nigel Rees" |

> Note: golang support regular expression flags in form of `(?imsU)pattern`
> Note: golang support regular expression flags in form of `(?imsU)pattern`

0 comments on commit 16a0ed9

Please sign in to comment.