Refactor Super Graph into a library #26
This commit is contained in:
@ -6,6 +6,7 @@ import (
|
||||
"github.com/cespare/xxhash/v2"
|
||||
)
|
||||
|
||||
// Filter function filters the JSON keeping only the provided keys and removing all others
|
||||
func Filter(w *bytes.Buffer, b []byte, keys []string) error {
|
||||
var err error
|
||||
kmap := make(map[uint64]struct{}, len(keys))
|
||||
|
@ -17,11 +17,13 @@ const (
|
||||
expectNumClose
|
||||
)
|
||||
|
||||
// Field struct holds a JSON key and value
|
||||
type Field struct {
|
||||
Key []byte
|
||||
Value []byte
|
||||
}
|
||||
|
||||
// Value function is a utility function to sanitize returned values
|
||||
func Value(b []byte) []byte {
|
||||
e := (len(b) - 1)
|
||||
switch {
|
||||
@ -36,6 +38,7 @@ func Value(b []byte) []byte {
|
||||
}
|
||||
}
|
||||
|
||||
// Keys function fetches values for the provided keys
|
||||
func Get(b []byte, keys [][]byte) []Field {
|
||||
kmap := make(map[uint64]struct{}, len(keys))
|
||||
|
||||
|
@ -1,5 +1,7 @@
|
||||
// Package jsn provides fast and no-allocation functions to extract values and modify JSON data
|
||||
package jsn
|
||||
|
||||
// Keys function fetches all the unique keys in the JSON data in order of their occurance
|
||||
func Keys(b []byte) [][]byte {
|
||||
res := make([][]byte, 0, 20)
|
||||
|
||||
@ -8,7 +10,7 @@ func Keys(b []byte) [][]byte {
|
||||
var k []byte
|
||||
state := expectValue
|
||||
|
||||
st := NewStack()
|
||||
st := newStack()
|
||||
ae := 0
|
||||
instr := false
|
||||
slash := 0
|
||||
|
@ -7,6 +7,7 @@ import (
|
||||
"github.com/cespare/xxhash/v2"
|
||||
)
|
||||
|
||||
// Replace function replaces key-value pairs provided in the `from` argument with those in the `to` argument
|
||||
func Replace(w *bytes.Buffer, b []byte, from, to []Field) error {
|
||||
if len(from) != len(to) {
|
||||
return errors.New("'from' and 'to' must be of the same length")
|
||||
|
24
jsn/stack.go
24
jsn/stack.go
@ -4,34 +4,34 @@ type skipInfo struct {
|
||||
ss, se int
|
||||
}
|
||||
|
||||
type Stack struct {
|
||||
type stack struct {
|
||||
stA [20]skipInfo
|
||||
st []skipInfo
|
||||
top int
|
||||
}
|
||||
|
||||
// Create a new Stack
|
||||
func NewStack() *Stack {
|
||||
s := &Stack{top: -1}
|
||||
// Create a new stack
|
||||
func newStack() *stack {
|
||||
s := &stack{top: -1}
|
||||
s.st = s.stA[:0]
|
||||
return s
|
||||
}
|
||||
|
||||
// Return the number of items in the Stack
|
||||
func (s *Stack) Len() int {
|
||||
// Return the number of items in the stack
|
||||
func (s *stack) Len() int {
|
||||
return (s.top + 1)
|
||||
}
|
||||
|
||||
// View the top item on the Stack
|
||||
func (s *Stack) Peek() *skipInfo {
|
||||
// View the top item on the stack
|
||||
func (s *stack) Peek() *skipInfo {
|
||||
if s.top == -1 {
|
||||
return nil
|
||||
}
|
||||
return &s.st[s.top]
|
||||
}
|
||||
|
||||
// Pop the top item of the Stack and return it
|
||||
func (s *Stack) Pop() *skipInfo {
|
||||
// Pop the top item of the stack and return it
|
||||
func (s *stack) Pop() *skipInfo {
|
||||
if s.top == -1 {
|
||||
return nil
|
||||
}
|
||||
@ -40,8 +40,8 @@ func (s *Stack) Pop() *skipInfo {
|
||||
return &s.st[(s.top + 1)]
|
||||
}
|
||||
|
||||
// Push a value onto the top of the Stack
|
||||
func (s *Stack) Push(value skipInfo) {
|
||||
// Push a value onto the top of the stack
|
||||
func (s *stack) Push(value skipInfo) {
|
||||
s.top++
|
||||
if len(s.st) <= s.top {
|
||||
s.st = append(s.st, value)
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
)
|
||||
|
||||
// Strip function strips out all values from the JSON data expect for the provided path
|
||||
func Strip(b []byte, path [][]byte) []byte {
|
||||
s, e, d := 0, 0, 0
|
||||
|
||||
|
@ -7,7 +7,7 @@ import (
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// Validate validates JSON s.
|
||||
// Validate function validates JSON
|
||||
func Validate(s string) error {
|
||||
s = skipWS(s)
|
||||
|
||||
|
Reference in New Issue
Block a user