Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use generic optional type instead of pointers for changeset #3

Open
hlubek opened this issue Dec 6, 2022 · 1 comment
Open

Use generic optional type instead of pointers for changeset #3

hlubek opened this issue Dec 6, 2022 · 1 comment

Comments

@hlubek
Copy link
Contributor

hlubek commented Dec 6, 2022

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
Copy link
Contributor Author

hlubek commented Apr 12, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant