jsong

package module
v0.0.0-...-10c1b39 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2023 License: MIT Imports: 14 Imported by: 0

README

jsong

Manipulate Go types as JSON objects

Documentation

Overview

Package jsong implements merging JSON objects.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrSkip = errors.New("skip")
	ErrStop = errors.New("stop")
)

Functions

func Array

func Array(v any) ([]any, bool)

func Bool

func Bool(v any) (bool, bool)

func Compare

func Compare(a, b any) int

func CutKey

func CutKey(k string) (head any, tail string, leaf bool)

func Delete

func Delete(v any, path string) any

Delete the path from the value v and return the result.

The empty path returns nil.

func Extract

func Extract(v any, path string) any

Extract the path from the value v or return nil if not present.

JSON paths are field or array indices joined by the dot character. The empty path returns the input value processed by ValueOf.

func Float64

func Float64(v any) (float64, bool)

func Glob

func Glob(v any, glob string, visitFn func(k string, v any))

func GlobKey

func GlobKey(v any, k string) []string

func GlobValues

func GlobValues(v any, k string) []any

func IsLeaf

func IsLeaf(k string) bool

func IsNull

func IsNull(v any) bool

func JoinKey

func JoinKey(base string, as ...any) string

JoinKey appends the key args to the base.

Args should be either string or int64 or else JoinKey panics.

func Map

func Map(v any) any

func Merge

func Merge(dst, src any, dstPath, srcPath string) any

Merge the field of the JSON object data with the MergeOptions. It returns the resulting JSON data or any merge errors.

func Must

func Must[T any](v T, err error) T

func MustOk

func MustOk[T any](v T, ok bool) T

func Object

func Object(v any) (map[string]any, bool)

func Sort

func Sort(vs any) any

func SortByKey

func SortByKey(vs any, key string) any

SortByKey sorts the values by extracting the key using jsong.

func String

func String(v any) (string, bool)

func ValueOf

func ValueOf(v any) any

ValueOf creates the jsong value of the input v.

It performs an operation similar to, but more efficient than:

data, _ := json.Marshal(x)
v := new(any)
json.Unmarshal(data, v)

ValueOf panics if v contains any cycles.

func Visit

func Visit(v any, visitFn func(k string, v any) error) error

Types

type AddScalar

type AddScalar struct {
	C any
}

func (AddScalar) Map

func (a AddScalar) Map(v any) any

type AnyReducer

type AnyReducer struct{ V any }

func (*AnyReducer) Add

func (a *AnyReducer) Add(v any)

func (*AnyReducer) Value

func (a *AnyReducer) Value() any

type ArrayMapper

type ArrayMapper []Mapper

func (ArrayMapper) Map

func (a ArrayMapper) Map(v any) any

type ArrayReducer

type ArrayReducer []Reducer

func (ArrayReducer) Add

func (a ArrayReducer) Add(x any)

func (ArrayReducer) Value

func (a ArrayReducer) Value() any

type ArrayRemapper

type ArrayRemapper []any

func (ArrayRemapper) Map

func (a ArrayRemapper) Map(v any) any

type Decoder

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

Decoder decodes JSONG from JSON.

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

NewDecoder creates a new JSONG decoder.

func (*Decoder) Decode

func (d *Decoder) Decode() (any, error)

type Filter

type Filter interface {
	Filter(any) bool
}

type GlobFilter

type GlobFilter struct {
	Glob string
}

func (GlobFilter) Filter

func (f GlobFilter) Filter(v any) bool

type HashReducer

type HashReducer[T comparable] struct {
	New        func() Reducer
	Hash       func(any) T
	Partitions map[T]Reducer
}

func (*HashReducer[T]) Add

func (a *HashReducer[T]) Add(x any)

func (*HashReducer[T]) Value

func (a *HashReducer[T]) Value() any

type Ident

type Ident struct{}

type KeyMatcher

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

func CompileKeyMatcher

func CompileKeyMatcher(glob string) (*KeyMatcher, error)

func (*KeyMatcher) MatchKey

func (m *KeyMatcher) MatchKey(k string) bool

type MapSeq

type MapSeq []Mapper

func (MapSeq) Map

func (a MapSeq) Map(v any) any

type Mapper

type Mapper interface {
	Map(v any) any
}

type Math2Mapper

type Math2Mapper struct {
	Fn2 func(float64, float64) float64
}

func (Math2Mapper) Map

func (a Math2Mapper) Map(v any) any

type MathMapper

type MathMapper struct {
	Fn func(float64) float64
}

func (MathMapper) Map

func (a MathMapper) Map(v any) any

type MulScalar

type MulScalar struct {
	M any
}

func (MulScalar) Map

func (a MulScalar) Map(v any) any

type NullReducer

type NullReducer struct{}

func (NullReducer) Add

func (NullReducer) Add(any)

func (NullReducer) Value

func (NullReducer) Value() any

type NumericReducer

type NumericReducer struct {
	Op ReduceOp
	// contains filtered or unexported fields
}

func (*NumericReducer) Add

func (a *NumericReducer) Add(x any)

func (*NumericReducer) Value

func (a *NumericReducer) Value() any

type ObjectFieldFilter

type ObjectFieldFilter map[string]struct{}

ObjectFieldFilter returns a Filter which returns true only if the object has all the given fields.

func (ObjectFieldFilter) Filter

func (f ObjectFieldFilter) Filter(v any) bool

type ObjectMapper

type ObjectMapper map[string]Mapper

func (ObjectMapper) Map

func (a ObjectMapper) Map(v any) any

type ObjectReducer

type ObjectReducer map[string]Reducer

func (ObjectReducer) Add

func (a ObjectReducer) Add(x any)

func (ObjectReducer) Value

func (a ObjectReducer) Value() any

type ObjectRemapper

type ObjectRemapper map[string]any

func (ObjectRemapper) Map

func (a ObjectRemapper) Map(v any) any

type PartitionReducer

type PartitionReducer struct {
	New func() Reducer
	Key string
	// contains filtered or unexported fields
}

func (*PartitionReducer) Add

func (a *PartitionReducer) Add(x any)

func (*PartitionReducer) Reset

func (a *PartitionReducer) Reset()

func (*PartitionReducer) Value

func (a *PartitionReducer) Value() any

type ReduceOp

type ReduceOp string
const (
	ReduceUndefined ReduceOp = ""
	ReduceSum       ReduceOp = "sum"
	ReduceMin       ReduceOp = "min"
	ReduceMax       ReduceOp = "max"
	ReduceAny       ReduceOp = "any"
	ReduceMean      ReduceOp = "avg"
)

type Reducer

type Reducer interface {
	Add(any)
	Value() any
}

type StringAgg

type StringAgg struct {
	strings.Builder
}

func (*StringAgg) Add

func (a *StringAgg) Add(x any)

func (*StringAgg) Value

func (a *StringAgg) Value() any

type SumReducer

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

func (*SumReducer) Add

func (a *SumReducer) Add(x any)

func (*SumReducer) Value

func (a *SumReducer) Value() any

type TrueCounter

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

func (*TrueCounter) Add

func (a *TrueCounter) Add(x any)

func (*TrueCounter) Value

func (a *TrueCounter) Value() any

Directories

Path Synopsis
Binary jsong executes a JSONG CLI command.
Binary jsong executes a JSONG CLI command.
cmd

Jump to

Keyboard shortcuts

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