FAQ
Hi,

I'm writing web app in Go and I have this use case where I have a
arbitrarly deep map[string]{interface}, and I need to get a struct out of
it.
The thing is, I also need to get a different struct out of it depending on
the context.

type ArticleFull struct {
     Field1 string
     Field2 int
     Field3 []ArticleFull
}

type ArticleLight struct {
     Field1 string
     Field2 int
}

...

var data map[string]interface{}

data = GetSomeData()

if context.format == "full" {
     // I want an ArticleFull
}

else if context.format == "light" {
     // I want an ArticleLight
}


Can someone help me find a way to do what I want, or point out an
alternative ??

--
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

  • David DENG at Jul 8, 2013 at 2:22 pm
    1. Using reflection to fill the struct
        2. Or, saving the map into a memory block as a json/gob, then reading it
        out with your struct. This is somehow less efficiency, but reuse the code
        of json/gob library, which does similar things as 1.

    David
    On Monday, July 8, 2013 10:14:14 PM UTC+8, briche...@gmail.com wrote:

    Hi,

    I'm writing web app in Go and I have this use case where I have a
    arbitrarly deep map[string]{interface}, and I need to get a struct out of
    it.
    The thing is, I also need to get a different struct out of it depending on
    the context.

    type ArticleFull struct {
    Field1 string
    Field2 int
    Field3 []ArticleFull
    }

    type ArticleLight struct {
    Field1 string
    Field2 int
    }

    ...

    var data map[string]interface{}

    data = GetSomeData()

    if context.format == "full" {
    // I want an ArticleFull
    }

    else if context.format == "light" {
    // I want an ArticleLight
    }


    Can someone help me find a way to do what I want, or point out an
    alternative ??
    --
    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.
  • Mitchell Hashimoto at Jul 8, 2013 at 8:07 pm
    Hi,

    This does exactly what you're looking
    for: https://github.com/mitchellh/mapstructure

    It is used heavily in Packer (www.packer.io) so it is well maintained.

    Best,
    Mitchell

    On Monday, July 8, 2013 7:14:14 AM UTC-7, briche...@gmail.com wrote:

    Hi,

    I'm writing web app in Go and I have this use case where I have a
    arbitrarly deep map[string]{interface}, and I need to get a struct out of
    it.
    The thing is, I also need to get a different struct out of it depending on
    the context.

    type ArticleFull struct {
    Field1 string
    Field2 int
    Field3 []ArticleFull
    }

    type ArticleLight struct {
    Field1 string
    Field2 int
    }

    ...

    var data map[string]interface{}

    data = GetSomeData()

    if context.format == "full" {
    // I want an ArticleFull
    }

    else if context.format == "light" {
    // I want an ArticleLight
    }


    Can someone help me find a way to do what I want, or point out an
    alternative ??
    --
    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.
  • Briche Arnaud at Jul 9, 2013 at 11:58 am
    Hi Mitchell,

    Indeed https://github.com/mitchellh/mapstructure is exactly what I need.
    One more question though, because you seem to know a lot about reflection
    in go :

    I want to write a function that should transform arbitrary data to

    --
    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.
  • Briche Arnaud at Jul 9, 2013 at 12:05 pm
    Hi,

    Indeed, https://github.com/mitchellh/mapstructure works well for my use
    case.
    One more question though, because you seems to know a lot about reflection
    in go :

    I want to write a function that should transform arbitrary data to a given
    structure encoded in json, signature should look like : func Transform(data
    map[string]interface{}, t interface{}) []byte
    where t is in instance of the structure type a want to encode in json.

    How would you write such function ? (if it is even possible)

    I tried :

         func Encode(data map[string]interface{}, presenter interface{}) {
    t := reflect.TypeOf(presenter).Elem()

    instance := reflect.New(t).Elem().Interface()

    mapstructure.Decode(data, instance)


    b, err := json.Marshal(instance)

    if err != nil {
    panic("Cannot serialize data")
    }

    return b
    }


    but a get json where all value are empty strings.

    --
    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.
  • David DENG at Jul 9, 2013 at 1:08 pm
    Try

    instance := reflect.New(t).Interface()


    instead of

    instance := reflect.New(t).Elem().Interface()


    David
    On Tuesday, July 9, 2013 8:05:52 PM UTC+8, briche...@gmail.com wrote:

    Hi,

    Indeed, https://github.com/mitchellh/mapstructure works well for my use
    case.
    One more question though, because you seems to know a lot about reflection
    in go :

    I want to write a function that should transform arbitrary data to a given
    structure encoded in json, signature should look like : func Transform(data
    map[string]interface{}, t interface{}) []byte
    where t is in instance of the structure type a want to encode in json.

    How would you write such function ? (if it is even possible)

    I tried :

    func Encode(data map[string]interface{}, presenter interface{}) {
    t := reflect.TypeOf(presenter).Elem()

    instance := reflect.New(t).Elem().Interface()

    mapstructure.Decode(data, instance)


    b, err := json.Marshal(instance)

    if err != nil {
    panic("Cannot serialize data")
    }

    return b
    }


    but a get json where all value are empty strings.
    --
    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.
  • Briche Arnaud at Jul 9, 2013 at 1:37 pm
    It Works. Thanks David and everyone here. !

    Le mardi 9 juillet 2013 15:08:31 UTC+2, David DENG a écrit :
    Try

    instance := reflect.New(t).Interface()


    instead of

    instance := reflect.New(t).Elem().Interface()


    David
    On Tuesday, July 9, 2013 8:05:52 PM UTC+8, briche...@gmail.com wrote:

    Hi,

    Indeed, https://github.com/mitchellh/mapstructure works well for my use
    case.
    One more question though, because you seems to know a lot about
    reflection in go :

    I want to write a function that should transform arbitrary data to a
    given structure encoded in json, signature should look like : func
    Transform(data map[string]interface{}, t interface{}) []byte
    where t is in instance of the structure type a want to encode in json.

    How would you write such function ? (if it is even possible)

    I tried :

    func Encode(data map[string]interface{}, presenter interface{}) {
    t := reflect.TypeOf(presenter).Elem()

    instance := reflect.New(t).Elem().Interface()

    mapstructure.Decode(data, instance)


    b, err := json.Marshal(instance)

    if err != nil {
    panic("Cannot serialize data")
    }

    return b
    }


    but a get json where all value are empty strings.
    --
    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
postedJul 8, '13 at 2:14p
activeJul 9, '13 at 1:37p
posts7
users3
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase