Skip to content

[Feature] Support conditional bindings #216

Open
@phroggyy

Description

@phroggyy

Is your feature request related to a problem? Please describe.

When building applications, we might want to support different database drivers, e.g using Go CDK and using the in-memory docstore for development, and mongo or firestore for production. For the scenarios, it would be useful if there was a way to conditionally generate code.

This does reduce flexibility, but eases development, especially when you are wiring up something that is unlikely to change often (e.g the database driver is unlikely to change without a re-compilation anyway).

Describe the solution you'd like

When running wire, the wire generator will execute conditionals at generation time (e.g if you have a switch on an env variable, it would do a check based on the env variable set at the time wire was run).

Describe alternatives you've considered

I have looked at the proposed solution of #205, but this still causes problems since we then are back at needing to effectively build out the whole dependency ourselves.

Additional context

Example of current conditional implementation

wire.go

wire.Build(
	conversations.NewManager,
	datastore.ProvideCollection,
	wire.Value(datastore.CollectionName("conversations")),
	datastore.ProvideContext,
)

return &conversations.ConversationManager{}, nil

datastore/store.go

func ProvideCollection(ctx context.Context, name CollectionName) (*docstore.Collection, error) {
	switch os.Getenv("DB_DRIVER") {
	case "mongo":
		return NewMongoCollection(ctx, ProvideHostName(), ProvideDatabaseName(), name)
	case "memory":
		return NewInMemoryCollection()
	}
	
	return nil, errors.New("tried to initialize invalid driver")
}

Desired configuration:

wire.go

switch os.Getenv("DB_DRIVER") {
case "mongo":
	wire.Build(
		conversations.NewManager,
		datastore.MongoProviderSet,
		wire.Value(datastore.CollectionName("conversations")),
		datastore.ProvideContext,
	)
	return &conversations.ConversationManager{}, nil
case "memory":
	wire.Build(conversations.NewManager, datastore.NewInMemoryCollection, datastore.ProvideContext)

	return &conversations.ConversationManager{}, nil
}

return nil, errors.New("tried to initialize invalid driver")

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions