Skip to content

Commit

Permalink
deprecate JQ function
Browse files Browse the repository at this point in the history
  • Loading branch information
glossd committed Nov 11, 2024
1 parent f06f7a6 commit b9b532b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 23 deletions.
28 changes: 12 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ fmt.Println("First sold pet ", j.Q(".[0]"))
`fetch.J` is an interface with `Q` method which provides easy access to any field.
Method `fetch.J#String()` returns JSON formatted string of the value.
```go
j, err := fetch.Unmarshal[fetch.J](`{
j := fetch.Parse(`{
"name": "Jason",
"category": {
"name":"dogs"
Expand All @@ -178,29 +178,19 @@ j, err := fetch.Unmarshal[fetch.J](`{
{"name":"briard"}
]
}`)
if err != nil {
panic(err)
}

fmt.Println("Print the whole json:", j)
fmt.Println("Pet's name is", j.Q(".name"))
fmt.Println("Pet's category name is", j.Q(".category.name"))
fmt.Println("First tag's name is", j.Q(".tags[0].name"))
```
```
Method `fetch.J#Q` returns `fetch.J`. You can use the method `Q` on the result as well.
```go
category := j.Q(".category")
fmt.Println("Pet's category object", category)
fmt.Println("Pet's category name is", category.Q(".name"))
```

`fetch.JQ` is a helper function to parse JSON into `fetch.J` and query it.
```go
jsonStr := `{"category": {"name":"dogs"}}`
name := fetch.JQ(jsonStr, ".category.name")
fmt.Println("Category name:", name)
```

To convert `fetch.J` to a basic value use one of `As*` methods

| J Method | Return type |
Expand All @@ -213,7 +203,7 @@ To convert `fetch.J` to a basic value use one of `As*` methods

E.g.
```go
n, ok := fetch.JQ(`{"price": 14.99}`, ".price").AsNumber()
n, ok := fetch.Parse(`{"price": 14.99}`).Q(".price").AsNumber()
if !ok {
// not a number
}
Expand All @@ -222,11 +212,11 @@ fmt.Printf("Price: %.2f\n", n) // n is a float64

Use `IsNil` to check the value on presence.
```go
if fetch.JQ("{}", ".price").IsNil() {
if fetch.Parse("{}").Q(".price").IsNil() {
fmt.Println("key 'price' doesn't exist")
}
// fields of unknown values are nil as well.
if fetch.JQ("{}", ".price.cents").IsNil() {
if fetch.Parse("{}").Q(".price.cents").IsNil() {
fmt.Println("'cents' of undefined is fine.")
}
```
Expand All @@ -238,7 +228,7 @@ To convert any object into a string, which is treating public struct fields as d
```go
str, err := fetch.Marhsal(map[string]string{"key":"value"})
```
#### Unmarshal
#### Unmarshalling
Unmarshal will parse the input into the generic type.
```go
type Pet struct {
Expand All @@ -251,6 +241,12 @@ if err != nil {
fmt.Println(p.Name)
```

`fetch.Parse` unmarshalls JSON string into `fetch.J`, returning `fetch.Nil` instead of an error,
which allows you to write one-liners.
```go
fmt.Println(fetch.Parse(`{"name":"Jason"}`).Q(".name"))
```

### Global Setters
You can set base URL path for all requests.
```go
Expand Down
11 changes: 4 additions & 7 deletions j.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
// | fetch.B | bool | boolean |
// | fetch.Nil | (nil) *struct{} | null, undefined, anything not found |
type J interface {
// Q parses JQ-like patterns and returns according to the path value.
// Q parses jq-like patterns and returns according to the path value.
// E.g.
//{
// "name": "Jason",
Expand Down Expand Up @@ -295,14 +295,11 @@ func (n Nil) AsString() (string, bool) { return "", false }
func (n Nil) AsBoolean() (bool, bool) { return false, false }
func (n Nil) IsNil() bool { return true }

// JQ unmarshals jsonStr into fetch.J
// Deprecated, use fetch.Parse(jsonStr).Q(pattern)
// JQ parses jsonStr into fetch.J
// and calls J.Q method with the pattern.
func JQ(jsonStr, pattern string) J {
j, err := Unmarshal[J](jsonStr)
if err != nil {
return jnil
}
return j.Q(pattern)
return Parse(jsonStr).Q(pattern)
}

func isJNil(v any) bool {
Expand Down

0 comments on commit b9b532b

Please sign in to comment.