populate

package
v5.3.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2017 License: BSD-3-Clause Imports: 4 Imported by: 9

Documentation

Overview

Package populate provides function to populate Go values from an untyped interface{} soup.

Arbitrary JSON documents can be unmarshaled into an interface{} via encoding/json.Unmarshal. Hjson (human JSON) allows only this type of unmarshalling. Both produce a (slightly different) soup of interface{}, []interface{} and map[string]interface{}.

Package populate takes such a soup and populates a Go object from this soup doing sensible type conversions where appropriate:

  • Fundamental types (the various ints, bools, strings, floats) work as expected.
  • Maps work as expected.
  • Slices work as expected with one syntactical suggar: You can populate a []T from a single instance of T, the resulting slice has length 1 and contains just this T.
  • time.Durations can be populated from ints or floats (containing the duration in nanoseconds) or from strings like "2.5s" or "45ms" i.e. strings parsable by time.ParseDuration.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Lax

func Lax(dst, src interface{}) error

Lax populates dst from src. Src may contain elements which cannot be mapped to dst in which case they are ignored silently.

func Strict

func Strict(dst, src interface{}) error

Strict populates dst from src failing if elements in src cannot be mapped to dst.

Types

type Populator

type Populator interface {
	Populate(src interface{}) error
}

Populator is the interface a type can implement to provide a custom deserialisation.

Jump to

Keyboard shortcuts

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