construct icon indicating copy to clipboard operation
construct copied to clipboard

Use generic optional type instead of pointers for changeset

Open hlubek opened this issue 3 years ago • 1 comments

Bulding pointers to pointers can be tricky and need some extra lines of code.

We could use a generic Optional type behind some command flag:

type Optional[T any] struct {
	Valid bool
	Value T
}

func NewValidOptional[T any](value T) Optional[T] {
	return Optional[T]{
		Valid: true,
		Value: value,
	}
}

func NewEmptyOptional[T any]() Optional[T] {
	return Optional[T]{
		Valid: false,
	}
}

func init() {
	v1 := NewValidOptional(uuid.Nil)
	v2 := NewEmptyOptional[uuid.UUID]()
	
}

hlubek avatar Dec 06 '22 15:12 hlubek

Idea: generate chainable API (e.g. WithID(id uuid.UUID)) that takes care of taking the pointer. If a field is a pointer value (e.g. *time.Time), we could generate a WithMyFieldValue(value time.Time) method that sets the pointer to a pointer to a time.Time in the changeset.

hlubek avatar Apr 12 '23 09:04 hlubek