parse

package
v1.18.1-0...-81a2a87 Latest Latest
Warning

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

Go to latest
Published: May 17, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MustMatchExisting

func MustMatchExisting() func(fieldOpts) fieldOpts

MustMatchExisting Parser.Field will add an error when the old value does not match the existing value.

func Optional

func Optional() func(fieldOpts) fieldOpts

Optional stops Parser.Field or Parser.Prefix from adding an error when the expected key is missing.

Types

type Option

type Option func(fieldOpts) fieldOpts

func Validate

func Validate(fn func() []shared.FieldError) Option

Validate runs fn on the Parser.Field after unmarshalling. It has no effect when passed to Parser.Prefix.

type Parser

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

func Changes

func Changes(changes []shared.Change) *Parser

Changes constructs a new Parser for a set of changes.

func (*Parser) Consumed

func (p *Parser) Consumed() []shared.FieldError

Consumed checks the Parser has used all of the changes. It adds an error for any unparsed changes.

func (*Parser) Each

func (p *Parser) Each(fn func(int, *Parser) []shared.FieldError, required ...int) *Parser

Each will run fn with a Parser for any indexed keys. If required is specified then those indexes must exist.

Consider the changes:

{"key": "/0/thing", "old": null, "new": "a string"}
{"key": "/1/thing", "old": null, "new": "another string"}

Then to parse to a list of strings s do:

parser.Each(func(i int, p *Parser) {
	var v string
	p.Field("/thing", v)
	s = append(s, v)
	return p.Consumed()
})

func (*Parser) Errors

func (p *Parser) Errors() []shared.FieldError

Errors returns the current error list for the Parser.

func (*Parser) Field

func (p *Parser) Field(key string, existing any, opts ...Option) *Parser

Field will JSON unmarshal the specified key into existing. It will add an error if the key does not exist.

Consider the change:

{"key": "/thing", "old": null, "new": "a string"}

Then to parse to a string s do:

parser.Field("/thing", &s)

func (*Parser) OutOfRange

func (p *Parser) OutOfRange() []shared.FieldError

OutOfRange can be used with Parser.Each when the index is not in an expected range. It adds an out of range error for all changes.

func (*Parser) Prefix

func (p *Parser) Prefix(prefix string, fn func(*Parser) []shared.FieldError, opts ...Option) *Parser

Prefix will run fn with a Parser of any changes with the specified prefix. It will add an error if the prefix does not exist.

Consider the changes:

{"key": "/thing/name", "old": null, "new": "a string"}
{"key": "/thing/size", "old": null, "new": 5}

Then to parse to a Thing t do:

parser.Prefix("/thing", func(p *Parser) {
	return p.
		Field("/name", &t.Name).
		Field("/size", &t.Size).
		Consumed()
})

Jump to

Keyboard shortcuts

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