repositorypackage
0.0.0-20221219134934-20b57c865748
Repository: https://github.com/phogolabs/inflate.git
Documentation: pkg.go.dev
# Packages
No description provided by the author
# README
inflate
A Golang reflection package on steroids
Motivation
The project is motivated from the fact that there are no packages that convert values based on different criteria:
- Convert values based on OpenAPI parameters serialization format
- Sets the default value defined by tag attribute
- Sets the structure fields from another structure or map by using tag attribute
The library works in greedy manner. It tries to convert incompatible values as much as it can. Thanks for the inspiration to the contributors of the following projects:
Installation
$ go get -u github.com/phogolabs/inflate
Usage
The basic usage of the package gives some handy features.
If you want to convert a value from one type to another, you can use the following function:
type Order struct {
ID string `field:"order_id"`
}
type OrderItem struct {
OrderID string `field:"order_id"`
}
source := &Order{ID: "0000123"}
target := &OrderItem{}
if err := inflate.Set(target, source); err != nil {
panic(err)
}
fmt.Printf("%+v", target)
// Output: &{OrderID:0000123}
You can use the package to set the default values (if they are not set):
type Address struct {
City string `json:"city"`
Country string `json:"country"`
}
type Profile struct {
Name string `default:"John"`
Address Address `default:"{\"city\":\"London\",\"country\":\"UK\"}"`
}
profile := &Profile{}
if err := inflate.SetDefault(profile); err != nil {
panic(err)
}
fmt.Printf("%+v", profile)
// Output:
// &{Name:John Address:{City:London Country:UK}}
The package supports serialization of parameters in OpenAPI spec format. For more advanced examples, please read the online documentation.
Contributing
We are open for any contributions. Just fork the project.