jsonbind

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 15, 2021 License: MIT Imports: 16 Imported by: 0

README

os-jsonbind

Extract data with JSON schema

This library extend json schema, you can predefine standard json schema with extend tags, which can be used for extract data from source data(bytes) and create new data for json

Install

go get github.com/cfhamlet/os-jsonbind

Usage

  • source data

    srcData := []byte(`{"k1": 1, "k2": "v2", "k3": [2,3,4], "k4": {"i1": 5, "i2": true}}`) // json bytes
    
  • define a json schema of the target object with "bind" key, value is path string of the source data

    spec := []byte(`{"properties": {"K": {"type": "number", "bind": "k1"}}}`) // "k1" gjson style
    
  • create a binder with the schema

    import "github.com/cfhamlet/os-jsonbind"
    
    binder, err := jsonbind.Compile(spec)
    
  • extract data from source data, return target result, binded flag and error

    result, binded, err := binder.Bind(context.Background(), srcData)
    // result is map[string]interface{}{"K": 1}
    // binded is true
    // err is null
    
Rule Syntax

The default rule type is gjson, you can specify other syntax with rule name as prefix

"bind": "ajson:$.k1"

Supported syntax rule types:

License

MIT licensed.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCast         = errors.New("cast error")
	ErrPreallocate  = errors.New("preallocate error")
	ErrNotSupported = errors.New("not supported")
)

Functions

func JSONPath

func JSONPath(path []string) string

func MustAtoi

func MustAtoi(i string) int

func NewSchema

func NewSchema(b []byte) (*jsonschema.Schema, error)

func Register

func Register(c *jsonschema.Compiler)

func SplitSpec

func SplitSpec(s string) (string, string)

func Unmarshal

func Unmarshal(r io.Reader) (interface{}, error)

Types

type AJSONGetter

type AJSONGetter struct {
	// contains filtered or unexported fields
}

func NewAJSONGetter

func NewAJSONGetter(spec string) (*AJSONGetter, error)

func (*AJSONGetter) Get

func (g *AJSONGetter) Get(ctx context.Context, obj interface{}) (interface{}, bool, error)

func (*AJSONGetter) New

func (g *AJSONGetter) New(b []byte) (interface{}, error)

type BindNode

type BindNode struct {
	Path string

	Next interface{}
	// contains filtered or unexported fields
}

func (*BindNode) Bind

func (node *BindNode) Bind(ctx context.Context, p ParsedCache) (interface{}, bool, error)

func (*BindNode) Get

func (node *BindNode) Get(ctx context.Context, p ParsedCache) (interface{}, bool, error)

type BindPathError

type BindPathError string

func (BindPathError) Error

func (e BindPathError) Error() string

type Binder

type Binder struct {
	// contains filtered or unexported fields
}

func Compile

func Compile(b []byte) (*Binder, error)

func (*Binder) Bind

func (binder *Binder) Bind(ctx context.Context, b []byte) (interface{}, bool, error)

type GJSONGetter

type GJSONGetter struct {
	// contains filtered or unexported fields
}

func NewGJSONGetter

func NewGJSONGetter(spec string) *GJSONGetter

func (*GJSONGetter) Get

func (g *GJSONGetter) Get(ctx context.Context, obj interface{}) (interface{}, bool, error)

func (*GJSONGetter) New

func (g *GJSONGetter) New(b []byte) (interface{}, error)

type Getter

type Getter interface {
	Get(context.Context, interface{}) (interface{}, bool, error)
	New([]byte) (interface{}, error)
}

type GvalJSONPathGetter

type GvalJSONPathGetter struct {
	// contains filtered or unexported fields
}

func NewGvalJSONPathGetter

func NewGvalJSONPathGetter(spec string) (*GvalJSONPathGetter, error)

func (*GvalJSONPathGetter) Get

func (g *GvalJSONPathGetter) Get(ctx context.Context, obj interface{}) (interface{}, bool, error)

func (*GvalJSONPathGetter) New

func (g *GvalJSONPathGetter) New(b []byte) (interface{}, error)

type MapGetter

type MapGetter struct{}

func NewMapGetter

func NewMapGetter() *MapGetter

func (*MapGetter) Get

func (g *MapGetter) Get(context.Context, interface{}) (interface{}, bool, error)

func (*MapGetter) New

func (g *MapGetter) New([]byte) (interface{}, error)

type NewGetter

type NewGetter func(string) (Getter, error)

type NilGetter

type NilGetter struct{}

func NewNilGetter

func NewNilGetter() *NilGetter

func (*NilGetter) Get

func (g *NilGetter) Get(context.Context, interface{}) (interface{}, bool, error)

func (*NilGetter) New

func (g *NilGetter) New([]byte) (interface{}, error)

type OJGGetter

type OJGGetter struct {
	// contains filtered or unexported fields
}

func NewOJGGetter

func NewOJGGetter(spec string) (*OJGGetter, error)

func (*OJGGetter) Get

func (g *OJGGetter) Get(ctx context.Context, obj interface{}) (interface{}, bool, error)

func (*OJGGetter) New

func (g *OJGGetter) New(b []byte) (interface{}, error)

type OJSONPathGetter

type OJSONPathGetter struct {
	// contains filtered or unexported fields
}

func NewOJSONPathGetter

func NewOJSONPathGetter(spec string) (*OJSONPathGetter, error)

func (*OJSONPathGetter) Get

func (g *OJSONPathGetter) Get(ctx context.Context, obj interface{}) (interface{}, bool, error)

func (*OJSONPathGetter) New

func (g *OJSONPathGetter) New(b []byte) (interface{}, error)

type ParsedCache

type ParsedCache map[string]interface{}

type SliceGetter

type SliceGetter struct {
	// contains filtered or unexported fields
}

func NewSliceGetter

func NewSliceGetter(length int) *SliceGetter

func (*SliceGetter) Get

func (g *SliceGetter) Get(context.Context, interface{}) (interface{}, bool, error)

func (*SliceGetter) New

func (g *SliceGetter) New([]byte) (interface{}, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL