-
Notifications
You must be signed in to change notification settings - Fork 11
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
Generate YAML from Go struct #60
base: main
Are you sure you want to change the base?
Conversation
34b021c
to
48335e8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add todo for that?
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
generatedCode.Func().Id("main").Params().Block( | |
init..., | |
) | |
generatedCode.Func().Id("main").Params().Block(init...) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return nil, errors.Wrapf(err, "run %v", cmd) | |
return nil, errors.Wrapf(err, "mod %v", cmd) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return nil, errors.Wrapf(err, "run %v", cmd) | |
return nil, errors.Wrapf(err, "mod init %v", cmd) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
pkg/yamlgen/yamlgen.go
Outdated
return checkForOmitEmptyTagOptionRec(reflect.ValueOf(obj)) | ||
} | ||
|
||
func checkForOmitEmptyTagOptionRec(v reflect.Value) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add todo for that?
}) | ||
|
||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 <[email protected]>
Signed-off-by: Saswata Mukherjee <[email protected]>
Signed-off-by: Saswata Mukherjee <[email protected]>
Signed-off-by: Saswata Mukherjee <[email protected]>
Signed-off-by: Saswata Mukherjee <[email protected]>
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.