transform

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2023 License: Apache-2.0 Imports: 5 Imported by: 0

README

This package contains various functions which support the transformation of 
something into something else in a generic way.

	tomap:
		struct_to_map - create a map from a structure's exported and tagged fields

	tostruct:
		map_to_struct - populate a structure's exported and tagged fields  from 
			a map's contents.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Imap_to_struct

func Imap_to_struct(m map[string]interface{}, ustructp interface{}, tag_id string)

Accepts a map of [string]interface{}, and attempts to populate the fields in the user struct (pointer) from the values in the map. Only the fields in the struct which are tagged with the given tag ID are are affected. If the tag ID is '_' (underbar), then all fields which are external/exported are potentially affected. The struct tag takes the format:

tag_id:"tagstr"

where tagstr is used as the field name in the map to look up. If tagstr is given as an '_' (underbar) character, then the structure field name is used. Names are case sensitive.

This function supports transferring the simple types (bool, int, float, etc.) and pointers to those types from the map. It also supports structures, anonymous structures and pointers to structures, slices and maps.

func Map_to_struct

func Map_to_struct(m map[string]string, ustructp interface{}, tag_id string)

Accepts a map of [string]string, or [string]interface{}, and attempts to populate the fields in the user struct (pointer) from the values in the map. Only the fields in the struct which are tagged with the given tag ID are given are affected. If the tag ID is '_' (underbar), then all fields which are external/exported are potentially affected. The struct tag takes the format:

tag_id:"tagstr"

where tagstr is used as the field name in the map to look up. If tagstr is given as an '_' (underbar) character, then the structure field name is used. Names are case sensitive.

This function supports transferring the simple types (bool, int, float, etc.) and pointers to those types from the map. It also supports structures, anonymous structures and pointers to structures, slices and maps.

func Struct_to_map

func Struct_to_map(ustruct interface{}, tag_id string) (m map[string]string)

Accept a structure and build a map from its values. The map is [string]string, and the keys are taken from fields tagged with tags that match the tag_id string passed in.

If the tag id is map, then a tag might be `map:"xyz"` where xyz is used as the name in the map, or `map:"_"` where the structure field name is used in the map. If the tag id "_" is passed in, then all fields in the structure are captured.

This function will capture all simple fields (int, bool, float, etc.) and structures, anonynmous structures and pointers to structures. It will _NOT_ capture arrays or maps.

Types

This section is empty.

Jump to

Keyboard shortcuts

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