alecthomas / kong

Kong is a command-line parser for Go
MIT License
2.17k stars 141 forks source link

Improvement: Documentation for slices of custom types #145

Closed mwmahlberg closed 3 years ago

mwmahlberg commented 3 years ago

It is unclear to me how something like this should be done properly:

type WebhookConfig struct {
  Name string
  URL  string
}

var config {
  Webhooks []WebhookConfig
}

especially in conjunction with https://github.com/alecthomas/kong-yaml

Implementing a custom MapperDecoder or json.Unmarshaller seems a bit cumbersome, taking a rather common configuration like:

webhooks:
    - name: "foo"
      url: "bar"
    - name: "bar"
      url: "baz"

into account. Am I right to assume that a custom Decoder/Unmarshaller is required in this case?

alecthomas commented 3 years ago

You are correct. It's not clear to me how this could be any more convenient though, given that Webhooks must also be exposed via flags?

That said however, in this particular instance you could potentially just use a map.

alecthomas commented 3 years ago

On further reflection this is not possible as Kong always assumes that structs without cmd are invalid unless they implement the decoder interface.

costela commented 1 year ago

For the record: this should be fixed with https://github.com/alecthomas/kong-yaml/pull/15#issue-1601121303 :crossed_fingers: