Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ type Interface interface {
// Marshal serializes first root node to w.
Marshal(w io.Writer) error

// Populate fills map "to" with top root node data.
Populate(to map[string]any)

// ErrorOffset returns last error offset.
ErrorOffset() int
// Prealloc prepares space for further parse.
Expand Down
28 changes: 28 additions & 0 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strconv"
"unsafe"

"github.com/koykov/byteconv"
"github.com/koykov/entry"
"github.com/koykov/indirect"
)
Expand Down Expand Up @@ -371,6 +372,33 @@ func (n *Node) LastChild() *Node {
return nullNode
}

// Populate fills map "to" with node data.
func (n *Node) Populate(to map[string]any) {
if to == nil {
return
}
switch n.Type() {
case TypeObject:
n.Each(func(_ int, node *Node) {
to[node.KeyString()] = node
})
case TypeArray:
if vec := n.indirectVector(); vec != nil {
n.Each(func(c int, node *Node) {
key := byteconv.B2S(vec.BufferizeInt(int64(c)))
to[key] = node
})
}
case TypeUnknown:
case TypeNull:
return
default:
if key := n.KeyString(); len(key) > 0 {
to[key] = n
}
}
}

// SortKeys sorts child nodes by key in AB order.
func (n *Node) SortKeys() *Node {
if n.Type() != TypeObject {
Expand Down
7 changes: 7 additions & 0 deletions vector.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,13 @@ func (vec *Vector) RootTop() *Node {
return &vec.nodes[rootRow[len(rootRow)-1]]
}

// Populate fills map "to" with top root node data.
func (vec *Vector) Populate(to map[string]any) {
if root := vec.Root(); root != nil {
root.Populate(to)
}
}

// Each applies custom function to each root node.
func (vec *Vector) Each(fn func(idx int, node *Node)) {
rootRow := vec.Index.GetRow(0)
Expand Down
31 changes: 30 additions & 1 deletion vector_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
package vector

import "sync"
import (
"sync"
"testing"
)

var testPool = sync.Pool{New: func() any { return &Vector{} }}

func TestVector(t *testing.T) {
t.Run("populate", func(t *testing.T) {
var vec Vector
_ = vec.SetSrc([]byte("zzz"), false)
r, ri := vec.AcquireNode(0)
r.SetType(TypeObject)

n, i := vec.AcquireChildWithType(r, 1, TypeString)
n.Key().InitString("foo", 0, 3)
n.Value().InitString("qwerty", 0, 6)
r.ReleaseChild(i, n)

n, i = vec.AcquireChildWithType(r, 1, TypeString)
n.Key().InitString("bar", 0, 3)
n.Value().InitString("asdfgh", 0, 6)
r.ReleaseChild(i, n)

vec.ReleaseNode(ri, r)

m := make(map[string]any)
vec.Populate(m)

// assert m
})
}