Conversation
34b021c to
48335e8
Compare
| // TODO(saswatamcode): Currently takes file names, need to make it module based(something such as https://golang.org/pkg/cmd/go/internal/list/). | ||
|
|
||
| // GenGoCode generates Go code for yaml gen from structs in src file. | ||
| func GenGoCode(src []byte) (string, error) { |
There was a problem hiding this comment.
Good for start, but I wonder if we can scope to just one desired structure, to limit imports. Maybe it's later optimization - so fine for now
| init = append(init, jen.Id("configs").Op(":=").Map(jen.String()).Interface().Values()) | ||
|
|
||
| // Loop through declarations in file. | ||
| for _, decl := range f.Decls { |
There was a problem hiding this comment.
I think I would expect some recursion here (:
pkg/yamlgen/yamlgen.go
Outdated
| generatedCode.Func().Id("main").Params().Block( | ||
| init..., | ||
| ) |
There was a problem hiding this comment.
| generatedCode.Func().Id("main").Params().Block( | |
| init..., | |
| ) | |
| generatedCode.Func().Id("main").Params().Block(init...) |
pkg/yamlgen/yamlgen.go
Outdated
| cmd = exec.CommandContext(ctx, "go", "mod", "tidy") | ||
| cmd.Dir = tmpDir | ||
| if err := cmd.Run(); err != nil { | ||
| return nil, errors.Wrapf(err, "run %v", cmd) |
There was a problem hiding this comment.
| return nil, errors.Wrapf(err, "run %v", cmd) | |
| return nil, errors.Wrapf(err, "mod %v", cmd) |
pkg/yamlgen/yamlgen.go
Outdated
| cmd := exec.CommandContext(ctx, "go", "mod", "init", "structgen") | ||
| cmd.Dir = tmpDir | ||
| if err := cmd.Run(); err != nil { | ||
| return nil, errors.Wrapf(err, "run %v", cmd) |
There was a problem hiding this comment.
| return nil, errors.Wrapf(err, "run %v", cmd) | |
| return nil, errors.Wrapf(err, "mod init %v", cmd) |
pkg/yamlgen/yamlgen.go
Outdated
| return checkForOmitEmptyTagOptionRec(reflect.ValueOf(obj)) | ||
| } | ||
|
|
||
| func checkForOmitEmptyTagOptionRec(v reflect.Value) error { |
There was a problem hiding this comment.
Do we need to check for emit empty if we fill all the structs? I think in this case we need to also set non default values for other things that slices... so maybe for next iteration
48335e8 to
cd9a223
Compare
bwplotka
left a comment
There was a problem hiding this comment.
LGTM, let's add recursion and understand what map will work, what not. 👍🏽
| // TODO(saswatamcode): Currently takes file names, need to make it module based(something such as https://golang.org/pkg/cmd/go/internal/list/). | ||
|
|
||
| // GenGoCode generates Go code for yaml gen from structs in src file. | ||
| func GenGoCode(src []byte) (string, error) { |
| }) | ||
|
|
||
| t.Run("struct with unexported field", func(t *testing.T) { | ||
| source := []byte("package main\n\nimport \"regexp\"\n\ntype ValidatorConfig struct {\n\tType string `yaml:\"type\"`\n\tRegex string `yaml:\"regex\"`\n\tToken string `yaml:\"token\"`\n\n\tr *regexp.Regexp\n}\n") |
There was a problem hiding this comment.
I would use ` and add + backtick + , something like that. Still easier than long string manually crafted.
Another idea - put all in files - that would do as well.
|
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 thanos-io/thanos#4751 (comment) 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 |
Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com>
Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com>
Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com>
Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com>
Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com>
b5520ae to
021aec7
Compare
|
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! 🙂 |

This PR allows mdox to generate YAML from Go structs, using the following semantics(for now).
Thus, structs like below,
Result in markdown like below,
Dependencies added: jennifer, structtag
Few TODOs:
Resolves #23.