wire icon indicating copy to clipboard operation
wire copied to clipboard

wire: add "subtract" command

Open zombiezen opened this issue 7 years ago • 6 comments

If someone wants to "override" a set of providers from a larger set, it would be helpful to have the gowire tool output the minimal code to create a new provider set that "carves out" the given provider from the set. Ideal workflow:

$ gowire subtract github.com/google/go-cloud/gcp/gcpcloud.GCP github.com/google/go-cloud/gcp.ProjectID
import (
  "github.com/google/go-cloud/gcp"
  "github.com/google/go-cloud/gcp/gcpcloud"
  "github.com/google/go-cloud/wire"
)

var Set = wire.NewSet(
  gcpcloud.Services,
  gcp.CredentialsTokenSource,
  gcp.DefaultCredentials,
)

Perhaps this would include an interactive mode that shows you everything in the provider set and lets you pick which types to remove by menu, since typing type names is arduous.

zombiezen avatar May 31 '18 16:05 zombiezen

Another idea is to have wire.Subtract be a wire directive, used to wrap a ProviderSet in a wire.NewSet or a wire.Build.

Use cases might be:

  1. I want to make a Set AB that unions 2 other sets A and B, but there's a type T that's the same between A and B.
var SetAB = wire.NewSet(A, B)
  • Today, you'd get an error that there's two providers for T and you'd be kind of hosed.
  • With wire.Subtract, you could do:
var SetAB = wire.NewSet(A, wire.Subtract(B, T))
  1. I want to use an existing ProviderSet, but want to override one of the types it provides. For example, azureblob defines a Set that has NewPipeline and Options{} in it. Today, this code would produce an error because Options is provided twice, once by the injector function parameter and once by azureblob.Set.
func azureBucket(bucketName string, opts *azureblob.Options) *blob.Bucket {
  wire.Build(
    azureblob.DefaultIdentity,
    azureblob.Set,
  )

Again, you'd be kind of hosed; you'd have to explicitly add all of the providers in azureblob.Set that you need. With wire.Subtract, you would do:

func azureBucket(bucketName string, opts *azureblob.Options) *blob.Bucket {
  wire.Build(
    azureblob.DefaultIdentity,
    wire.Subtract(azureblob.Set, azureblob.Options),
  )

vangent avatar Mar 22 '19 17:03 vangent

@zombiezen Any chance this will be worked on? Will a PR be accepted if I implement this? This one seems useful for mocking purpose for me.

michaellee8 avatar Jul 12 '22 14:07 michaellee8

I have same issue here, Just want to change the timeout value in the default server's driver. :(

giautm avatar Jul 14 '22 16:07 giautm

This seems reasonable to me, but I haven't been working on Wire for a while. @vangent, WDYT?

zombiezen avatar Jul 17 '22 05:07 zombiezen

@eliben can you add the new owners for this repo?

vangent avatar Jul 18 '22 16:07 vangent

wire.Subtract sounds good, I'm happy to help review and merge a PR (new owner here :D)

jayzhuang avatar Jul 31 '22 23:07 jayzhuang