https://github.com/obsius/govertible
Golang package to convert similar structs.
https://github.com/obsius/govertible
Last synced: about 1 month ago
JSON representation
Golang package to convert similar structs.
- Host: GitHub
- URL: https://github.com/obsius/govertible
- Owner: obsius
- License: mit
- Created: 2017-09-08T01:43:42.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-11-02T02:35:07.000Z (over 8 years ago)
- Last Synced: 2025-11-30T09:51:10.141Z (7 months ago)
- Language: Go
- Homepage:
- Size: 209 KB
- Stars: 4
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# govertible
[](https://godoc.org/github.com/obsius/govertible)
[](https://coveralls.io/github/obsius/govertible?branch=master)
[](https://goreportcard.com/report/obsius/govertible)
[](https://travis-ci.org/obsius/govertible)
[](https://raw.githubusercontent.com/obsius/govertible/master/LICENSE)

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
```golang
type ConvertibleTo interface {
ConvertTo(interface{}) (bool, error)
}
```
#### ConvertibleFrom
```golang
type ConvertibleTo interface {
ConvertTo(interface{}) (bool, error)
}
```
## Examples
Simple example converting one struct to another by field names without implementing an interface.
```golang
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.
```golang
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.
```golang
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