mdox
mdox copied to clipboard
Generate YAML from Go struct
This PR allows mdox to generate YAML from Go structs, using the following semantics(for now). Thus, structs like below,
type Config struct {
Version int `yaml:"version"`
Validator []ValidatorConfig `yaml:"validators"`
Ignore []IgnoreConfig `yaml:"ignore"`
}
type ValidatorConfig struct {
Type string `yaml:"type"`
Regex string `yaml:"regex"`
Token string `yaml:"token"`
r *regexp.Regexp
}
type IgnoreConfig struct {
Url string `yaml:"url"`
ID int `yaml:"id"`
Token string `yaml:"token"`
}
Result in markdown like below,
```yaml mdox-gen-go-struct="main.go:Config"
version: 0
validators:
- type: ""
regex: ""
token: ""
ignore:
- url: ""
id: 0
token: ""
```yaml mdox-gen-go-struct="main.go:ValidatorConfig"
type: ""
regex: ""
token: ""
Dependencies added: jennifer, structtag
Few TODOs:
// TODO(saswatamcode): Add tests.
// TODO(saswatamcode): Check jennifer code for some safety.
// TODO(saswatamcode): Add mechanism for caching output from generated code.
// TODO(saswatamcode): Better errors.
Resolves #23.
Any update on this? @bwplotka
I think @saswatamcode has some comments to be addressed, but quite close.
What do you need this for @Dentrax? Curious about your use case and if this PR fixes it (:
I've created a related issue https://github.com/thanos-io/thanos/issues/4751#issuecomment-967799915 and closed due to inactivity. We want to add support for both comments and default values while generating YAML.
Kind ping, still looking for this feature! @saswatamcode @bwplotka
cc @saswatamcode
I'm trying to look into this, this week and bring it to a nice usable state, rebasing and correcting some of the old code. Plan to clean it up in the next few commits! 🙂