Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jmattheis/goverter
Generate type-safe Go converters by simply defining an interface
https://github.com/jmattheis/goverter
code-generation converter copy generator go golang struct
Last synced: 2 days ago
JSON representation
Generate type-safe Go converters by simply defining an interface
- Host: GitHub
- URL: https://github.com/jmattheis/goverter
- Owner: jmattheis
- License: mit
- Created: 2021-03-09T20:39:27.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2024-04-10T17:30:05.000Z (8 months ago)
- Last Synced: 2024-05-02T02:34:14.576Z (8 months ago)
- Topics: code-generation, converter, copy, generator, go, golang, struct
- Language: Go
- Homepage: https://goverter.jmattheis.de/
- Size: 688 KB
- Stars: 442
- Watchers: 7
- Forks: 40
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
- awesome-go - goverter - Generate converters by defining an interface. (Generators / Search and Analytic Databases)
- awesome-go-extra - goverter - safe Go converters by simply defining an interface|141|15|8|2021-03-09T20:39:27Z|2022-08-01T19:46:45Z| (Generators / Utility/Miscellaneous)
- awesome-ccamel - jmattheis/goverter - Generate type-safe Go converters by defining signatures of different types. (Go)
README
goverter
a "type-safe Go converter" generator
goverter is a tool for creating type-safe converters. All you have to
do is create an interface and execute goverter. The project is meant as
alternative to [jinzhu/copier](https://github.com/jinzhu/copier) that doesn't
use reflection.[Getting Started](https://goverter.jmattheis.de/guide/getting-started) ᛫
[Installation](https://goverter.jmattheis.de/guide/install) ᛫
[CLI](https://goverter.jmattheis.de/reference/cli) ᛫
[Config](https://goverter.jmattheis.de/reference/settings)## Features
- **Fast execution**: No reflection is used at runtime
- Automatically converts builtin types: slices, maps, named types, primitive
types, pointers, structs with same fields
- [Enum support](https://goverter.jmattheis.de/guide/enum)
- [Deep copies](https://en.wikipedia.org/wiki/Object_copying#Deep_copy) per
default and supports [shallow
copying](https://en.wikipedia.org/wiki/Object_copying#Shallow_copy)
- **Customizable**: [You can implement custom converter methods](https://goverter.jmattheis.de/reference/extend)
- [Clear errors when generating the conversion methods](https://goverter.jmattheis.de/guide/error-early) if
- the target struct has unmapped fields
- types cannot be converted without losing information
- Detailed [documentation](https://goverter.jmattheis.de/) with a lot examples
- Thoroughly tested, see [our test scenarios](./scenario)## Example
Given this converter:
```go
package example// goverter:converter
type Converter interface {
ConvertItems(source []Input) []Output// goverter:ignore Irrelevant
// goverter:map Nested.AgeInYears Age
Convert(source Input) Output
}type Input struct {
Name string
Nested InputNested
}
type InputNested struct {
AgeInYears int
}
type Output struct {
Name string
Age int
Irrelevant bool
}
```Goverter will generated these conversion methods:
```go
package generatedimport example "goverter/example"
type ConverterImpl struct{}
func (c *ConverterImpl) Convert(source example.Input) example.Output {
var exampleOutput example.Output
exampleOutput.Name = source.Name
exampleOutput.Age = source.Nested.AgeInYears
return exampleOutput
}
func (c *ConverterImpl) ConvertItems(source []example.Input) []example.Output {
var exampleOutputList []example.Output
if source != nil {
exampleOutputList = make([]example.Output, len(source))
for i := 0; i < len(source); i++ {
exampleOutputList[i] = c.Convert(source[i])
}
}
return exampleOutputList
}
```See [Getting Started](https://goverter.jmattheis.de/guide/getting-started).
## Versioning
goverter uses [SemVer](http://semver.org/) for versioning the cli.
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE)
file for details_Logo by [MariaLetta](https://github.com/MariaLetta)_