many

package module
v0.0.0-...-e482363 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2024 License: MIT Imports: 2 Imported by: 0

README

many

A converter of schema of map[string]any and []any in Go

Usage

src:

[
  [
    {
      "a": "a1",
      "b": "b1"
    },
    {
      "a": "a2",
      "b": "b2"
    }
  ],
  [
    {
      "a": "a3",
      "b": "b3"
    },
    {
      "a": "a4",
      "b": {
        "c": "c1",
        "d": "d1"
      }
    }
  ]
]

phases:

[
  {
    "#mode": "router",
    "#array.result": "#strict.#array.#array.a"
  }
]

code:

	setter := many.New()
	var expression []map[string]any
	var src any
	_ = json.Unmarshal(expressionJSON, &expression)
	_ = json.Unmarshal(srcBys, &src)

	dst, info := setter.Set(src, nil, expression)
	bys, err := json.Marshal(dst)
	fmt.Println(string(bys), err, info)

output:

[{"result":["a1","a2"]},{"result":["a3","a4"]}] <nil> map[]

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithForArray

func WithForArray(forArray bool) func(*base.Setter)

WithForArray traverse array to get value by rest of routers when meet an array in GetByRouter.

Logic of forArray is as same as plugin 'for'. Set forArray to true allowed you to omit router 'for' in routers. On the other hand it makes the mean of router ambiguous.

func WithGetterPlugins

func WithGetterPlugins(ps []base.GetterPlugin) func(*base.Setter)

WithGetterPlugins set plugins in ps and DefaultGetterPlugins in this Many. The latter overrides the previous if they use a duplicate name.

func WithOmitempty

func WithOmitempty(omitempty bool) func(*base.Setter)

WithOmitempty delete null value in map[string]any if omitempty is true

func WithPhases

func WithPhases(phases []map[string]any) func(*base.Setter)

WithPhases set default phases in this Many. default phases is used in Set when phases is nil

func WithPluginPrefix

func WithPluginPrefix(prefix string) func(*base.Setter)

WithPluginPrefix plugin prefix is a string to mark a router string is a pointer to a plugin. Works in both setter and getter. Default value is "#"

func WithRedirectSrc

func WithRedirectSrc(redirectSrc bool) func(*base.Setter)

WithRedirectSrc set src to dst while a phase finished in Set

func WithSegmentation

func WithSegmentation(segmentation string) func(*base.Setter)

WithSegmentation segmentation is a string to split routers. Works in both setter and getter. Default value is "."

func WithSetterPlugins

func WithSetterPlugins(ps []base.SetterPlugin) func(*base.Setter)

WithSetterPlugins set plugins in ps and DefaultSetterPlugins in this Many. The latter overrides the previous if they use a duplicate name.

Types

type Many

type Many interface {
	Set(src any, dst any, phases []map[string]any) (any, map[string]any)
	Get(root, data, expression any) any
	Verify(phases []map[string]any) error
}

Many a converter of schema of map[string]any and []any

func New

func New(opts ...Option) Many

New create a Many. The methods provided by Many interface are safe to exec concurrently.

type Option

type Option func(*base.Setter)

Option to custom Many

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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