Giter Club home page Giter Club logo

gomutate's Introduction

gomutate

ci workflow

Generate code to mutate your Go types.

For a type like this:

type Acme struct {
 Name        string
 YearOfBirth int
 Employees   []*Employee
}

type Employee struct {
 Name     string
 Position string
 JoinedAt time.Time
}

Generates code allowing you to mutate it like this:

acme := Acme{
    Name:        "Acme Inc.",
    YearOfBirth: 2000,
    Employees: []*Employee{
        {
            Name:     "John Doe",
            Position: "CEO",
            JoinedAt: time.Date(2010, 1, 1, 0, 0, 0, 0, time.UTC),
        },
        {
            Name:     "Jane Doe",
            Position: "CTO",
            JoinedAt: time.Date(2010, 1, 1, 0, 0, 0, 0, time.UTC),
        },
    },
}

mutator := NewMutatorAcme(&acme)
mutator.SetYearOfBirth(2019)
mutator.EmployeesAt(0).SetName("John Smith")

and reporting changes like this:

fmt.Println(mutator.FormatChanges())

which outputs:

YearOfBirth Updated: 2018 -> 2019
Employees Name Updated: John Doe -> John Smith

Usage

go get github.com/pdcalado/gomutate
go run github.com/pdcalado/gomutate -type <type-name> -w <path-to-output-file> <path-to-input-file>

Input and output files must be in the same package. Omit the -w flag to print to stdout.

Features

See our tests for examples of other possibly unlisted supported operations.

  • operations with pointers and basic types are idempotent (the same mutation performed twice must only report one change)
  • a custom formatter and custom change logger can be provided
  • mutate a field with basic type
  • mutate a field with a slice or map of basic types
  • mutate a field with a slice of structs or struct pointers defined in the same package
  • mutate a field with a map of structs or struct pointers defined in the same package, including struct pointers as keys
  • append and delete from a slice
  • insert and delete from a map

Limitations

  • Only supports structs
  • Only supports chaining mutators for types defined in the same package
  • Only supports types with exported fields
  • Does not support nested slices or maps, like [][]string or map[string]map[int]string

gomutate's People

Contributors

pdcalado avatar

Stargazers

 avatar  avatar

Watchers

 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.