FAQ
Hello,

I have to implement a function

type EventHandler func(topicURI string, event interface{})

I use the WAMP implementation
https://github.com/jcelliott/turnpike/tree/master.
In the callback function I want access values inside of the event parameter.
Using fmt.Println(event) at runtime shows the content of the event:

map[MachineType:10 MachineId:10002 MonitoringFeatures:map[Alerting:true
StateInfos:true RealtimeData:true Statistic:true VNC:false
VideoSteam:false]]

This shows that it is the JSon representation of a type in my program.
I could not find the right API call for this situation.
If a have a string representation of a JSon encoded value I can use Decode,
  but want is the best way to "decode" the interface value.

best regards


Andreas

--
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/groups/opt_out.

Search Discussions

  • Kyle Lemons at Dec 16, 2013 at 7:13 pm
    You need to type assert it:
    http://golang.org/ref/spec#Type_assertions

    That value will be (recursively) either a map[string]interface{} (for an
    object), a []interface{} (for a list) or a string/float64 (scalar). See
    the encoding/json for what happens when you decode into an interface.

    On Mon, Dec 16, 2013 at 10:55 AM, andreas wrote:

    Hello,

    I have to implement a function

    type EventHandler func(topicURI string, event interface{})

    I use the WAMP implementation
    https://github.com/jcelliott/turnpike/tree/master.
    In the callback function I want access values inside of the event
    parameter.
    Using fmt.Println(event) at runtime shows the content of the event:

    map[MachineType:10 MachineId:10002 MonitoringFeatures:map[Alerting:true
    StateInfos:true RealtimeData:true Statistic:true VNC:false
    VideoSteam:false]]

    This shows that it is the JSon representation of a type in my program.
    I could not find the right API call for this situation.
    If a have a string representation of a JSon encoded value I can use
    Decode,
    but want is the best way to "decode" the interface value.

    best regards


    Andreas

    --
    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/groups/opt_out.
    --
    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/groups/opt_out.
  • Andreas at Dec 17, 2013 at 3:02 pm
    Yes, this was the first thing what I have implemented, but it is not really
    a convenient solution.
    I was looking for a way to use JSon Encoding / Decoding to reconstruct
    values of a defined complex struct type.
    Am Montag, 16. Dezember 2013 20:12:46 UTC+1 schrieb Kyle Lemons:
    You need to type assert it:
    http://golang.org/ref/spec#Type_assertions

    That value will be (recursively) either a map[string]interface{} (for an
    object), a []interface{} (for a list) or a string/float64 (scalar). See
    the encoding/json for what happens when you decode into an interface.


    On Mon, Dec 16, 2013 at 10:55 AM, andreas <abau...@webware-experts.de<javascript:>
    wrote:
    Hello,

    I have to implement a function

    type EventHandler func(topicURI string, event interface{})

    I use the WAMP implementation
    https://github.com/jcelliott/turnpike/tree/master.
    In the callback function I want access values inside of the event
    parameter.
    Using fmt.Println(event) at runtime shows the content of the event:

    map[MachineType:10 MachineId:10002 MonitoringFeatures:map[Alerting:true
    StateInfos:true RealtimeData:true Statistic:true VNC:false
    VideoSteam:false]]

    This shows that it is the JSon representation of a type in my program.
    I could not find the right API call for this situation.
    If a have a string representation of a JSon encoded value I can use
    Decode,
    but want is the best way to "decode" the interface value.

    best regards


    Andreas

    --
    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...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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/groups/opt_out.
  • Egon at Dec 16, 2013 at 7:30 pm
    Does the json data have some particular structure or is it arbitrary?

    To me, it seems that turnpike has made a wrong decision how it handles
    unmarshaling. I.e. there is going to be a lot of conversions and
    type-handling due to that early unmarshal. It would probably make things
    easier if it passed the raw-data to the event handler.

    The shortest way to convert from one structure to another is with
    http://play.golang.org/p/ru_-AwuKnY. Obviously, this is slow.

    Alternatively you can use reflection and type checks to get to the
    necessary value: http://play.golang.org/p/Sjxl82-UNw.

    + egon

    Alternatively if it's some sort of flexible it's
    On Monday, December 16, 2013 8:55:16 PM UTC+2, andreas wrote:

    Hello,

    I have to implement a function

    type EventHandler func(topicURI string, event interface{})

    I use the WAMP implementation
    https://github.com/jcelliott/turnpike/tree/master.
    In the callback function I want access values inside of the event
    parameter.
    Using fmt.Println(event) at runtime shows the content of the event:

    map[MachineType:10 MachineId:10002 MonitoringFeatures:map[Alerting:true
    StateInfos:true RealtimeData:true Statistic:true VNC:false
    VideoSteam:false]]

    This shows that it is the JSon representation of a type in my program.
    I could not find the right API call for this situation.
    If a have a string representation of a JSon encoded value I can use
    Decode,
    but want is the best way to "decode" the interface value.

    best regards


    Andreas
    --
    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/groups/opt_out.
  • Egon at Dec 16, 2013 at 7:40 pm

    On Monday, December 16, 2013 9:30:16 PM UTC+2, egon wrote:
    Does the json data have some particular structure or is it arbitrary?

    To me, it seems that turnpike has made a wrong decision how it handles
    unmarshaling. I.e. there is going to be a lot of conversions and
    type-handling due to that early unmarshal. It would probably make things
    easier if it passed the raw-data to the event handler.

    The shortest way to convert from one structure to another is with
    http://play.golang.org/p/ru_-AwuKnY. Obviously, this is slow.

    Alternatively you can use reflection and type checks to get to the
    necessary value: http://play.golang.org/p/Sjxl82-UNw.
    Correction here is a better version: http://play.golang.org/p/tXFgGINmxI

    + egon

    Alternatively if it's some sort of flexible it's
    On Monday, December 16, 2013 8:55:16 PM UTC+2, andreas wrote:

    Hello,

    I have to implement a function

    type EventHandler func(topicURI string, event interface{})

    I use the WAMP implementation
    https://github.com/jcelliott/turnpike/tree/master.
    In the callback function I want access values inside of the event
    parameter.
    Using fmt.Println(event) at runtime shows the content of the event:

    map[MachineType:10 MachineId:10002 MonitoringFeatures:map[Alerting:true
    StateInfos:true RealtimeData:true Statistic:true VNC:false
    VideoSteam:false]]

    This shows that it is the JSon representation of a type in my program.
    I could not find the right API call for this situation.
    If a have a string representation of a JSon encoded value I can use
    Decode,
    but want is the best way to "decode" the interface value.

    best regards


    Andreas
    --
    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/groups/opt_out.
  • C Banning at Dec 17, 2013 at 11:40 am
    One of the functions MapValue(), ValuesForKey() or ValuesFromKeyPath() in
    github.com/clbanning/x2j might work, also.
    (http://godoc.org/github.com/clbanning/x2j)
    On Monday, December 16, 2013 1:40:11 PM UTC-6, egon wrote:


    On Monday, December 16, 2013 9:30:16 PM UTC+2, egon wrote:

    Does the json data have some particular structure or is it arbitrary?

    To me, it seems that turnpike has made a wrong decision how it handles
    unmarshaling. I.e. there is going to be a lot of conversions and
    type-handling due to that early unmarshal. It would probably make things
    easier if it passed the raw-data to the event handler.

    The shortest way to convert from one structure to another is with
    http://play.golang.org/p/ru_-AwuKnY. Obviously, this is slow.

    Alternatively you can use reflection and type checks to get to the
    necessary value: http://play.golang.org/p/Sjxl82-UNw.
    Correction here is a better version: http://play.golang.org/p/tXFgGINmxI

    + egon

    Alternatively if it's some sort of flexible it's
    On Monday, December 16, 2013 8:55:16 PM UTC+2, andreas wrote:

    Hello,

    I have to implement a function

    type EventHandler func(topicURI string, event interface{})

    I use the WAMP implementation
    https://github.com/jcelliott/turnpike/tree/master.
    In the callback function I want access values inside of the event
    parameter.
    Using fmt.Println(event) at runtime shows the content of the event:

    map[MachineType:10 MachineId:10002 MonitoringFeatures:map[Alerting:true
    StateInfos:true RealtimeData:true Statistic:true VNC:false
    VideoSteam:false]]

    This shows that it is the JSon representation of a type in my program.
    I could not find the right API call for this situation.
    If a have a string representation of a JSon encoded value I can use
    Decode,
    but want is the best way to "decode" the interface value.

    best regards


    Andreas
    --
    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/groups/opt_out.
  • Andreas at Dec 17, 2013 at 3:12 pm
    >>>
    Does the json data have some particular structure or is it arbitrary?
    >>>

    Yes the solution I'm looking for is related to defined complex struct type.
    I want to reconstruct the value of such a type

    >>>
    The shortest way to convert from one structure to another is with
    http://play.golang.org/p/ru_-AwuKnY. Obviously, this is slow.
    >>>

    This is a kind of solution I'm looking for, but I thought that there is a
    way to avoid complete Marshal and UnMarshal.


    Am Montag, 16. Dezember 2013 20:30:16 UTC+1 schrieb egon:
    Does the json data have some particular structure or is it arbitrary?

    To me, it seems that turnpike has made a wrong decision how it handles
    unmarshaling. I.e. there is going to be a lot of conversions and
    type-handling due to that early unmarshal. It would probably make things
    easier if it passed the raw-data to the event handler.

    The shortest way to convert from one structure to another is with
    http://play.golang.org/p/ru_-AwuKnY. Obviously, this is slow.

    Alternatively you can use reflection and type checks to get to the
    necessary value: http://play.golang.org/p/Sjxl82-UNw.

    + egon

    Alternatively if it's some sort of flexible it's
    On Monday, December 16, 2013 8:55:16 PM UTC+2, andreas wrote:

    Hello,

    I have to implement a function

    type EventHandler func(topicURI string, event interface{})

    I use the WAMP implementation
    https://github.com/jcelliott/turnpike/tree/master.
    In the callback function I want access values inside of the event
    parameter.
    Using fmt.Println(event) at runtime shows the content of the event:

    map[MachineType:10 MachineId:10002 MonitoringFeatures:map[Alerting:true
    StateInfos:true RealtimeData:true Statistic:true VNC:false
    VideoSteam:false]]

    This shows that it is the JSon representation of a type in my program.
    I could not find the right API call for this situation.
    If a have a string representation of a JSon encoded value I can use
    Decode,
    but want is the best way to "decode" the interface value.

    best regards


    Andreas
    --
    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/groups/opt_out.
  • Egon at Dec 17, 2013 at 3:29 pm

    On Tuesday, December 17, 2013 5:11:56 PM UTC+2, andreas wrote:
    Does the json data have some particular structure or is it arbitrary?
    Yes the solution I'm looking for is related to defined complex struct type.
    I want to reconstruct the value of such a type
    The shortest way to convert from one structure to another is with
    http://play.golang.org/p/ru_-AwuKnY. Obviously, this is slow.
    This is a kind of solution I'm looking for, but I thought that there is a
    way to avoid complete Marshal and UnMarshal.
    There is... you can look at how json does the unmarshal and marshal and
    merge them into one function. Alternatively just modify turnpike so that it
    gives you the raw bytes of the message and then simply unmarshal... making
    things much simpler.

    + egon



    Am Montag, 16. Dezember 2013 20:30:16 UTC+1 schrieb egon:
    Does the json data have some particular structure or is it arbitrary?

    To me, it seems that turnpike has made a wrong decision how it handles
    unmarshaling. I.e. there is going to be a lot of conversions and
    type-handling due to that early unmarshal. It would probably make things
    easier if it passed the raw-data to the event handler.

    The shortest way to convert from one structure to another is with
    http://play.golang.org/p/ru_-AwuKnY. Obviously, this is slow.

    Alternatively you can use reflection and type checks to get to the
    necessary value: http://play.golang.org/p/Sjxl82-UNw.

    + egon

    Alternatively if it's some sort of flexible it's
    On Monday, December 16, 2013 8:55:16 PM UTC+2, andreas wrote:

    Hello,

    I have to implement a function

    type EventHandler func(topicURI string, event interface{})

    I use the WAMP implementation
    https://github.com/jcelliott/turnpike/tree/master.
    In the callback function I want access values inside of the event
    parameter.
    Using fmt.Println(event) at runtime shows the content of the event:

    map[MachineType:10 MachineId:10002 MonitoringFeatures:map[Alerting:true
    StateInfos:true RealtimeData:true Statistic:true VNC:false
    VideoSteam:false]]

    This shows that it is the JSon representation of a type in my program.
    I could not find the right API call for this situation.
    If a have a string representation of a JSon encoded value I can use
    Decode,
    but want is the best way to "decode" the interface value.

    best regards


    Andreas
    --
    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/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedDec 16, '13 at 6:55p
activeDec 17, '13 at 3:29p
posts8
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase