channels icon indicating copy to clipboard operation
channels copied to clipboard

Wrap for `in` channel?

Open arvenil opened this issue 6 years ago • 2 comments

Hi,

I'm a bit lost with examples. Let's say I have 3 channels:

	out1 := make(chan string, 10)
	out2 := make(chan string, 10)
	in := make(chan string, 20)

Now I would like to multiplex them: Multiplex(output SimpleInChannel, inputs ...SimpleOutChannel)

The problem is that I would need to convert them to interfaces SimpleInChannel and SimpleOutChannel. For SimpleOutChannel there is func Wrap(ch interface{}) SimpleOutChannel, there is no such function for SimpleInChannel.

In my case I create the in chan, it's not delivered from external source, so I could use func NewNativeChannel(size BufferCap) NativeChannel but the method func (ch NativeChannel) Out() <-chan interface{} returns <-chan interface{} so I can't "convert" it to chan string. In other words I would need to now read values from <-chan interface{} and push them to chan string - or I'm missing something?

What could work is if the library operates strictly on interface{} type like in the case of Wrap and use Value.Send() from reflect package instead of <- in multiplexer https://github.com/eapache/channels/blob/master/channels.go#L109.

arvenil avatar Jul 28 '17 13:07 arvenil

It is mentioned in the godoc that:

Due to limitations of Go's type system, importing this library directly is often not practical for production code.

This kind of dance around type-casting is one of the things I was referring to :)

In other words I would need to now read values from <-chan interface{} and push them to chan string - or I'm missing something?

You can use Unwrap for this.

eapache avatar Jul 28 '17 13:07 eapache

func Unwrap(input SimpleOutChannel, output interface{}) So it operates on out channel, not on in channel. It's a reverse for Wrap.

I think there should be WrapOut WrapIn UnwrapOut UnwrapIn. But to be honest the best would be to not create additional chan interface{} rather all types should be interface{} and then just check for correction with reflection if they are chan or not.

I'm writing my own multiplexer right, let's see if it works, I will share when I finish.

arvenil avatar Jul 28 '17 14:07 arvenil