Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A slice of structs #1

Open
logrusorgru opened this issue Oct 15, 2014 · 2 comments
Open

A slice of structs #1

logrusorgru opened this issue Oct 15, 2014 · 2 comments

Comments

@logrusorgru
Copy link

Hi

Is it possible to use a slice of structs, like here?

type Configs struct {
    Port    string    `param:"port"`
    Groups []Group    `param:"groups"`
}

type Group struct {
    Id     string    `param:"id"`
    /* other fields */
}

How?

@logrusorgru
Copy link
Author

The map instead of slice doesn't work

Source

package main

import (
    "fmt"
    "github.com/goji/param"
    "github.com/kr/pretty"
    "net/url"
    "time"
)

type Configs struct {
    AppId  string
    Key    string
    Port   string           `param:"port"`
    Groups map[string]Group `param:"groups"`
}

type Group struct {
    Id   string `param:"id"`
    Rm   string `param:"rm"`
    Bans Ban    `param:"bans"`
}

type Ban struct {
    Enable   bool          `param:"enable"`
    Reason   string        `param:"reason"`
    Comment  string        `param:"comment"`
    Duration time.Duration `param:"duration"`
    Visible  bool          `param:"visible"`
}

func main() {
    scfg := Configs{}
    err := param.Parse(url.Values{
        "port":                      {"8998"},
        "groups[0][id]":             {"123"},
        "groups[0][rm]":             {"all"},
        "groups[0][bans][enable]":   {"true"},
        "groups[0][bans][reason]":   {"5"},
        "groups[0][bans][comment]":  {"flooderast"},
        "groups[0][bans][duration]": {"168"},
        "groups[0][bans][visible]":  {"false"},
        "groups[1][id]":             {"123"},
        "groups[1][rm]":             {"all"},
        "groups[1][bans][enable]":   {"true"},
        "groups[1][bans][reason]":   {"5"},
        "groups[1][bans][comment]":  {"no flood"},
        "groups[1][bans][duration]": {"168"},
        "groups[1][bans][visible]":  {"false"},
    }, &scfg)
    if err != nil {
        fmt.Println(err)
    } else {
        fmt.Printf("%# v", pretty.Formatter(scfg))
    }
}

Result/output

main.Configs{
    AppId:  "",
    Key:    "",
    Port:   "8998",
    Groups: {
        "0": {
            Id:   "",
            Rm:   "",
            Bans: main.Ban{Enable:false, Reason:"", Comment:"no flood", Duration:0, Visible:false},
        },
        "1": {},
    },
}

@zenazn
Copy link
Member

zenazn commented Oct 15, 2014

So slices can only contain non-nested types due to a limitation in the implementation (I don't think it would be too hard to add if you felt inclined).

The other thing looks like a bug. This week is super busy for me but I'll investigate / fix it as soon as I get a chance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants