rflgo composes new value into destination type based on the source data value. It will reflects all values
from the source, but keeps the structure, types and the properties of the destination. Both must have the same kind of type,
for example, if dest int
then the source must be source int
.
You can use the go get
method:
go get github.com/dalikewara/rflgo
- Add support for this kind of type:
map
,chan
,array
,func
Imagine you have source s []*userSource
data like this:
type roleSource struct {
Permission string
CreatedAt time.Time
}
type userSource struct {
Id int
Name string
Roles *[]roleSource
CreatedAt time.Time
}
r := &[]roleSource{
{
Permission: "create",
CreatedAt: time.Now(),
},
}
s := []*userSource{
{
Id: 1,
Name: "johndoe",
Roles: r,
CreatedAt: time.Now(),
},
{
Id: 2,
Name: "dalikewara",
Roles: r,
CreatedAt: time.Now(),
},
}
[{"Id":1,"Name":"johndoe","Roles":[{"Permission":"create","CreatedAt":"2022-09-06T18:14:33.620313918+07:00"}],"CreatedAt":"2022-09-06T18:14:33.620314256+07:00"},{"Id":2,"Name":"dalikewara","Roles":[{"Permission":"create","CreatedAt":"2022-09-06T18:14:33.620313918+07:00"}],"CreatedAt":"2022-09-06T18:14:33.620314303+07:00"}]
and you want to compose the source data into this d []*userDest
:
type roleDest struct {
Permission string
}
type userDest struct {
Name string
Roles *[]roleDest
}
var d []*userDest
you can compose it with rflgo.Compose()
:
err := rflgo.Compose(&d, s)
if err != nil {
panic(err)
}
then the d
will be the same as shown bellow:
type roleDest struct {
Permission string
}
type userDest struct {
Name string
Roles *[]roleDest
}
d := []*userDest{
{
Name: "johndoe",
Roles: &[]roleDest{
{
Permission: "create",
},
},
},
{
Name: "dalikewara",
Roles: &[]roleDest{
{
Permission: "create",
},
},
},
}
[{"Name":"johndoe","Roles":[{"Permission":"create"}]},{"Name":"dalikewara","Roles":[{"Permission":"create"}]}]
Read at CHANGELOG.md
Copyright © 2022 Dali Kewara