Skip to content

worldline-go/struct2

Repository files navigation

struct2

License Coverage GitHub Workflow Status Go Report Card Go PKG

This repository helps to work with struct, convert map and get information about that.

This is a modified version of common struct to map libraries with cool features.

Usage

go get github.com/worldline-go/struct2

Map

Get decoder and run Map method, default looking the struct tag in struct.

Supported tags: -, omitempty, string, ptr2, omitnested, flatten, remain.

Convertion order is -, omitempty, string, ptr2, custom hook function, hooker interface, omitnested + flatten

type ColorGroup struct {
    ID     int      `db:"id"`
    Name   string   `db:"name"`
    Colors []string `db:"colors"`
    // custom type with implemented Hooker interface
    // covertion result to time.Time
    Date types.Time `db:"time"`
	// RGB unknown type but to untouch it add omitnested to keep that struct type
	RGB *rgb `db:"rgb,omitempty,omitnested"`
}

//...

// default tagName is `struct`
decoder := struct2.Decoder{
    TagName: "db",
}

// get map[string]interface{}
result := decoder.Map(group)

// or use one line
// result := (&struct2.Decoder{}).SetTagName("db").Map(group) // default tag name is "struct"

Custom decoder can be use in struct which have struct2.Hooker interface.
Or set a slice of custom struct2.HookFunc functions in decoder.

Check documentation examples.

Tags Information

omitnested: very helpful to don't want to touch data.

ptr2: convert pointer to the concrete value. If pointer is nil, new empty value is generated. ptr2 to effect custom hook functions and hooker interface also omitnested.

remain: Must be defined as map[string]interface{} in struct. Puts all unknown fields, destined for the struct into the remain field.

Decode

Decode is working almostly same as the mitchellh/mapstructure repo.

Default tag is struct in struct.

decoder := struct2.Decoder{
    WeaklyTypedInput: true,
    WeaklyIgnoreSeperator: true,
    TagName: "struct",
    BackupTagName: "json",
}

//...

// input and output could be any, output should be pointer
if err := d.Decode(input, output); err != nil {
    return err
}

Inspired Projects

When starting this project, I want to make it from scratch and I can learn more and make some new features but codes turning to fatih/structs repo and they are solved lots of problems so I copied parts in there and add some features as hook functions. After that I want to extend that one to make map to struct operations. In that time, I see I need to check all types due to mixing types together. So I copied parts in mitchellh/mapstructure. Thanks for all people to make them.

fatih/structs
mitchellh/mapstructure