inline

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2024 License: Unlicense Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ErrMalformedJSON  = "given malformed JSON"
	ErrNotGivenStruct = "not given a struct in the raw stream"
)

Variables

View Source
var (
	ErrGivenNonStringKey     = errors.New("given object with non-string key")
	ErrMissingInlineField    = errors.New("target struct missing specified inline field")
	ErrNotGivenMutable       = errors.New("not given something which we can assign to")
	ErrNotStructHolder       = errors.New("holder is not a struct")
	ErrInlineNotRightMap     = errors.New("target's inline field is not a map[string]interface{}")
	ErrUnsettableInlineField = errors.New("target struct's inline field not assignable")
)

Functions

func Contains

func Contains[S ~[]E, E comparable](s S, v E) bool

Contains reports whether v is present in s.

func Index

func Index[S ~[]E, E comparable](s S, v E) int

Index returns the index of the first occurrence of v in s, or -1 if not present.

func InlineUnmarshalJSON

func InlineUnmarshalJSON(target any, raw []byte) (err error)

UnmarshalJSON unmarshals a JSON object into a struct, with support for inline fields. Inline fields are fields that are not explicitly defined in the struct, but are instead stored in a map[string]interface{} field with the tag `json:"-,inline"`.

Example:

type MyStruct struct {
	ExplicitField string `json:"explicit"`
	InlineField   map[string]interface{} `json:"-,inline"`
}

func (target *MyStruct) UnmarshalJSON(raw []byte) (err error) {
	err = inline.UnmarshalJSON(target, raw)
	if err == nil {
		target.MetaData = nil
	}
	return
}

raw := []byte(`{"explicit":"value","extra":42}`)
var target MyStruct
err := json.UnmarshalJSON(&target, raw)
if err != nil {
	panic(err)
}

fmt.Println(target.ExplicitField) // "value"
fmt.Println(target.InlineField)   // map[extra:42]

Types

This section is empty.

Jump to

Keyboard shortcuts

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