FAQ
I want to be able to normalize the data in a map[string]interface{}.

So if I have a type

type A string

I want to be able to convert an instance of A in a map to a string.

So if I have something like m := map[string]interface{}{"a" : A("string
field")}

I want to be able to convert it to map[string]interface{}{"a" : "string
field"}

However, I do not want to use something like fmt.Sprintf("%v", m["a"]),
because I don't want ints or the like converted to strings.

Using reflection I'm able to go through and change each field, but since
the map is map[string]*interface{}*, I can't get the type of m["a"]
(reflect.TypeOf(m["a"]) and reflect.TypeOf(m["a"]).Kind() ==
reflect.Interface ).

Does anyone know how to check if an interface field is either a string or
any kind of (type X string) ?

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

  • Adonovan at Jul 10, 2013 at 3:04 am

    On Tuesday, 9 July 2013 17:08:18 UTC-4, lber...@gmail.com wrote:
    I want to be able to normalize the data in a map[string]interface{}.

    So if I have a type

    type A string

    I want to be able to convert an instance of A in a map to a string.

    So if I have something like m := map[string]interface{}{"a" : A("string
    field")}

    I want to be able to convert it to map[string]interface{}{"a" : "string
    field"}

    m := map[string]interface{}{"a" : A("string field")}
    m2 := make(map[string]interface{}{"a" : "string field"})
    for k, v := range m {
         m2[k] = string(v)
    }


    However, I do not want to use something like fmt.Sprintf("%v", m["a"]),
    because I don't want ints or the like converted to strings.

    Using reflection I'm able to go through and change each field, but since
    the map is map[string]*interface{}*, I can't get the type of m["a"]
    (reflect.TypeOf(m["a"]) and reflect.TypeOf(m["a"]).Kind() ==
    reflect.Interface ).

    Does anyone know how to check if an interface field is either a string or
    any kind of (type X string) ?
    --
    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.
  • Louisa Berger at Jul 10, 2013 at 5:48 pm
    This is not just a map of strings : I don't want ints or the like converted
    to strings.


    On Tue, Jul 9, 2013 at 11:04 PM, wrote:


    On Tuesday, 9 July 2013 17:08:18 UTC-4, lber...@gmail.com wrote:

    I want to be able to normalize the data in a map[string]interface{}.

    So if I have a type

    type A string

    I want to be able to convert an instance of A in a map to a string.

    So if I have something like m := map[string]interface{}{"a" : A("string
    field")}

    I want to be able to convert it to map[string]interface{}{"a" : "string
    field"}

    m := map[string]interface{}{"a" : A("string field")}
    m2 := make(map[string]interface{}{"a" : "string field"})
    for k, v := range m {
    m2[k] = string(v)
    }


    However, I do not want to use something like fmt.Sprintf("%v", m["a"]),
    because I don't want ints or the like converted to strings.

    Using reflection I'm able to go through and change each field, but since
    the map is map[string]*interface{}*, I can't get the type of m["a"]
    (reflect.TypeOf(m["a"]) and reflect.TypeOf(m["a"]).Kind() ==
    reflect.Interface ).

    Does anyone know how to check if an interface field is either a string or
    any kind of (type X string) ?
    --
    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.
  • Alan Donovan at Jul 10, 2013 at 6:05 pm

    On 10 July 2013 13:47, Louisa Berger wrote:

    This is not just a map of strings : I don't want ints or the like
    converted to strings.
    Does anyone know how to check if an interface field is either a string or
    any kind of (type X string) ?
    So if I understand you correctly, there are an arbitrary number of named
    types like A whose underlying type is string, and you don't know them a
    priori?

    In that case use reflection:

    m := map[string]interface{}{"a" : A("string field")}
    m2 := make(map[string]interface{}{"a" : "string field"})
    for k, v := range m {
         if reflect.TypeOf(v).Kind() == reflect.String {
           m2[k] = string(v)
         } else {
           m2[k] = v
         }
    }

    --
    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.
  • Zeebo at Jul 10, 2013 at 6:56 pm
    this is what type assertions/type switches are for.

    http://play.golang.org/p/5KHzDlFsqo
    On Tuesday, July 9, 2013 3:08:18 PM UTC-6, lber...@gmail.com wrote:

    I want to be able to normalize the data in a map[string]interface{}.

    So if I have a type

    type A string

    I want to be able to convert an instance of A in a map to a string.

    So if I have something like m := map[string]interface{}{"a" : A("string
    field")}

    I want to be able to convert it to map[string]interface{}{"a" : "string
    field"}

    However, I do not want to use something like fmt.Sprintf("%v", m["a"]),
    because I don't want ints or the like converted to strings.

    Using reflection I'm able to go through and change each field, but since
    the map is map[string]*interface{}*, I can't get the type of m["a"]
    (reflect.TypeOf(m["a"]) and reflect.TypeOf(m["a"]).Kind() ==
    reflect.Interface ).

    Does anyone know how to check if an interface field is either a string or
    any kind of (type X string) ?
    --
    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 11, 2013 at 7:42 am
    Use reflect.ValueOf(m["a"]).Type().Kind()==reflect.String to check.

    http://play.golang.org/p/5lbef6NeWf

    David
    On Wednesday, July 10, 2013 5:08:18 AM UTC+8, lber...@gmail.com wrote:

    I want to be able to normalize the data in a map[string]interface{}.

    So if I have a type

    type A string

    I want to be able to convert an instance of A in a map to a string.

    So if I have something like m := map[string]interface{}{"a" : A("string
    field")}

    I want to be able to convert it to map[string]interface{}{"a" : "string
    field"}

    However, I do not want to use something like fmt.Sprintf("%v", m["a"]),
    because I don't want ints or the like converted to strings.

    Using reflection I'm able to go through and change each field, but since
    the map is map[string]*interface{}*, I can't get the type of m["a"]
    (reflect.TypeOf(m["a"]) and reflect.TypeOf(m["a"]).Kind() ==
    reflect.Interface ).

    Does anyone know how to check if an interface field is either a string or
    any kind of (type X string) ?
    --
    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 10, '13 at 2:42a
activeJul 11, '13 at 7:42a
posts6
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase