jsonutils

package
v0.0.0-...-8aeb8a1 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNodeNotExist = errors.New("Inner node doesn't exist")

Functions

func JsonEscape

func JsonEscape(i string) string

func Merge

func Merge(left map[string]interface{}, right map[string]interface{}) map[string]interface{}

Merge puts all keys from the right map into the left map with deep overwriting returns merged map result

func UnmarshalConfig

func UnmarshalConfig(config interface{}, object interface{}) error

UnmarshalConfig serializes and deserializes config into the object return error if occurred

Types

type JSONPath

type JSONPath interface {
	IsEmpty() bool
	Get(obj map[string]interface{}) (interface{}, bool)
	GetAndRemove(obj map[string]interface{}) (interface{}, bool)
	Set(obj map[string]interface{}, value interface{}) error
	SetIfNotExist(obj map[string]interface{}, value interface{}) error
	SetOrMergeIfExist(obj map[string]interface{}, values map[string]interface{}) error
	String() string
	FieldName() string
}

func NewJSONPath

func NewJSONPath(path string) JSONPath

NewJSONPath return JSONPath (Single or Multiple)

type JSONPaths

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

JSONPaths supports several JSON paths Map:

"key1/key2/key3" -> JSONPath: [key1, key2, key3]
"key4/key5/key6" -> JSONPath: [key4, key5, key6]
"key7/key8/key9" -> JSONPath: [key7, key8, key9]

func NewJSONPaths

func NewJSONPaths(pathes []string) *JSONPaths

NewJSONPaths parses configuration settings and returns map of parsed recognition nodes

func (*JSONPaths) Get

func (jpa *JSONPaths) Get(event map[string]interface{}) (map[string]interface{}, bool)

Get returns values from event according to configuration settings Map:

"key1/key2/key3" -> value1
"key4/key5/key6" -> value2
"key7/key8/key9" -> value3

func (*JSONPaths) Set

func (jpa *JSONPaths) Set(event map[string]interface{}, values map[string]interface{}) error

Set puts all non nil values into event according to inner JSON paths settings

func (*JSONPaths) String

func (jpa *JSONPaths) String() string

String returns string representation of JSONPaths

type MultipleJSONPath

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

MultipleJSONPath is a struct for extracting and setting value by JSON paths with extended syntax /key1/key2||/key3/key4

func NewMultipleJSONPath

func NewMultipleJSONPath(paths []string) *MultipleJSONPath

NewMultipleJSONPath returns MultipleJSONPath instance

func (*MultipleJSONPath) FieldName

func (mjp *MultipleJSONPath) FieldName() string

FieldName returns string representation of first JSON path

func (*MultipleJSONPath) Get

func (mjp *MultipleJSONPath) Get(obj map[string]interface{}) (interface{}, bool)

Get returns first json path non empty result

func (*MultipleJSONPath) GetAndRemove

func (mjp *MultipleJSONPath) GetAndRemove(obj map[string]interface{}) (interface{}, bool)

GetAndRemove returns first json path non empty result and remove it from origin json

func (*MultipleJSONPath) IsEmpty

func (mjp *MultipleJSONPath) IsEmpty() bool

IsEmpty returns true if all paths are empty

func (*MultipleJSONPath) Set

func (mjp *MultipleJSONPath) Set(obj map[string]interface{}, value interface{}) error

Set puts value to first json path that exists:

{key1:"abc", key2:"qwe"} /key1/key3 -> not set
{key1:{key2: {key3: "qwe"}}} /key1/key3 -> not set
{key1:{key2: {key3: "qwe"}}} /key1/key2 -> set ok
{key1:{}} /key1/key2 -> set ok
{} /key1 -> set ok

returns err if value wasn't set

func (*MultipleJSONPath) SetIfNotExist

func (mjp *MultipleJSONPath) SetIfNotExist(obj map[string]interface{}, value interface{}) error

SetIfNotExist puts value into the object only if JSON path doesn't exist in other words the func DOESN'T overwrite the key with input value {key1:"abc", key2:"qwe"} /key1/key2 -> not set {key1:"abc", key2:"qwe"} /key1/key3 -> set

func (*MultipleJSONPath) SetOrMergeIfExist

func (mjp *MultipleJSONPath) SetOrMergeIfExist(obj map[string]interface{}, values map[string]interface{}) error

SetOrMergeIfExist puts value into the object if JSON path doesn't exist if JSON path exist and is a map than all values will be merged separately {key1:"abc", key2:"qwe"} /key1/key3 -> set {key1:"abc", key2:{key3:"qwe"}} /key1/key2 -> merge

func (*MultipleJSONPath) String

func (mjp *MultipleJSONPath) String() string

String returns string representation of JSON path (/key1/key2)

type SingleJSONPath

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

SingleJSONPath is a struct for extracting and setting value by JSON path

func NewSingleJSONPath

func NewSingleJSONPath(path string) *SingleJSONPath

NewSingleJSONPath return Single JSONPath (Single or Multiple)

func (*SingleJSONPath) FieldName

func (jp *SingleJSONPath) FieldName() string

FieldName returns string representation of flat field (key1_key2)

func (*SingleJSONPath) Get

func (jp *SingleJSONPath) Get(obj map[string]interface{}) (interface{}, bool)

Get returns value of json path

func (*SingleJSONPath) GetAndRemove

func (jp *SingleJSONPath) GetAndRemove(obj map[string]interface{}) (interface{}, bool)

GetAndRemove returns value of json path and remove it from origin json

func (*SingleJSONPath) IsEmpty

func (jp *SingleJSONPath) IsEmpty() bool

IsEmpty returns true if path is empty

func (*SingleJSONPath) Set

func (jp *SingleJSONPath) Set(obj map[string]interface{}, value interface{}) error

Set put value to json path with creating inner objects assume that obj can't be nil return err if value wasn't set

func (*SingleJSONPath) SetIfNotExist

func (jp *SingleJSONPath) SetIfNotExist(obj map[string]interface{}, value interface{}) error

SetIfNotExist puts value into the object only if JSON path doesn't exist in other words DOESN'T overwrite key {key1:"abc", key2:"qwe"} /key1/key3 -> set {key1:"abc", key2:"qwe"} /key1/key2 -> not set

func (*SingleJSONPath) SetOrMergeIfExist

func (jp *SingleJSONPath) SetOrMergeIfExist(obj map[string]interface{}, values map[string]interface{}) error

SetOrMergeIfExist puts value into the object if JSON path doesn't exist if JSON path exist and is a map than all values will be merged separately {key1:"abc", key2:"qwe"} /key1/key3 -> set {key1:"abc", key2:{key3:"qwe"}} /key1/key2 -> merge

func (*SingleJSONPath) String

func (jp *SingleJSONPath) String() string

String returns string representation of JSON path (/key1/key2)

Jump to

Keyboard shortcuts

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