gomutate

command module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2023 License: MIT Imports: 17 Imported by: 0

README

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.MutateYearOfBirth(2019)
mutator.MutateEmployeesAt(0).MutateName("John Smith")

and reporting changes like this:

fmt.Println(mutator.FormatChanges())

which outputs:

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

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

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

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