Re: [go-nuts] Map of Types is impossible, is there any alternative solution?

2023-04-11 Thread Rob Pike
I had written out a message to suggest this approach but deleted it because
it still required a type switch afterwards.

Later I realized that the variant types of the message could easily be
handled by an interface, like this:

type Message interface {
  Build([]byte) Message
  Handle() error
}

The map is then of type

map[byte] func([]byte) Message

Then for each struct type required, implement the Build and Handle methods.
No switch required.
   err := theMap[theTypeByte].Build(theData).Handle()
The real answer will take a little more detail but that's the gist of an
approach that should work.

-rob


On Wed, Apr 12, 2023 at 7:55 AM burak serdar  wrote:

> You can do:
>
> var messageProcessors = map[uint8]func([]byte) Message {
>   0: processorForType0,
>   1: processorForType1,
>   ...
> }
>
> Then:
>
> output:=messageProcessors[id](payload)
>
> Of course, with the appropriate error checks.
>
> On Tue, Apr 11, 2023 at 3:13 PM Bèrto ëd Sèra 
> wrote:
>
>> I do know there's no way to use
>> var rule = map[uint8]Message{
>> 0: thisType,
>> 1: thatType,
>> 4: someOtherType,
>> .
>> }
>>
>> type Message interface {
>> 
>> Become(data []byte)
>> 
>> }
>>
>> type thisType struct {
>> data []byte
>> }
>> func (my *thisType) Become(data []byte) { }
>> type thatType struct {
>> data []byte
>> }
>> func (my *thatType) Become(data []byte) { }
>>
>> WHAT I'M TRYING TO DO IS: I receive a data packet via UDP, it has an Id
>> byte, a payload and ends with a crc32.
>> |id|payload|crc32
>>
>> Upon reception I need to encapsulate the payload into a struct, and
>> choose the right type of struct depending on the value of the ID byte.
>> Different kinds of payloads get managed by different types of structs, the
>> interface ensure all needed structs implement a common API.
>>
>> I wonder if there's a way to do what I'm trying to do without using a
>> switch. Anything as declarative as a map would make things a lot easier for
>> anyone who isn't familiar with the code.  However, I see no way to use a
>> map for this.
>>
>> Thanks in advance
>> Berto
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "golang-nuts" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to golang-nuts+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/golang-nuts/48c7bcaf-8836-44ce-9ccc-df2c53c90365n%40googlegroups.com
>> 
>> .
>>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/CAMV2RqoJk%2BpsZmVVi-enohqQzY%3Dh0uYX7b3ak0S9GkPuO%2Bemhg%40mail.gmail.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAOXNBZTOT5v91yaqP3FNXWqdrU1ObmqM9ojPabOpk58pZtfJRg%40mail.gmail.com.


Re: [go-nuts] Map of Types is impossible, is there any alternative solution?

2023-04-11 Thread burak serdar
You can do:

var messageProcessors = map[uint8]func([]byte) Message {
  0: processorForType0,
  1: processorForType1,
  ...
}

Then:

output:=messageProcessors[id](payload)

Of course, with the appropriate error checks.

On Tue, Apr 11, 2023 at 3:13 PM Bèrto ëd Sèra 
wrote:

> I do know there's no way to use
> var rule = map[uint8]Message{
> 0: thisType,
> 1: thatType,
> 4: someOtherType,
> .
> }
>
> type Message interface {
> 
> Become(data []byte)
> 
> }
>
> type thisType struct {
> data []byte
> }
> func (my *thisType) Become(data []byte) { }
> type thatType struct {
> data []byte
> }
> func (my *thatType) Become(data []byte) { }
>
> WHAT I'M TRYING TO DO IS: I receive a data packet via UDP, it has an Id
> byte, a payload and ends with a crc32.
> |id|payload|crc32
>
> Upon reception I need to encapsulate the payload into a struct, and choose
> the right type of struct depending on the value of the ID byte. Different
> kinds of payloads get managed by different types of structs, the interface
> ensure all needed structs implement a common API.
>
> I wonder if there's a way to do what I'm trying to do without using a
> switch. Anything as declarative as a map would make things a lot easier for
> anyone who isn't familiar with the code.  However, I see no way to use a
> map for this.
>
> Thanks in advance
> Berto
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/48c7bcaf-8836-44ce-9ccc-df2c53c90365n%40googlegroups.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAMV2RqoJk%2BpsZmVVi-enohqQzY%3Dh0uYX7b3ak0S9GkPuO%2Bemhg%40mail.gmail.com.


[go-nuts] Map of Types is impossible, is there any alternative solution?

2023-04-11 Thread Bèrto ëd Sèra
I do know there's no way to use
var rule = map[uint8]Message{
0: thisType,
1: thatType,
4: someOtherType,
.
}

type Message interface {

Become(data []byte)

}

type thisType struct {
data []byte
}
func (my *thisType) Become(data []byte) { }
type thatType struct {
data []byte
}
func (my *thatType) Become(data []byte) { }

WHAT I'M TRYING TO DO IS: I receive a data packet via UDP, it has an Id 
byte, a payload and ends with a crc32. 
|id|payload|crc32

Upon reception I need to encapsulate the payload into a struct, and choose 
the right type of struct depending on the value of the ID byte. Different 
kinds of payloads get managed by different types of structs, the interface 
ensure all needed structs implement a common API.

I wonder if there's a way to do what I'm trying to do without using a 
switch. Anything as declarative as a map would make things a lot easier for 
anyone who isn't familiar with the code.  However, I see no way to use a 
map for this.

Thanks in advance
Berto

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/48c7bcaf-8836-44ce-9ccc-df2c53c90365n%40googlegroups.com.