Skip to content

Add support for json output format#49

Merged
g4s8 merged 3 commits intog4s8:masterfrom
rodionstolyarov:json-format
Jan 9, 2025
Merged

Add support for json output format#49
g4s8 merged 3 commits intog4s8:masterfrom
rodionstolyarov:json-format

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.

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