Skip to content

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

License

Notifications You must be signed in to change notification settings

dalikewara/rflgo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rflgo

go.dev reference GitHub go.mod Go version GitHub tag (latest SemVer) GitHub license

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.

Getting started

Installation

You can use the go get method:

go get github.com/dalikewara/rflgo

Todos

  • Add support for this kind of type: map, chan, array, func

Usage

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"}]}]

Release

Changelog

Read at CHANGELOG.md

Credits

Copyright © 2022 Dali Kewara

License

MIT License

About

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

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published