hashicorp / terraform-plugin-go

A low-level Go binding for the Terraform protocol for integrations to be built on top of.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Consider Adding Channel Direction in tf5server/tf6server WithDebug() Function Signature

bflad opened this issue · comments

terraform-plugin-go version

v0.7.0

Use cases

Downstream SDK/server implementations may already have an existing go-plugin ServeTestConfig, whose fields include channel directionality in the type:

type ServeTestConfig struct {
  // ...
  ReattachConfigCh chan<- *ReattachConfig
  CloseCh chan<- struct{}
  // ...
}

The current tf5server and tf6server implementations have a function signature that does not include the channel directionality:

func WithDebug(ctx context.Context, config chan *plugin.ReattachConfig, closeCh chan struct{}) ServeOpt { /* */ }

Since the function signature states it wants a bi-directional channel, it is unnecessarily difficult to pass the channels from the go-plugin fields through the function, which winds up in the same type anyways.

Attempted solutions

Workaround:

closeCh := make(chan struct{})
reattachConfigCh := make(chan *plugin.ReattachConfig)

go func() {
	val, ok := <-closeCh

	if ok {
		opts.TestConfig.CloseCh <- val
	}
}()

go func() {
	val, ok := <-reattachConfigCh

	if ok {
		opts.TestConfig.ReattachConfigCh <- val
	}
}()

tf5serveOpts = append(tf5serveOpts, tf5server.WithDebug(
	opts.TestConfig.Context,
	reattachConfigCh,
	closeCh),
)

Proposal

Add <- to function signature.

func WithDebug(ctx context.Context, config chan<- *plugin.ReattachConfig, closeCh chan<- struct{}) ServeOpt { /* */ }

Then the implementation becomes passthrough:

tf5serveOpts = append(tf5serveOpts, tf5server.WithDebug(
	opts.TestConfig.Context,
	opts.TestConfig.ReattachConfigCh,
	opts.TestConfig.CloseCh),
)

References

Closing as this isn't easy given the current code. I'm sure it could be simplified somehow, but its not a near-term priority.