partial

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

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

Go to latest
Published: Aug 15, 2022 License: MIT Imports: 1 Imported by: 0

README

Partial

Using Go 1.18's generics, this models a partial struct:

type MyStruct struct {
  Thing1 string
  Thing2 string
}

whole := MyStruct{
  Thing1: "hello",
}
partStruct := partial.New(whole).Without("Thing2")

Why is this useful?

Sometimes you want to do partial updates, or partial matches on a struct. In the above example whole.Thing2 is initialised to "", but really we just don't know what it is. By using a Partial that explicitly says "I only know about Thing1", we can safely apply partStruct as a database update for example.

This is also useful for matching things in tests: you might not care about the value in Thing2, and just want to match on Thing1.

Generators

Two generators are included. To use them, install them with

go install github.com/incident-io/partial/cmd/partial

Then add a go:generate comment to each package that contains relevant structs:

//go:generate partial

Within that package, annotate each struct that you want a matcher or builder for with:

// codegen-partial:builder,matcher
type MyStruct struct {
  ...
}
Builder

The builder generated lets you build up a partial of the given struct. For example:

//go:generate partial
package things

// codegen-partial:builder
type MyStruct struct {
  Thing1 string
  Thing2 string
}

will generate a builder that you can use like so:

partStruct := things.MyStructBuilder(
  things.MyStructBuilder.Thing1("hello"),
)

Because the builder is generated, you get type checking and autocompletion.

Matcher

The matcher produces Gomega matchers, that let you match on part of the struct. If we update the comment in the above example to

// codegen-partial:builder,matcher

we can then match as follows:

Expect(myStruct).To(things.MyStructMatcher(
  things.MyStructMatcher.Thing1("hello"),
))

This will ignore any value in myStruct.Thing2.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Partial

type Partial[T any] struct {
	Subject    T
	FieldNames []string `json:"-"`
	// contains filtered or unexported fields
}

Partial wraps a domain object of type T, and maintains a list of columns that have been set for the model.

Tracking columns allows us to control which fields we wish to create or update when calling gorm functions via the Querier, avoiding an issue with default field values and accidentally including columns in queries.

func New

func New[T any](subjectPtr *T) (model Partial[T], err error)

New builds a model from a domain object, tracking all the JSON fields of the model.

This should be used only for objects loaded from the database, where we know all the fields are populated correctly. It should not be used with user constructed domain objects, as those should be built directly into Partial's using their codegen'd builders.

func (Partial[T]) Add

func (m Partial[T]) Add(opts ...func(*T) []string) Partial[T]

Add returns a new Partial with additional setters, taking precendence over whatever was previously set.

func (Partial[T]) Apply

func (m Partial[T]) Apply(base T) *T

func (Partial[T]) Empty

func (m Partial[T]) Empty() bool

func (Partial[T]) Match

func (m Partial[T]) Match(otherPtr *T) bool

Match checks if the given object matches against the fields that are set on the tracked model.

This helps check if applying the changes tracked in the model would result in any change, and is useful to check when building idempotent update methods.

func (Partial[T]) Merge

func (m Partial[T]) Merge(other Partial[T]) Partial[T]

Merge combines one Partial with another of the same type, with the other fields taking precedence.

func (*Partial[T]) SetApply

func (m *Partial[T]) SetApply(apply func(T) *T)

func (Partial[T]) Without

func (m Partial[T]) Without(fieldNamesToRemove ...string) Partial[T]

Without removes the given field names from the model, causing these fields to be excluded from any queries.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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