goverter

package module
v0.0.0-...-ca4481b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 12, 2024 License: MIT Imports: 5 Imported by: 0

README

goverter

a "type-safe Go converter" generator

Build Status codecov Go Report Card Go Reference latest release

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 that doesn't use reflection.

InstallationCLIConfig

Features

Getting Started

  1. Ensure your go version is 1.16 or above

  2. Create a go modules project if you haven't done so already

    $ go mod init module-name
    
  3. Create your converter interface and mark it with a comment containing goverter:converter

    input.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
    }
    

    See Settings for more information.

  4. Run goverter:

    $ go run github.com/emp1re/goverter-test/cmd/goverter@latest gen ./
    

    It's recommended to use an explicit version instead of latest. See Installation and CLI for more information.

  5. goverter created a file at ./generated/generated.go, it may look like this:

    package generated
    
    import 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 Generation for more information.

Versioning

goverter uses SemVer for versioning the cli.

License

This project is licensed under the MIT License - see the LICENSE file for details

Logo by MariaLetta

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateConverters

func GenerateConverters(c *GenerateConfig) error

GenerateConverters generates converters.

Types

type GenerateConfig

type GenerateConfig struct {
	// PackagePatterns are golang package patterns to scan, required.
	PackagePatterns []string
	// WorkingDir is the working directory (usually the location of go.mod file), can be empty.
	WorkingDir string
	// Global are the global config commands that will be applied to all converters
	Global config.RawLines
	// BuildTags is a comma separated list passed to -tags when scanning for conversion interfaces.
	BuildTags string
	// OutputBuildConstraint will be added as go:build constraints to all files.
	OutputBuildConstraint string
}

GenerateConfig the config for generating a converter.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL