-
-
Notifications
You must be signed in to change notification settings - Fork 530
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 ability to encode comments with desc tag #193
Conversation
encode.go
Outdated
enc.encode(key.add(keyName), sf) | ||
} | ||
} | ||
writeFields(fieldsDirect) | ||
writeFields(fieldsSub) | ||
} | ||
|
||
func (enc *Encoder) addComment(key Key, tag reflect.StructTag) { | ||
if s := tag.Get("desc"); s != "" { | ||
enc.wf("%s#%s\n", enc.indentStr(key), s) |
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.
Maybe we can add a space after the #
Would be useful to have this merged. If you want to have a single struct to define your application's config then it is nice to be able to maintain a two-way mapping between your canonical 'init' config with helpful comments and from modified config back to your struct. Without this you end up with the overhead of a manually assembled example/default config with comments and run the risk of them getting out of sync. |
Would also be useful to decode the comments into something accessible, perhaps that means not using field tags? |
I think this looks great, and would be pretty useful for me - any plans to get this merged soon? Paging @BurntSushi |
FYI: I've added this feature in another fork if anyone is interested https://github.com/hydronica/toml |
I'm not a huge fan of using struct tags for this. Struct tags are limited in what you can put in there, it's a static string you can never modify (i.e. what if I want to put dynamic data in there?) and it just looks ugly for long strings IMO. Instead, we could do something like:
And then you can use something like:
Or ... something like that. I'm not sure. It's a bit more boilerplate, but it's a lot more flexible. How do other encoders solve this problem, if at all? I can't really find an example of another encoder which does something like this. |
I'll close this for now as I almost certainly don't want to use struct tags for this. I have some ideas for a better solution, as mentioned, but I need to prototype a few things to see what does and doesn't work. And before I take this on I want to fix some other things, like supporting all of TOML 1.0, fixing a few remaining bugs, and improving the errors a wee bit. I absolutely want to add this feature on the short term (i.e. weeks, not years), just need to think of the best way to do it. You can track #75 for updates. |
Resolve issue #75 by adding a new tag 'desc' to describe variables.
example struct
encodes to