Giter Site home page Giter Site logo

dengrandpa / goverter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jmattheis/goverter

0.0 0.0 0.0 400 KB

Generate type-safe Go converters by simply defining an interface

Home Page: https://goverter.jmattheis.de/

License: MIT License

Go 100.00%

goverter's Introduction

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 Configuration for more information.

  4. Run goverter:

    $ go run github.com/jmattheis/goverter/cmd/[email protected] ./

    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

goverter's People

Contributors

jmattheis avatar nissim-natanov-outreach avatar pavelpatrin avatar antonyc avatar ewhauser avatar francois-du-toit-hpe avatar sergeydobrodey avatar soniah avatar yasserrabee avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.