interfacetools

package module
v0.0.0-...-5c708af Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2015 License: BSD-2-Clause Imports: 6 Imported by: 2

README

go-interfacetools

Various tools for interacting with Go interfaces, such as copying maps to struct.

CopyOut

This function lets you copy from an interface (such as a map[string] interface{}) into the output object. The conversion types supported:

  • map[string] interface{} to map or struct
  • []interface{} to slice or array
  • base type to base type

When converting from map to struct, it uses the same tagging rules as encoding/json.

Special case: null values

Some file formats do not have a simple mechanism for representing null/nil values. A string with a value of "null" can actually be a legitimate value so it can't be assumed that such a string is null. To accomodate such input formats, an empty string is considered to be a null value if the destination is a pointer, map, struct, slice, or array.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrIncompatStruct = errors.New("Incompatible struct")
View Source
var IgnoreIncompatStruct bool = true

Ignore incompatible structs during copy (default behavior)

An example scenario is where you have a map of hybrid datatypes to be copied to a map of singular struct:

e.g. src_map = { "list": [ 1, 2, 3], "struct": { "item1": "STRING" }}
     dest_map == map[string] struct { Item1 string `json:"item`"` }

In this case dest_map does not have a "list" type so it is incompatible, therefore it is desireable to IgnoreIncompatStruct when CopyOut.

Functions

func CopyOut

func CopyOut(src interface{}, out interface{}) (err error)

Performs a deep-copy of source object to the passed in interface.

If the object is a struct, then the copy is attempted based on the tag of the json tag of the field; the same rules apply per encoding/json. If a field is not available, then a copier skips it.

func CopyOutTagged

func CopyOutTagged(src interface{}, out interface{}, tag string) (err error)

Types

type Copier

type Copier interface {
	CopyIn(interface{}) error
}

Copier is the interface implemented by objects that can decode input interfaces themselves. The Copier must copy the input data if it wishes to retain the data after returning. Unhandled input must return an error.

Jump to

Keyboard shortcuts

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