FAQ
Hello,

I could not find an easy way to do the following.

I have a yaml file with lists, as in example,

schools:
   - Meadow Glen
   - Forest Creek
   - Shady Grove
libraries:
   - Joseph Hollingsworth Memorial
   - Andrew Keriman Memorial


conf, err := yaml.ReadFile(appconffile)
if err != nil {
log.Fatalf("readfile(%q): %s", appconffile, err)
}

I can read value like appconf.Require('schools[1]')

but, is there a way to get the node schools and hence iterate for all
items.
I basically want a function like

func GetValues(key string)([]string, error){
...
}

where GetValues('schools') --> [Meadow Glen, Forest Creek, Shady Grove],
error

Thanks.

--
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 Jul 9, 2013 at 7:54 pm
    I've gone around in circles at various times trying to figure out a good
    API for something like this, and as such haven't committed anything to the
    repository. Here is one attempt that might set you in the right direction:

    https://gist.github.com/anonymous/5960637

    // Range runs the given closure on each element of the given list or map. If
    // the directive identifies a map, idx will be 0. The index or key will be set
    // to the appropriate values for a list or map, respectively, and the other
    // will be zero.
    //
    // Example:
    // config.Range("servers", func(i int, _ string, n node) {
    // fmt.Printf("server[%d] = %q\n", i, n.(Scalar).String())
    // })
    // config.Range("users", func(_ int, name string, n node) {
    // fmt.Printf("user[%q] = %#v\n", name, n)
    // })
    func Range(directive string, each func(idx int, key string, node
    yaml.Node)) error {
      node, err := yaml.Child(global.Root, directive)
      if err != nil {
       return fmt.Errorf("no such directive: %q: %s", directive, err)
      }

      switch n := node.(type) {
      case yaml.List:

       for i, v := range n {
        each(i, "", v)
       }
      case yaml.Map:
       for k, v := range n {
        each(0, k, v)
       }
      default:

       return fmt.Errorf("%q is not a sequence or mapping (%T)", directive, n)
      }
      return nil
    }



    Other alternatives I've considered are `func Flatten(directive string)
    []string` and `func FlattenMap(directive string) map[string][]string`.
      Input from people who are using it in the wild might help with choosing an
    API that works well. As it stands, though, everyone will basically need
    their own purpose-built helpers.
    On Mon, Jul 8, 2013 at 4:56 PM, Bsr wrote:

    Hello,

    I could not find an easy way to do the following.

    I have a yaml file with lists, as in example,


    schools:
    - Meadow Glen
    - Forest Creek
    - Shady Grove
    libraries:
    - Joseph Hollingsworth Memorial
    - Andrew Keriman Memorial


    conf, err := yaml.ReadFile(appconffile)
    if err != nil {
    log.Fatalf("readfile(%q): %s", appconffile, err)
    }

    I can read value like appconf.Require('schools[1]')

    but, is there a way to get the node schools and hence iterate for all
    items.
    I basically want a function like

    func GetValues(key string)([]string, error){
    ...
    }

    where GetValues('schools') --> [Meadow Glen, Forest Creek, Shady Grove],
    error

    Thanks.

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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJul 9, '13 at 3:45a
activeJul 9, '13 at 7:54p
posts2
users2
websitegolang.org

2 users in discussion

Bsr: 1 post Kyle Lemons: 1 post

People

Translate

site design / logo © 2022 Grokbase