Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unmarshaling Into an Interface{} and Then Performing Type Assertion

Tags:

go

rabbitmq

I get a string through a rabbitmq message system. Before sending,

I use json.Marshal, convert the outcome to string and send through rabbitmq.

The structs that I convert and send can be: (changed the names and the size of the structs but it should not matter)

type Somthing1 struct{    Thing        string    `json:"thing"`    OtherThing   int64     `json:"other_thing"` } 

or

type Somthing2 struct{    Croc        int       `json:"croc"`    Odile       bool      `json:"odile"` } 

The message goes through perfectly as a string and is printed on the other side (some server)

Up until now everything works. Now I'm trying to convert them back into their structs and assert the types.

first attempt is by:

func typeAssert(msg string) {   var input interface{}   json.Unmarshal([]byte(msg), &input)   switch input.(type){  case Somthing1:     job := Somthing1{}     job = input.(Somthing1)     queueResults(job)    case Somthing2:     stats := Somthing2{}     stats = input.(Somthing2)     queueStatsRes(stats)  default: } 

This does not work. When Printing the type of input after Unmarshaling it I get map[string]interface{} (?!?)

and even stranger than that, the map key is the string I got and the map value is empty.

I did some other attempts like:

 func typeAssert(msg string) {    var input interface{}    json.Unmarshal([]byte(msg), &input)    switch v := input.(type){   case Somthing1:     v = input.(Somthing1)     queueResults(v)     case Somthing2:     v = input.(Somthing2)     queueStatsRes(v)   default: } 

and also tried writing the switch like was explained in this answer: Golang: cannot type switch on non-interface value

switch v := interface{}(input).(type) 

still with no success...

Any ideas?

like image 572
Blue Bot Avatar asked Feb 23 '16 17:02

Blue Bot


1 Answers

The default types that the json package Unmarshals into are shown in the Unmarshal function documentation

bool, for JSON booleans float64, for JSON numbers string, for JSON strings []interface{}, for JSON arrays map[string]interface{}, for JSON objects nil for JSON null 

Since you're unmarshaling into an interface{}, the returned types will only be from that set. The json package doesn't know about Something1 and Something2. You either need to convert from the map[string]interface{} that the json object is being unmarshaled into, or unmarshal directly into the struct type you want.

If you don't want to do unpack the data from a generic interface, or somehow tag the data so you know what type to expect, you could iteratively take the json and try to unmarshal it into each type you want.

You can even pack those into a wrapper struct to do the unmarshaling for you:

type Something1 struct {     Thing      string `json:"thing"`     OtherThing int64  `json:"other_thing"` }  type Something2 struct {     Croc  int  `json:"croc"`     Odile bool `json:"odile"` }  type Unpacker struct {     Data       interface{} }  func (u *Unpacker) UnmarshalJSON(b []byte) error {     smth1 := &Something1{}     err := json.Unmarshal(b, smth1)      // no error, but we also need to make sure we unmarshaled something     if err == nil && smth1.Thing != "" {         u.Data = smth1         return nil     }      // abort if we have an error other than the wrong type     if _, ok := err.(*json.UnmarshalTypeError); err != nil && !ok {         return err     }      smth2 := &Something2{}     err = json.Unmarshal(b, smth2)     if err != nil {         return err     }      u.Data = smth2     return nil } 

http://play.golang.org/p/Trwd6IShDW

like image 110
JimB Avatar answered Oct 07 '22 12:10

JimB