FAQ
Picked up https://github.com/BurntSushi/toml and seems to work well (thanks
to the author).

But how to handle a map as field of primitives? e.g. with

type A struct {
   Features map[string]string}


how would you format the toml file to ensure this could properly read in a
map such as {"key1": "val1", "key2": "val2"}? If instead we had

type A struct {
   Features map[string]B}

type B struct {
   C string
}

the toml doc:

[Bird]
   [Bird.Features.color]
   C = "white"
   [Bird.Features.name]
   C = "Polly"

[Dog]
   [Dog.Features.color]
   C = "black"
   [Dog.Features.name]
   C = "Fido"


seems like it works, but what about "anonymous" fields for maps? (or the
best workaround)



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

  • Andrew Gallant at Nov 29, 2013 at 12:33 pm
    Author here. I'm not sure what you mean by anonymous fields for maps. Could
    you give an example with with Go types? And perhaps also include the TOML
    you think should go with it.
    On Friday, November 29, 2013 1:33:47 AM UTC-5, h00gs wrote:

    Picked up https://github.com/BurntSushi/toml and seems to work well
    (thanks to the author).

    But how to handle a map as field of primitives? e.g. with

    type A struct {
    Features map[string]string}


    how would you format the toml file to ensure this could properly read in a
    map such as {"key1": "val1", "key2": "val2"}? If instead we had

    type A struct {
    Features map[string]B}

    type B struct {
    C string
    }

    the toml doc:

    [Bird]
    [Bird.Features.color]
    C = "white"
    [Bird.Features.name]
    C = "Polly"

    [Dog]
    [Dog.Features.color]
    C = "black"
    [Dog.Features.name]
    C = "Fido"


    seems like it works, but what about "anonymous" fields for maps? (or the
    best workaround)


    --
    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.
  • H00gs at Nov 29, 2013 at 1:22 pm
    Hi Andrew, thanks for the code.

    Ok, some code:

    package main

    import (
         "fmt"
         "<path to your toml pkg>"
    )

    //type A struct {
    // Features map[string]string
    //}

    type A struct {
         Features map[string]B
    }

    type B struct {
         C string
    }

    func main() {
         blob := `
    [Bird]
         [Bird.Features.color]
         C = "white"
         [Bird.Features.name]
         C = "Polly"

    [Dog]
         [Dog.Features.color]
         C = "black"
         [Dog.Features.name]
         C = "Fido"`
         var a map[string]A
         _, err := toml.Decode(blob, &a)
         fmt.Printf("Toml decode produced error: %v\n", err)
         fmt.Printf("Dog name = %v\n", a["Dog"].Features["name"].C)
    }

    So this works for me (with toml pkg location updated). But what if I want
    to replace type B with a string value in the commented out version of type
    A? How would the blob need to be formatted to eliminate B and its field C?

    hoogs

    On Friday, November 29, 2013 8:33:01 PM UTC+8, Andrew Gallant wrote:

    Author here. I'm not sure what you mean by anonymous fields for maps.
    Could you give an example with with Go types? And perhaps also include the
    TOML you think should go with it.
    On Friday, November 29, 2013 1:33:47 AM UTC-5, h00gs wrote:

    Picked up https://github.com/BurntSushi/toml and seems to work well
    (thanks to the author).

    But how to handle a map as field of primitives? e.g. with

    type A struct {
    Features map[string]string}


    how would you format the toml file to ensure this could properly read in
    a map such as {"key1": "val1", "key2": "val2"}? If instead we had

    type A struct {
    Features map[string]B}

    type B struct {
    C string
    }

    the toml doc:

    [Bird]
    [Bird.Features.color]
    C = "white"
    [Bird.Features.name]
    C = "Polly"

    [Dog]
    [Dog.Features.color]
    C = "black"
    [Dog.Features.name]
    C = "Fido"


    seems like it works, but what about "anonymous" fields for maps? (or the
    best workaround)


    --
    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.
  • Martin Schnabel at Nov 29, 2013 at 2:14 pm

    On 11/29/2013 02:22 PM, h00gs wrote:
    So this works for me (with toml pkg location updated). But what if I
    want to replace type B with a string value in the commented out version
    of type A? How would the blob need to be formatted to eliminate B and
    its field C?
    http://play.golang.org/p/g7E8rObXq1

    --
    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.
  • H00gs at Nov 29, 2013 at 2:34 pm
    Nice, perfect, problem solved.

    That really was not obvious from any documentation I could find...

    Thanks!
    hoogs
    On Friday, November 29, 2013 10:14:15 PM UTC+8, mb0 wrote:
    On 11/29/2013 02:22 PM, h00gs wrote:
    So this works for me (with toml pkg location updated). But what if I
    want to replace type B with a string value in the commented out version
    of type A? How would the blob need to be formatted to eliminate B and
    its field C?
    http://play.golang.org/p/g7E8rObXq1
    --
    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
postedNov 29, '13 at 6:33a
activeNov 29, '13 at 2:34p
posts5
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase