Skip to content

Feature: In factory *Func() mods, having a way to know which factory type we're being called from #450

Open
@jessdwitch

Description

@jessdwitch

When you call a *Func() mod like IDFunc(), it'd be nice to know if the caller is a Build(), BuildMany(), BuildSetter() or BuildManySetter(). I think the straightforward way to do this would be to pass in an enum, like FactoryType:

type FactoryType int

const (
	FactoryBuild = iota
	FactoryBuildMany
	...
)

and then the *Func functions become: *Func(func(FactoryType) T), e.g. IDFunc(func(FactoryType) int32)

There's at least 2 scenarios this could be useful for:

  • In the case of Manys, you'd be able to create sequences, like incremented IDs (you'd have to track the index outside of the function, but I think generally for unit testing, you'd want to keep that at the test level anyway for use in assertions).
  • For nested models where the you need to create the child in the middle of a service function, there's currently no way to have the ID unset for BuildSetter() but have it set to a non-0 value when you call Build() without having to create the object, store it, then modify it, which feels like it defeats the purpose of having a factory.

Edit: I goofed midway down and said Build when I meant Func, sorry!

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions