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

Generate factory function for each type #3

Closed
magnusbaeck opened this issue Oct 26, 2021 · 0 comments · Fixed by #16
Closed

Generate factory function for each type #3

magnusbaeck opened this issue Oct 26, 2021 · 0 comments · Fixed by #16
Assignees
Labels
enhancement New feature or request

Comments

@magnusbaeck
Copy link
Member

Description

When #2 gets merged we'll have basic functionality in place but creating events from scratch will be rather cumbersome since there aren't any factory functions that populate primarily meta fields with reasonable defaults. Quoting the README.md example:

var event eiffelevents.CompositionDefinedV3
event.Meta.Type = "EiffelCompositionDefinedEvent"
event.Meta.Version = "3.2.0"
event.Meta.ID = "87dac043-2e1b-41c5-833a-712833f2a613"
event.Meta.Time = time.Now().UnixMilli()

To solve this we should generate factory functions with the following example signature:

func NewCompositionDefinedV3(modifiers ...EventModifier) (*CompositionDefinedV3, error)

The EventModifier type would be a function type whose implementations could make various transforms of the newly created event. Unfortunately it's a bit icky to write such functions that work on all events since there currently aren't any common types, i.e. every single event type (and major version) has its own struct for the meta field, but maybe we can use reflection and define an interface like

type FieldSetter interface {
	SetIntField(field string, value int) error
	SetStringField(field string, value string) error
	...
}

that all struct types can implement? This is what this could look like from the client side to explicitly select a particular event version (if you don't want the latest v3.x.y):

event := eiffelevents.NewCompositionDefinedV3(eventmodifier.Version("3.1.1"))

Other useful EventModifier implementations includes those that modify meta.source.serializer, meta.source.name, meta.source.host, and meta.source.domainId.

We could also consider supporting factory factory functions, i.e. a function that returns a function that creates an event. This would be useful to avoid repeating the same set of factory configurations.

cdFactory := eiffelevents.NewCompositionDefinedV3Factory(eventmodifier.Version("3.1.1"), eventmodifier.DomainID(...))
...
event1 := cdFactory()
event2 := cdFactory()
...
eventN := cdFactory()

Motivation

This'll make it much easier for programs that create events to construct valid event structs. It'll reduce the amount of boilerplate code and the risk of bugs.

Exemplification

N/A

Benefits

See above.

Possible Drawbacks

Nothing besides more code and complexity.

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

Successfully merging a pull request may close this issue.

1 participant