polyjson

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2024 License: MIT Imports: 4 Imported by: 0

README

PolyJSON

A library to generate code to semi-efficiently consume JSON documents that contain lists of polymorphic objects and visit the individual elements.

Please excuse the sparse documentation, this library was extracted from a different project that is not public yet.

What does it do?

Parsing a JSON document containing a list of events like the one in [[testdata/events/example.json]] can be easily implemented by having a struct with a field for every type, but every code that touches this struct must do a lot of nil-checks, error handling and has to be updated whenever a new type is added.

This library generates the struct, a visitor interface and a convenience implementation for the visitor interface.

How do I use it?

Create a struct for all type of objects in the JSON to be parsed and mark them with polyjson.Implements[...]. The parameter passed as the argument to the generic marker must not exist, as it is generated.

type FailedLogin struct {
	polyjson.Implements[Event]

	IPAddress string `json:"ip_address"`
}

The struct name will be converted to snake case and used as the json field name. You can override the generated json field tag by tagging the polyjson.Implements[...] marker.

type FailedLogin struct {
	polyjson.Implements[Event] `polyjson:"login_failed"`

	IPAddress string `json:"ip_address"`
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMultipleValues = errors.New("multiple values polyjson generated struct")
	ErrNoValue        = errors.New("no value in polyjson generated struct")
	ErrMissingTypeID  = errors.New("missing type ID field")
	ErrInvalidTypeID  = errors.New("invalid type ID value")
)

Functions

This section is empty.

Types

type AdditionalFieldMap

type AdditionalFieldMap[ValueType any] map[string]ValueType

func (AdditionalFieldMap[ValueType]) AdditionalField

func (a AdditionalFieldMap[ValueType]) AdditionalField(key string, in *jlexer.Lexer)

type Common

type Common[IF any] struct{ markers.Common }

type Implements

type Implements[IF any] struct {
	markers.Implements `json:"-"`
	Parent             *IF `json:"-"`
}

type Interface

type Interface[IF any] interface{ markers.Interface }

type TypeID

type TypeID[IF any] string

func (TypeID[IF]) TypeID

func (t TypeID[IF]) TypeID() string

Directories

Path Synopsis
cmd
gopolyjson command
att

Jump to

Keyboard shortcuts

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