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 ability to encode comments with desc tag #193

Closed
wants to merge 3 commits into from

Conversation

jbsmith7741
Copy link

@jbsmith7741 jbsmith7741 commented Sep 28, 2017

Resolve issue #75 by adding a new tag 'desc' to describe variables.

example struct

type Person struct {
	Name string `desc:"My Name"`
	Age  int    `desc:"My Age"`
	Dog  Dog    `desc:"My dog"`
}

type Dog struct {
	Name  string `desc:"My Dog"`
	Breed string `desc:"Dog's breed"`
	Pet  Bug    `desc:"Dog's pet"`
}

type Bug struct {
	Weight float64 `desc:"in ounces"`
}

encodes to

# My Name
Name = ""
# My Age
Age = 0
# My dog
[Dog]
  # My Dog
  Name = ""
  # Dog's breed
  Breed = ""
  # Dog's pet
  [Dog.Pet]
    # in ounces
    Weight = 0.0

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)

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 #

@silasdavis
Copy link

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.

@yonderblue
Copy link

Would also be useful to decode the comments into something accessible, perhaps that means not using field tags?

@bobheadxi
Copy link

bobheadxi commented Jul 2, 2018

I think this looks great, and would be pretty useful for me - any plans to get this merged soon? Paging @BurntSushi

@jbsmith7741
Copy link
Author

FYI: I've added this feature in another fork if anyone is interested https://github.com/hydronica/toml

@arp242
Copy link
Collaborator

arp242 commented Jun 8, 2021

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:

type Commenter interface {
    Comment() string
}

And then you can use something like:

type X struct {
    WithComment strCmt
}

type strCmt string

func (strCmt) Comment() string { return "Yo, a comment!" }

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.

@arp242
Copy link
Collaborator

arp242 commented Jun 21, 2021

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.

@arp242 arp242 closed this Jun 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants