FAQ
Hi list:

I'm now building a tcp server, use https://github.com/ugorji/go to
serilize/deserialize all messages between clients and server, I have some
question on converting the decoded "interface" to correct types.

here's the example:

// client will send following 2 kinds of message to server
type HeartbeatMsg struct {
     Msgid int
     Seq int
}

type LoginMsg struct {
     Msgid int
     Username string
     Password string
}

// handle hearbeat msg
func handlHeartbeat(msg *HeartbeatMsg , conn net.Conn) {

}

// handle login msg
func handleLogin(msg *LoginMsg,conn net.Conn) {

}

func handleMsg(conn net.Conn) {
     var MsgpackHandle codec.MsgpackHandle
     // create a decoder for conn
     d := codec.NewDecoder(conn, &golib.MsgpackHandle)

     for {
         // use interface because we don't know what exactely client send to
us
         var imsg interface{}
         err := d.Decode(&msg)

         // what can I do here to convert imsg to HeartbeatMsg or LoginMsg?
      }
}



Currently, I know there is Msgid of int at the begining of client message,
I can access imsg with reflect to get that value, then make a deep copy of
imsg to destination.
But there are cases that client messages don't share a COMMON HEADER, there
should be a clean way to archive this.

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Giulio Iotti at Dec 9, 2015 at 12:25 pm

    On Tuesday, December 8, 2015 at 4:33:47 PM UTC+1, ZPL wrote:
    Currently, I know there is Msgid of int at the begining of client message,
    I can access imsg with reflect to get that value, then make a deep copy of
    imsg to destination.
    But there are cases that client messages don't share a COMMON HEADER,
    there should be a clean way to archive this.
    The clean way is not to use reflection and make a real
    interface: https://play.golang.org/p/crVgeYVU6v

    --
    Giulio Iotti


    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • 刘志平 at Dec 9, 2015 at 1:28 pm
    thanks Giulio, but it's different.
    In my case, imsg is "pure" interface, not a reference to user defined
    struct, so I can't use type assert on imsg.

    2015-12-09 20:25 GMT+08:00 Giulio Iotti <dullgiulio@gmail.com>:
    On Tuesday, December 8, 2015 at 4:33:47 PM UTC+1, ZPL wrote:

    Currently, I know there is Msgid of int at the begining of client
    message, I can access imsg with reflect to get that value, then make a deep
    copy of imsg to destination.
    But there are cases that client messages don't share a COMMON HEADER,
    there should be a clean way to archive this.
    The clean way is not to use reflection and make a real interface:
    https://play.golang.org/p/crVgeYVU6v

    --
    Giulio Iotti


    --
    You received this message because you are subscribed to a topic in the
    Google Groups "golang-nuts" group.
    To unsubscribe from this topic, visit
    https://groups.google.com/d/topic/golang-nuts/YtyFfj6c0R8/unsubscribe.
    To unsubscribe from this group and all its topics, send an email to
    golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.


    --
    刘志平

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • MODCLOUD LU at Dec 9, 2015 at 1:28 pm
    sorry I used wrong email address to reply.

    2015-12-09 21:26 GMT+08:00 刘志平 <liuzp@cylan.com.cn>:
    thanks Giulio, but it's different.
    In my case, imsg is "pure" interface, not a reference to user defined
    struct, so I can't use type assert on imsg.

    2015-12-09 20:25 GMT+08:00 Giulio Iotti <dullgiulio@gmail.com>:
    On Tuesday, December 8, 2015 at 4:33:47 PM UTC+1, ZPL wrote:

    Currently, I know there is Msgid of int at the begining of client
    message, I can access imsg with reflect to get that value, then make a deep
    copy of imsg to destination.
    But there are cases that client messages don't share a COMMON HEADER,
    there should be a clean way to archive this.
    The clean way is not to use reflection and make a real interface:
    https://play.golang.org/p/crVgeYVU6v

    --
    Giulio Iotti


    --
    You received this message because you are subscribed to a topic in the
    Google Groups "golang-nuts" group.
    To unsubscribe from this topic, visit
    https://groups.google.com/d/topic/golang-nuts/YtyFfj6c0R8/unsubscribe.
    To unsubscribe from this group and all its topics, send an email to
    golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.


    --
    刘志平


    --
    --刘志平

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Konstantin Shaposhnikov at Dec 9, 2015 at 1:36 pm
    switch m := msg.(type) {case HeartbeatMsg:
         // m is a HeartbeatMsgcase LoginMsg:
         // m is a LoginMsgdefault:
         // m is some other type that we didn't name.}

    On Wednesday, 9 December 2015 21:29:09 UTC+8, ZPL wrote:

    sorry I used wrong email address to reply.

    2015-12-09 21:26 GMT+08:00 刘志平 <li...@cylan.com.cn <javascript:>>:
    thanks Giulio, but it's different.
    In my case, imsg is "pure" interface, not a reference to user defined
    struct, so I can't use type assert on imsg.

    2015-12-09 20:25 GMT+08:00 Giulio Iotti <dullg...@gmail.com <javascript:>
    :
    On Tuesday, December 8, 2015 at 4:33:47 PM UTC+1, ZPL wrote:

    Currently, I know there is Msgid of int at the begining of client
    message, I can access imsg with reflect to get that value, then make a deep
    copy of imsg to destination.
    But there are cases that client messages don't share a COMMON HEADER,
    there should be a clean way to archive this.
    The clean way is not to use reflection and make a real interface:
    https://play.golang.org/p/crVgeYVU6v

    --
    Giulio Iotti


    --
    You received this message because you are subscribed to a topic in the
    Google Groups "golang-nuts" group.
    To unsubscribe from this topic, visit
    https://groups.google.com/d/topic/golang-nuts/YtyFfj6c0R8/unsubscribe.
    To unsubscribe from this group and all its topics, send an email to
    golang-nuts...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/d/optout.


    --
    刘志平


    --
    --刘志平
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • MODCLOUD LU at Dec 9, 2015 at 2:02 pm
    Sorry I didn't make it clear, This question is related to msgpack
    seriliazation, here:
    https://github.com/ugorji/go/tree/master/codec#readme

    Take the example again:

    var MsgpackHandle codec.MsgpackHandle
    d := codec.NewDecoder(conn, MsgpackHandle)
    var msg interface{}
    d.Decode(&msg)
    logger.Info("type name:%s", reflect.ValueOf(msg).Kind())

    It will print "slice" NOT "struct". so I can't do type assertion.

    2015-12-09 21:36 GMT+08:00 Konstantin Shaposhnikov <k.shaposhnikov@gmail.com
    :
    switch m := msg.(type) {case HeartbeatMsg:
    // m is a HeartbeatMsgcase LoginMsg:
    // m is a LoginMsgdefault:
    // m is some other type that we didn't name.}

    On Wednesday, 9 December 2015 21:29:09 UTC+8, ZPL wrote:

    sorry I used wrong email address to reply.

    2015-12-09 21:26 GMT+08:00 刘志平 <li...@cylan.com.cn>:
    thanks Giulio, but it's different.
    In my case, imsg is "pure" interface, not a reference to user defined
    struct, so I can't use type assert on imsg.

    2015-12-09 20:25 GMT+08:00 Giulio Iotti <dullg...@gmail.com>:
    On Tuesday, December 8, 2015 at 4:33:47 PM UTC+1, ZPL wrote:

    Currently, I know there is Msgid of int at the begining of client
    message, I can access imsg with reflect to get that value, then make a deep
    copy of imsg to destination.
    But there are cases that client messages don't share a COMMON HEADER,
    there should be a clean way to archive this.
    The clean way is not to use reflection and make a real interface:
    https://play.golang.org/p/crVgeYVU6v

    --
    Giulio Iotti


    --
    You received this message because you are subscribed to a topic in the
    Google Groups "golang-nuts" group.
    To unsubscribe from this topic, visit
    https://groups.google.com/d/topic/golang-nuts/YtyFfj6c0R8/unsubscribe.
    To unsubscribe from this group and all its topics, send an email to
    golang-nuts...@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.


    --
    刘志平


    --
    --刘志平
    --
    You received this message because you are subscribed to a topic in the
    Google Groups "golang-nuts" group.
    To unsubscribe from this topic, visit
    https://groups.google.com/d/topic/golang-nuts/YtyFfj6c0R8/unsubscribe.
    To unsubscribe from this group and all its topics, send an email to
    golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.


    --
    --刘志平

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Giulio Iotti at Dec 9, 2015 at 3:56 pm

    On Wednesday, December 9, 2015 at 4:02:38 PM UTC+2, ZPL wrote:
    It will print "slice" NOT "struct". so I can't do type assertion.
    Then you have a slice of interface{} that you can use the type switch on:

    ms := msg.([]interface{})
    for _, m := range ms {
         // type switch as shown above...
    }

    --
    Giulio Iotti

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • ZP L at Dec 10, 2015 at 12:49 am
    yes, this might solve my problem, I'll try later!

    2015-12-09 23:56 GMT+08:00 Giulio Iotti <dullgiulio@gmail.com>:
    On Wednesday, December 9, 2015 at 4:02:38 PM UTC+2, ZPL wrote:

    It will print "slice" NOT "struct". so I can't do type assertion.
    Then you have a slice of interface{} that you can use the type switch on:

    ms := msg.([]interface{})
    for _, m := range ms {
    // type switch as shown above...
    }

    --
    Giulio Iotti

    --
    You received this message because you are subscribed to a topic in the
    Google Groups "golang-nuts" group.
    To unsubscribe from this topic, visit
    https://groups.google.com/d/topic/golang-nuts/YtyFfj6c0R8/unsubscribe.
    To unsubscribe from this group and all its topics, send an email to
    golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.


    --
    --刘志平

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Konstantin Shaposhnikov at Dec 9, 2015 at 4:08 pm
    Correct me if I am wrong but I beleive that msgpack doesn't encode the type
    of the struct that you send. You will have to do send it together with the
    message. But instead of re-implementing RPC protocol yourself maybe you can
    use an existing one, such as grpc?
    On Wednesday, 9 December 2015 22:02:38 UTC+8, ZPL wrote:

    Sorry I didn't make it clear, This question is related to msgpack
    seriliazation, here:
    https://github.com/ugorji/go/tree/master/codec#readme

    Take the example again:

    var MsgpackHandle codec.MsgpackHandle
    d := codec.NewDecoder(conn, MsgpackHandle)
    var msg interface{}
    d.Decode(&msg)
    logger.Info("type name:%s", reflect.ValueOf(msg).Kind())

    It will print "slice" NOT "struct". so I can't do type assertion.

    2015-12-09 21:36 GMT+08:00 Konstantin Shaposhnikov <k.shapo...@gmail.com
    <javascript:>>:
    switch m := msg.(type) {case HeartbeatMsg:
    // m is a HeartbeatMsgcase LoginMsg:
    // m is a LoginMsgdefault:
    // m is some other type that we didn't name.}

    On Wednesday, 9 December 2015 21:29:09 UTC+8, ZPL wrote:

    sorry I used wrong email address to reply.

    2015-12-09 21:26 GMT+08:00 刘志平 <li...@cylan.com.cn>:
    thanks Giulio, but it's different.
    In my case, imsg is "pure" interface, not a reference to user defined
    struct, so I can't use type assert on imsg.

    2015-12-09 20:25 GMT+08:00 Giulio Iotti <dullg...@gmail.com>:
    On Tuesday, December 8, 2015 at 4:33:47 PM UTC+1, ZPL wrote:

    Currently, I know there is Msgid of int at the begining of client
    message, I can access imsg with reflect to get that value, then make a deep
    copy of imsg to destination.
    But there are cases that client messages don't share a COMMON HEADER,
    there should be a clean way to archive this.
    The clean way is not to use reflection and make a real interface:
    https://play.golang.org/p/crVgeYVU6v

    --
    Giulio Iotti


    --
    You received this message because you are subscribed to a topic in the
    Google Groups "golang-nuts" group.
    To unsubscribe from this topic, visit
    https://groups.google.com/d/topic/golang-nuts/YtyFfj6c0R8/unsubscribe.
    To unsubscribe from this group and all its topics, send an email to
    golang-nuts...@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.


    --
    刘志平


    --
    --刘志平
    --
    You received this message because you are subscribed to a topic in the
    Google Groups "golang-nuts" group.
    To unsubscribe from this topic, visit
    https://groups.google.com/d/topic/golang-nuts/YtyFfj6c0R8/unsubscribe.
    To unsubscribe from this group and all its topics, send an email to
    golang-nuts...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/d/optout.


    --
    --刘志平
    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • ZP L at Dec 10, 2015 at 12:56 am
    yes. msgpack doesn't encode the type of struct, and for some reason, my
    server is not using RPC, but RPC seems to be a right choice.

    2015-12-10 0:08 GMT+08:00 Konstantin Shaposhnikov <k.shaposhnikov@gmail.com>
    :
    Correct me if I am wrong but I beleive that msgpack doesn't encode the
    type of the struct that you send. You will have to do send it together with
    the message. But instead of re-implementing RPC protocol yourself maybe you
    can use an existing one, such as grpc?
    On Wednesday, 9 December 2015 22:02:38 UTC+8, ZPL wrote:

    Sorry I didn't make it clear, This question is related to msgpack
    seriliazation, here:
    https://github.com/ugorji/go/tree/master/codec#readme

    Take the example again:

    var MsgpackHandle codec.MsgpackHandle
    d := codec.NewDecoder(conn, MsgpackHandle)
    var msg interface{}
    d.Decode(&msg)
    logger.Info("type name:%s", reflect.ValueOf(msg).Kind())

    It will print "slice" NOT "struct". so I can't do type assertion.

    2015-12-09 21:36 GMT+08:00 Konstantin Shaposhnikov <k.shapo...@gmail.com>
    :
    switch m := msg.(type) {case HeartbeatMsg:
    // m is a HeartbeatMsgcase LoginMsg:
    // m is a LoginMsgdefault:
    // m is some other type that we didn't name.}

    On Wednesday, 9 December 2015 21:29:09 UTC+8, ZPL wrote:

    sorry I used wrong email address to reply.

    2015-12-09 21:26 GMT+08:00 刘志平 <li...@cylan.com.cn>:
    thanks Giulio, but it's different.
    In my case, imsg is "pure" interface, not a reference to user defined
    struct, so I can't use type assert on imsg.

    2015-12-09 20:25 GMT+08:00 Giulio Iotti <dullg...@gmail.com>:
    On Tuesday, December 8, 2015 at 4:33:47 PM UTC+1, ZPL wrote:

    Currently, I know there is Msgid of int at the begining of client
    message, I can access imsg with reflect to get that value, then make a deep
    copy of imsg to destination.
    But there are cases that client messages don't share a COMMON
    HEADER, there should be a clean way to archive this.
    The clean way is not to use reflection and make a real interface:
    https://play.golang.org/p/crVgeYVU6v

    --
    Giulio Iotti


    --
    You received this message because you are subscribed to a topic in
    the Google Groups "golang-nuts" group.
    To unsubscribe from this topic, visit
    https://groups.google.com/d/topic/golang-nuts/YtyFfj6c0R8/unsubscribe
    .
    To unsubscribe from this group and all its topics, send an email to
    golang-nuts...@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.


    --
    刘志平


    --
    --刘志平
    --
    You received this message because you are subscribed to a topic in the
    Google Groups "golang-nuts" group.
    To unsubscribe from this topic, visit
    https://groups.google.com/d/topic/golang-nuts/YtyFfj6c0R8/unsubscribe.
    To unsubscribe from this group and all its topics, send an email to
    golang-nuts...@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.


    --
    --刘志平
    --
    You received this message because you are subscribed to a topic in the
    Google Groups "golang-nuts" group.
    To unsubscribe from this topic, visit
    https://groups.google.com/d/topic/golang-nuts/YtyFfj6c0R8/unsubscribe.
    To unsubscribe from this group and all its topics, send an email to
    golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.


    --
    --刘志平

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedDec 8, '15 at 3:39p
activeDec 10, '15 at 12:56a
posts10
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase