creational

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2023 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Construct

func Construct[
	O Object,
	FO Option[O] | OptionFunc[O],
](
	obj O,
	opts ...FO,
) (O, error)

func ConstructExtended added in v0.1.0

func ConstructExtended[
	O ObjectExtended,
	FO OptionWithContext[O] | OptionFuncWithContext[O],
](
	ctx context.Context,
	obj O,
	opts ...FO,
) (O, error)

func ConstructWithContext

func ConstructWithContext[
	O Object,
	FO OptionWithContext[O] | OptionFuncWithContext[O],
](
	ctx context.Context,
	obj O,
	opts ...FO,
) (O, error)

func MustConstruct

func MustConstruct[
	O Object,
	FO Option[O] | OptionFunc[O],
](
	obj O,
	opts ...FO,
) O

func MustNew

func MustNew[
	O Object,
	FO Option[O] | OptionFunc[O],
](
	opts ...FO,
) O
Example

ExampleMustNew describes basic flow - just construct object. Validation and initialization are on responsibility of an engineer.

var (
	ctx = context.TODO()
	w   bytes.Buffer
)

opts := []MyStructOption{
	WithW(&w),
	WithS("foobar"),
	WithI(100),
}
obj := MustNew(opts...)

fmt.Printf("Is valid: %t\n", obj.Validate() == nil)
fmt.Printf("Is inited: %t\n", obj.Init(ctx) == nil)
fmt.Printf("%s\n", obj)
Output:

Is valid: true
Is inited: true
foobar100

func New

func New[
	O Object,
	FO Option[O] | OptionFunc[O],
](
	opts ...FO,
) (O, error)

func NewExtended added in v0.1.0

func NewExtended[
	O ObjectExtended,
	FO OptionWithContext[O] | OptionFuncWithContext[O],
](
	ctx context.Context,
	opts ...FO,
) (O, error)
Example

ExampleNewExtended describes flow guarantees that .Validate() and .Init() method succeed Methods must be present on generic type

var (
	ctx = context.TODO()
	w   bytes.Buffer
)

opts := []MyStructOptionWithContext{
	WithWCtx(&w),
	WithSCtx("foobar"),
	WithICtx(100),
}

obj, err := NewExtended(ctx, opts...)
if err != nil {
	panic(err)
}

fmt.Printf("%s\n", obj)
Output:

foobar100

func NewWithContext

func NewWithContext[
	O Object,
	FO OptionWithContext[O] | OptionFuncWithContext[O],
](
	ctx context.Context,
	opts ...FO,
) (O, error)
Example

ExampleNewWithContext describes flow where attributes requires ctx to be created. For example - http.Request as attribute

var (
	ctx = context.TODO()
	w   bytes.Buffer
)

opts := []MyStructOptionWithContext{
	WithWCtx(&w),
	WithSCtx("foobar"),
	WithICtx(100),
}

obj, err := NewWithContext(ctx, opts...)
if err != nil {
	panic(err)
}

fmt.Printf("Is valid: %t\n", obj.Validate() == nil)
fmt.Printf("Is inited: %t\n", obj.Init(ctx) == nil)
fmt.Printf("%s\n", obj)
Output:

Is valid: true
Is inited: true
foobar100

Types

type Object

type Object interface{ any }

type ObjectExtended added in v0.1.0

type ObjectExtended interface {
	Validate() error
	Init(context.Context) error
}

type Option

type Option[O Object] func(*O) error

type OptionFunc

type OptionFunc[O Object] interface {
	~func(*O) error
}

type OptionFuncWithContext

type OptionFuncWithContext[O Object] interface {
	~func(context.Context, *O) error
}

type OptionWithContext

type OptionWithContext[O Object] func(context.Context, *O) error

Jump to

Keyboard shortcuts

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