mdox icon indicating copy to clipboard operation
mdox copied to clipboard

Generate YAML from Go struct

Open saswatamcode opened this issue 4 years ago • 6 comments
trafficstars

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.

saswatamcode avatar Jul 12 '21 11:07 saswatamcode

Any update on this? @bwplotka

Dentrax avatar May 10 '22 06:05 Dentrax

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

bwplotka avatar May 10 '22 20:05 bwplotka

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.

Dentrax avatar May 11 '22 12:05 Dentrax

Kind ping, still looking for this feature! @saswatamcode @bwplotka

Dentrax avatar Jan 26 '23 06:01 Dentrax

cc @saswatamcode

bwplotka avatar Jun 07 '23 08:06 bwplotka

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

saswatamcode avatar Jun 13 '23 16:06 saswatamcode