Skip to content

Commit

Permalink
opt: pass option by object
Browse files Browse the repository at this point in the history
  • Loading branch information
AsterDY committed Jun 27, 2024
1 parent 7ea1b17 commit cb0b837
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 111 deletions.
6 changes: 6 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,12 @@ func Get(src []byte, path ...interface{}) (ast.Node, error) {
return GetCopyFromString(rt.Mem2Str(src), path...)
}

func GetWithOptions(src []byte, opts ast.SearchOptions, path ...interface{}) (ast.Node, error) {
s := ast.NewSearcher(rt.Mem2Str(src))
s.SearchOptions = opts
return s.GetByPath(path...)
}

// GetFromString is same with Get except src is string.
//
// WARNING: The returned JSON is **Referenced** from the input.
Expand Down
36 changes: 15 additions & 21 deletions ast/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,8 @@ func quoteString(e *[]byte, s string) {
var bytesPool = sync.Pool{}

func (self *Node) MarshalJSON() ([]byte, error) {
return self.marshalJSON(noLazy)
}

func (self *Node) marshalJSON(locked bool) ([]byte, error) {
buf := newBuffer()
err := self.encode(buf, locked)
err := self.encode(buf)
if err != nil {
freeBuffer(buf)
return nil, err
Expand All @@ -111,7 +107,7 @@ func newBuffer() *[]byte {
if ret := bytesPool.Get(); ret != nil {
return ret.(*[]byte)
} else {
buf := make([]byte, 0, option.DefaultEncoderBufferSize)
buf := make([]byte, 0, option.DefaultAstEncoderBufferSize)
return &buf
}
}
Expand All @@ -121,9 +117,9 @@ func freeBuffer(buf *[]byte) {
bytesPool.Put(buf)
}

func (self *Node) encode(buf *[]byte, locked bool) error {
func (self *Node) encode(buf *[]byte) error {
if self.IsRaw() {
return self.encodeRaw(buf, locked)
return self.encodeRaw(buf)
}
switch int(self.itype()) {
case V_NONE : return ErrNotExist
Expand All @@ -140,16 +136,14 @@ func (self *Node) encode(buf *[]byte, locked bool) error {
}
}

func (self *Node) encodeRaw(buf *[]byte, locked bool) error {
if locked {
self.rlock()
if !self.IsRaw() {
self.runlock()
return self.encode(buf, false)
}
func (self *Node) encodeRaw(buf *[]byte) error {
lock := self.rlock()
if !self.IsRaw() {
self.runlock()
return self.encode(buf)
}
raw := self.toString()
if locked {
if lock {
self.runlock()
}
*buf = append(*buf, raw...)
Expand Down Expand Up @@ -212,7 +206,7 @@ func (self *Node) encodeArray(buf *[]byte) error {
*buf = append(*buf, ',')
}
started = true
if err := n.encode(buf, true); err != nil {
if err := n.encode(buf); err != nil {
return err
}
}
Expand All @@ -221,16 +215,16 @@ func (self *Node) encodeArray(buf *[]byte) error {
return nil
}

func (self *Pair) encode(buf *[]byte, locked bool) error {
func (self *Pair) encode(buf *[]byte) error {
if len(*buf) == 0 {
*buf = append(*buf, '"', '"', ':')
return self.Value.encode(buf, locked)
return self.Value.encode(buf)
}

quote(buf, self.Key)
*buf = append(*buf, ':')

return self.Value.encode(buf, locked)
return self.Value.encode(buf)
}

func (self *Node) encodeObject(buf *[]byte) error {
Expand Down Expand Up @@ -258,7 +252,7 @@ func (self *Node) encodeObject(buf *[]byte) error {
*buf = append(*buf, ',')
}
started = true
if err := n.encode(buf, true); err != nil {
if err := n.encode(buf); err != nil {
return err
}
}
Expand Down
28 changes: 21 additions & 7 deletions ast/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,16 @@ func (self *Node) Raw() (string, error) {
if self == nil {
return "", ErrNotExist
}
if noLazy {
self.rlock()
}
lock := self.rlock()
if !self.IsRaw() {
if noLazy {
if lock {
self.runlock()
}
buf, err := self.marshalJSON(false)
buf, err := self.MarshalJSON()
return rt.Mem2Str(buf), err
}
ret := self.toString()
if noLazy {
if lock {
self.runlock()
}
return ret, nil
Expand Down Expand Up @@ -1640,7 +1638,23 @@ func NewRaw(json string) Node {
if it == _V_NONE {
return Node{}
}
return newRawNode(parser.s[start:parser.p], it)
return newRawNode(parser.s[start:parser.p], it, false)
}

// NewRawConcurrentRead creates a node of raw json, which can be READ
// (GetByPath/Get/Index/GetOrIndex/Int64/Bool/Float64/String/Number/Interface/Array/Map/Raw/MarshalJSON) concurrently.
// If the input json is invalid, NewRaw returns a error Node.
func NewRawConcurrentRead(json string) Node {
parser := NewParserObj(json)
start, err := parser.skip()
if err != 0 {
return *newError(err, err.Message())
}
it := switchRawType(parser.s[start])
if it == _V_NONE {
return Node{}
}
return newRawNode(parser.s[start:parser.p], it, true)
}

// NewAny creates a node of type V_ANY if any's type isn't Node or *Node,
Expand Down
10 changes: 5 additions & 5 deletions ast/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -699,12 +699,12 @@ func TestCheckError_Empty(t *testing.T) {
t.Fatal()
}

n := newRawNode("[hello]", types.V_ARRAY)
n := newRawNode("[hello]", types.V_ARRAY, false)
n.parseRaw(false)
if n.Check() != nil {
t.Fatal(n.Check())
}
n = newRawNode("[hello]", types.V_ARRAY)
n = newRawNode("[hello]", types.V_ARRAY, false)
n.parseRaw(true)
p := NewParser("[hello]")
p.noLazy = true
Expand Down Expand Up @@ -735,7 +735,7 @@ func TestCheckError_Empty(t *testing.T) {
if e != nil {
t.Fatal(e)
}
exist, e := a.Set("d", newRawNode("x", types.V_OBJECT))
exist, e := a.Set("d", newRawNode("x", types.V_OBJECT, false))
if exist || e != nil {
t.Fatal(err)
}
Expand All @@ -746,7 +746,7 @@ func TestCheckError_Empty(t *testing.T) {
if d.Check() == nil {
t.Fatal(d)
}
exist, e = a.Set("e", newRawNode("[}", types.V_ARRAY))
exist, e = a.Set("e", newRawNode("[}", types.V_ARRAY, false))
if e != nil {
t.Fatal(e)
}
Expand Down Expand Up @@ -839,7 +839,7 @@ func TestUnset(t *testing.T) {
*entities = NewRaw(string(out))

hashtags := entities.Get("hashtags").Index(0)
hashtags.Set("text2", newRawNode(`{}`, types.V_OBJECT))
hashtags.Set("text2", NewRaw(`{}`))
exist, err = hashtags.Unset("indices") // NOTICE: Unset() won't change node.Len() here
if !exist || err != nil || hashtags.len() != 2 {
t.Fatal(hashtags.len())
Expand Down
Loading

0 comments on commit cb0b837

Please sign in to comment.