Documentation
¶
Overview ¶
Package structmapper provides functionality for mapping arbitrary structs to map[string]interface{} and mapping map[string]interface{} back to arbitrary structs.
Index ¶
Examples ¶
Constants ¶
const DefaultTagName = "mapper"
DefaultTagName defines the default tag name used by Mapper
Variables ¶
var ( // ErrTagNameEmpty designates that the passed tag name is empty ErrTagNameEmpty = errors.New("Tag name is empty") // ErrNotAStruct designates that the passed value is not a struct ErrNotAStruct = errors.New("Now a struct") // ErrInvalidMap designates that the passed value is not a valid map ErrInvalidMap = errors.New("Invalid map") // ErrFieldIsInterface designates that a field is an interface ErrFieldIsInterface = errors.New("Field is interface") // ErrMapIsNil designates that the passed map is nil ErrMapIsNil = errors.New("Map is nil") // ErrNotAStructPointer designates that the passed value is not a pointer to a struct ErrNotAStructPointer = errors.New("Not a struct pointer") )
Functions ¶
func IsNilOrEmpty ¶
IsNilOrEmpty checks if a passed interface is either nil or of the type's zero value.
The zero value depends on the passed type. For example, the zero value of a string is an empty string.
Types ¶
type InvalidTag ¶
type InvalidTag struct {
// contains filtered or unexported fields
}
InvalidTag is an error that indicates that the tag value was invalid
func IsInvalidTag ¶
func IsInvalidTag(err error) (*InvalidTag, bool)
IsInvalidTag checks if the given error is an InvalidTag error and returns the InvalidTag error along with a boolean that defines if it is indeed an invalid tag error. The returned *InvalidTag may be nil, if the flag is false
func (*InvalidTag) Error ¶
func (it *InvalidTag) Error() string
Error returns the error string and causes InvalidTag to implement the error interface
type Mapper ¶
type Mapper struct {
// contains filtered or unexported fields
}
Mapper provides the mapping logic
Example (Roundtrip) ¶
type ExampleStruct struct {
A string
B int `mapper:"bee"`
C []string
D map[string]int
E net.IP
Ignored string `mapper:"-"`
}
// Create new structmapper instance with default configuration
sm, err := structmapper.NewMapper()
if err != nil {
panic(err)
}
source := &ExampleStruct{
A: "a",
B: 5,
C: []string{"c0", "c1"},
D: map[string]int{
"d0": 1,
"d1": 2,
},
E: net.ParseIP("127.0.0.1"),
Ignored: "should be ignored",
}
// Convert to map
m, err := sm.ToMap(source)
if err != nil {
panic(err)
}
target := &ExampleStruct{}
// Convert back to struct
if err := sm.ToStruct(m, target); err != nil {
panic(err)
}
fmt.Printf("A=%s,B=%d,C=%s,D=d0=%d,d1=%d,E=%s,Ignored=%s", target.A, target.B, strings.Join(target.C, ","),
target.D["d0"], target.D["d1"], target.E, target.Ignored)
Output: A=a,B=5,C=c0,c1,D=d0=1,d1=2,E=127.0.0.1,Ignored=
func NewMapper ¶
NewMapper initializes a new mapper instance. Optionally Mapper options may be passed to this function
func (*Mapper) ToMap ¶
ToMap takes a source struct and maps its values onto a map[string]interface{}, which is then returned.
Example ¶
type ExampleStruct struct {
A string
B int `mapper:"bee"`
C []string
D map[string]int
E net.IP
Ignored string `mapper:"-"`
}
// Create new structmapper instance with default configuration
sm, err := structmapper.NewMapper()
if err != nil {
panic(err)
}
source := &ExampleStruct{
A: "a",
B: 5,
C: []string{"c0", "c1"},
D: map[string]int{
"d0": 1,
"d1": 2,
},
E: net.ParseIP("127.0.0.1"),
Ignored: "should be ignored",
}
m, err := sm.ToMap(source)
if err != nil {
panic(err)
}
c := []string{}
for _, v := range m["C"].([]interface{}) {
c = append(c, v.(string))
}
d := m["D"].(map[interface{}]interface{})
fmt.Printf("A=%s,B=%d,C=%s,D=d0=%d,d1=%d,E=%s,Ignored=%s", m["A"], m["bee"], strings.Join(c, ","),
d["d0"], d["d1"], m["E"], m["Ignored"])
Output: A=a,B=5,C=c0,c1,D=d0=1,d1=2,E=127.0.0.1,Ignored=%!s(<nil>)
func (*Mapper) ToStruct ¶
ToStruct takes a source map[string]interface{} and maps its values onto a target struct.
Example ¶
type ExampleStruct struct {
A string
B int `mapper:"bee"`
C []string
D map[string]int
E net.IP
Ignored string `mapper:"-"`
}
// Create new structmapper instance with default configuration
sm, err := structmapper.NewMapper()
if err != nil {
panic(err)
}
source := map[string]interface{}{
"A": "a",
"bee": 5,
"C": []string{"c0", "c1"},
"D": map[interface{}]interface{}{
"d0": 1,
"d1": 2,
},
"E": "127.0.0.1",
}
target := &ExampleStruct{}
if err := sm.ToStruct(source, target); err != nil {
panic(err)
}
fmt.Printf("A=%s,B=%d,C=%s,D=d0=%d,d1=%d,E=%s,Ignored=%s", target.A, target.B, strings.Join(target.C, ","),
target.D["d0"], target.D["d1"], target.E, target.Ignored)
Output: A=a,B=5,C=c0,c1,D=d0=1,d1=2,E=127.0.0.1,Ignored=