govertible
A lightweight package to convert similar structures to and from each other.
govertible will convert matching fields names of identical types from a source to a destination struct. ConvertTo()
and ConvertFrom()
are implementable and allow for custom conversions as shown in the examples below.
ConvertibleTo
type ConvertibleTo interface {
ConvertTo(interface{}) (bool, error)
}
ConvertibleFrom
type ConvertibleTo interface {
ConvertTo(interface{}) (bool, error)
}
Examples
Simple example converting one struct to another by field names without implementing an interface.
package main
import (
"fmt"
"github.com/obsius/govertible"
)
type employee struct {
Name string
Phone []byte
ID uint64
}
type person struct {
Name string
Phone []byte
Age int
}
func main() {
person := person{
Name: "El Capitan",
Phone: []byte("123-456-7890"),
Age: 20,
}
employee := employee{}
// convert a person struct to an employee struct
govertible.Convert(&person, &employee)
fmt.Println(employee)
}
Advanced example converting one struct to another using the convertTo interface.
package main
import (
"fmt"
"github.com/obsius/govertible"
)
type employee struct {
Name *string
Alias string
Phone []byte
ID uint64
}
type person struct {
Name string
Alias string
Phone []byte
Age int
}
func (this *person) ConvertTo(val interface{}) (bool, error) {
switch val.(type) {
case *employee:
v := val.(*employee)
govertible.ConvertFields(this, val)
v.ID = uint64(this.Age)
break
}
return false, nil
}
func main() {
person := person{
Name: "El Capitan",
Alias: "The Chief",
Phone: []byte("123-456-7890"),
Age: 10,
}
employee := employee{}
// convert a person struct to an employee struct
govertible.Convert(&person, &employee)
fmt.Println(employee)
}
Advanced example converting one struct to another using the convertFrom interface.
package main
import (
"fmt"
"github.com/obsius/govertible"
)
type employee struct {
Name *string
Alias string
Phone []byte
ID uint64
}
type person struct {
Name string
Alias string
Phone []byte
Age int
}
func (this *employee) ConvertFrom(val interface{}) (bool, error) {
switch val.(type) {
case *person:
v := val.(*person)
govertible.ConvertFields(val, this)
this.ID = uint64(v.Age)
break
}
return false, nil
}
func main() {
person := person{
Name: "El Capitan",
Alias: "The Chief",
Phone: []byte("123-456-7890"),
Age: 10,
}
employee := employee{}
// convert a person struct to an employee struct
govertible.Convert(&person, &employee)
fmt.Println(employee)
}
Benchmarks
operation | ns/op | # operations | total time |
---|---|---|---|
ConvertStruct | 22,850 | 100k | 2.5s |