factory

package
v0.0.19 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2023 License: MIT Imports: 0 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ChainableModifier

func ChainableModifier[T any](initFactory func() T) func(modifiers ...func(T)) T

ChainableModifier creates a factory that produces an initial value from initFactory, then lets you change modifiers to modify that value. You're then returned the value once all modifiers have ran over it. This can be particularly useful when building mock data in tests.

func example() {
	chain := ChainableModifier(func() *pb.MyRequest {
		return &pb.MyRequest{Foo: true, Bar: false}
	})

	firstRequest := chain() // returns &pb.MyRequest{Foo: true, Bar: false}

	secondRequestWithBarTrue := chain(func(v *pb.MyRequest) {
		v.Bar = true
	}) // returns &pb.MyRequest{Foo: true, Bar: true}

	thirdRequestWithValuesSwapped := chain(func(v *pb.MyRequest) {
		v.Foo = false
	}, func(v *pb.MyRequest) {
		v.Bar = true
	}) // returns &pb.MyRequest{Foo: false, Bar: true}
}

Types

This section is empty.

Jump to

Keyboard shortcuts

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