Skip to content

Commit

Permalink
add SearchOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
AsterDY committed Jan 20, 2024
1 parent e4a2225 commit 5d989cf
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,12 +290,20 @@ println(string(buf) == string(exp)) // true
- modification: `Set()`, `SetByIndex()`, `Add()`

### Ast.Value
Due to `ast.Node`'s **transversely-lazy-load** design, it is not effecient enough for read-once-for-each-path case, and it ** CANNOT be read concurrently**. If your business has such scenario, you can use `ast.Value` -- a more robust encapsulation of raw JSON message.
Due to `ast.Node`'s **transversely-lazy-load** design, it ** CANNOT be read concurrently**. If your business has such scenario, you can use `ast.Value`:
```go
var opts = sonic.SearchOptions{
Copy: false, // to control returning JSON by Copy instead of Reference
Validate: false // to control if validate returned JSON syntax
}
val, err := opts.GetFromString(json, paths...) // skip and search JSON
any, err := val.Interface() // converts to go primitive type
```

#### APIs
Most of its APIs are same with `ast.Node`'s, including both `Get` and `Set`. Besides:
- It provide `GetMany` \ `SetMany` to read or write multiple values once, in order to **reduce the overhead of repeatedly visiting path**.

- It provide `GetMany` \ `SetMany` \ `UnsetMany` \ `AddMany` \ `PopMany` to read or write multiple values once, in order to **reduce the overhead of repeatedly visiting path**.
-

### Ast.Visitor
Sonic provides an advanced API for fully parsing JSON into non-standard types (neither `struct` not `map[string]interface{}`) without using any intermediate representation (`ast.Node` or `interface{}`). For example, you might have the following types which are like `interface{}` but actually not `interface{}`:
Expand Down
12 changes: 6 additions & 6 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@ func DeleteFromString(src string, path ...interface{}) (string, error) {
return ast.NewSearcher(src).DeleteByPath(path...)
}

// GetOptions
type GetOptions struct {
// SearchOptions
type SearchOptions struct {
Copy bool // if copy returned JSON to reduce memory usage
Validate bool // if validate returned JSON for safty
}
Expand All @@ -235,12 +235,12 @@ type GetOptions struct {
//
// Note, the api expects the json is well-formed at least,
// otherwise it may return unexpected result.
func GetValue(opts GetOptions, src []byte, path ...interface{}) (ast.Value, error) {
return GetValueFromString(opts, rt.Mem2Str(src), path...)
func (opts SearchOptions) Get(src []byte, path ...interface{}) (ast.Value, error) {
return opts.GetFromString(rt.Mem2Str(src), path...)
}

// GetValueFromString is same with GetValue except src is string,
func GetValueFromString(opts GetOptions, src string, path ...interface{}) (ast.Value, error) {
// GetFromString is same with GetValue except src is string,
func (opts SearchOptions) GetFromString(src string, path ...interface{}) (ast.Value, error) {
s := ast.NewSearcher(src)
s.Validate(opts.Validate)
s.Copy(opts.Validate)
Expand Down

0 comments on commit 5d989cf

Please sign in to comment.