config

package
v2.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2025 License: MIT Imports: 0 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Accessor

type Accessor struct {
	Get func() any
	Set func(any)
}

Accessor represents the accessor to the result nodes of JSONPath.

Example
cfg := config.Config{}
cfg.SetAccessorMode()
jsonPath, srcJSON := `$.a`, `{"a":1,"b":0}`
var src any
json.Unmarshal([]byte(srcJSON), &src)
output, err := jsonpath.Retrieve(jsonPath, src, cfg)
if err != nil {
	fmt.Printf(`type: %v, value: %v`, reflect.TypeOf(err), err)
	return
}
accessor := output[0].(config.Accessor)
srcMap := src.(map[string]any)

fmt.Printf("Get : %v\n", accessor.Get())

accessor.Set(2)
fmt.Printf("Set -> Src : %v\n", srcMap[`a`])

accessor.Set(3)
fmt.Printf("Set -> Get : %v\n", accessor.Get())

srcMap[`a`] = 4
fmt.Printf("Src -> Get : %v\n", accessor.Get())
Output:

Get : 1
Set -> Src : 2
Set -> Get : 3
Src -> Get : 4

type Config

type Config struct {
	FilterFunctions    map[string]func(any) (any, error)
	AggregateFunctions map[string]func([]any) (any, error)
	AccessorMode       bool
}

Config represents the configuration parameters.

func (*Config) SetAccessorMode

func (c *Config) SetAccessorMode()

SetAccessorMode sets a collection of accessors to the result.

Example
cfg := config.Config{}
cfg.SetAccessorMode()
jsonPath, srcJSON := `$.a`, `{"a":1,"b":0}`
var src any
json.Unmarshal([]byte(srcJSON), &src)
output, err := jsonpath.Retrieve(jsonPath, src, cfg)
if err != nil {
	fmt.Printf(`type: %v, value: %v`, reflect.TypeOf(err), err)
	return
}
accessor := output[0].(config.Accessor)
srcMap := src.(map[string]any)

fmt.Printf("Get : %v\n", accessor.Get())

accessor.Set(2)
fmt.Printf("Set -> Src : %v\n", srcMap[`a`])

accessor.Set(3)
fmt.Printf("Set -> Get : %v\n", accessor.Get())

srcMap[`a`] = 4
fmt.Printf("Src -> Get : %v\n", accessor.Get())
Output:

Get : 1
Set -> Src : 2
Set -> Get : 3
Src -> Get : 4

func (*Config) SetAggregateFunction

func (c *Config) SetAggregateFunction(id string, function func([]any) (any, error))

SetAggregateFunction sets the custom function.

Example
config := config.Config{}
config.SetAggregateFunction(`max`, func(params []any) (any, error) {
	var result float64
	for _, param := range params {
		if floatParam, ok := param.(float64); ok {
			if result < floatParam {
				result = floatParam
			}
			continue
		}
		return nil, fmt.Errorf(`type error`)
	}
	return result, nil
})
jsonPath, srcJSON := `$[*].max()`, `[1,3]`
var src any
json.Unmarshal([]byte(srcJSON), &src)
output, err := jsonpath.Retrieve(jsonPath, src, config)
if err != nil {
	fmt.Printf(`type: %v, value: %v`, reflect.TypeOf(err), err)
	return
}
outputJSON, _ := json.Marshal(output)
fmt.Println(string(outputJSON))
Output:

[3]

func (*Config) SetFilterFunction

func (c *Config) SetFilterFunction(id string, function func(any) (any, error))

SetFilterFunction sets the custom function.

Example
config := config.Config{}
config.SetFilterFunction(`twice`, func(param any) (any, error) {
	if floatParam, ok := param.(float64); ok {
		return floatParam * 2, nil
	}
	return nil, fmt.Errorf(`type error`)
})
jsonPath, srcJSON := `$[*].twice()`, `[1,3]`
var src any
json.Unmarshal([]byte(srcJSON), &src)
output, err := jsonpath.Retrieve(jsonPath, src, config)
switch err.(type) {
case errors.ErrorFunctionNotFound:
	fmt.Printf(`type: %v, value: %v`, reflect.TypeOf(err), err)
	return
}
outputJSON, _ := json.Marshal(output)
fmt.Println(string(outputJSON))
Output:

[2,6]

Jump to

Keyboard shortcuts

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