Skip to content
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

Add support for json output format #49

Merged
merged 3 commits into from
Jan 9, 2025
Merged

Conversation

rodionstolyarov
Copy link
Contributor

Sometimes it needs to export documentation for envs in JSON format for further automation.

This PR supports a new output format json.

@g4s8 g4s8 self-requested a review January 7, 2025 10:45
Copy link
Owner

@g4s8 g4s8 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for contribution! Please check one comment above. Also, please add an example to _examples/ with json output, and build it using _examples/build-examples.sh.

@@ -31,6 +32,10 @@ var tplFuncs = map[string]any{
}
return sum
},
"marshal": func(v any) string {
a, _ := json.Marshal(v)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like here a fatal error can occur. We need to panic here and recover on render caller.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can return an error along with string result instead of panic.

- add an example for json format
- update marshal function to return an error
@rodionstolyarov
Copy link
Contributor Author

Thanks for your feedback! I've added json format to simple example.

Copy link
Owner

@g4s8 g4s8 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, it looks good. Could you check please some comments with questions?

@@ -0,0 +1 @@
[{"env_name":"HOST","doc":"Hosts name of hosts to listen on.","env_default":"","env_separator":";","required":true,"expand":false,"non_empty":false,"from_file":false},{"env_name":"PORT","doc":"Port to listen on.","env_default":"","env_separator":"","required":true,"expand":false,"non_empty":true,"from_file":false},{"env_name":"DEBUG","doc":"Debug mode enabled.","env_default":"false","env_separator":"","required":false,"expand":false,"non_empty":false,"from_file":false},{"env_name":"PREFIX","doc":"Prefix for something.","env_default":"","env_separator":"","required":false,"expand":false,"non_empty":false,"from_file":false}]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it could be more readable with indentations (it could be set in json encoder), it seems here we don't need size optimization, since the size of the documentation can't be big enough.

And what do you think about omitting empty fields? E.g. envDefault and envSeparator?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. I've added omitempty tag for all bool fields as well.

@g4s8 g4s8 enabled auto-merge January 9, 2025 18:12
@g4s8 g4s8 merged commit 185c18c into g4s8:master Jan 9, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants