Open gerricom opened 3 months ago
The solution I feel is best would be to store them in a slice of strings but that also generates errors.
package enums
type planet int // Satellites([]string)
//go:generate goenums -f planets.go
const (
mercury planet = iota // Mercury []{"None"}
venus // Venus []{"None"}
earth // Earth []{"Moon"}
mars // Mars []{"Phobos, Deimos"}
jupiter // Jupiter []{"Too many to list here..."}
saturn // Saturn []{"even more than Jupiter. D'oh!"}
uranus // Uranus []{"some around 28"}
neptune // Neptune []{"only sixteen"}
)
Looking into supporting this.
The solution I feel is best would be to store them in a slice
Yes, that could be a solution, but than one would need to always select the first item of that slice...
I had another idea this night: There is already a somewhat good working text format for various data types and that is CSV. RFC4180 describes clear rules of what spaces, quotes etc. are doing. And it is quite human-readable - at least better than JSON. Though you would need something like gocsv to map a csv back to a struct.
Thanks for your hard work! I really like your approach on creating good usable enums in go!
I have a very common usecase in one of my pet-projects: It is a web application and I'm in the need to hand over the enum to my frontend so that I can choose something in a select box. There are some descriptive texts in there. With the current implementation it is not really possible to hand over strings in the enum extension, especially if those strings are longer or contain one of the splitting characters that are currently used.
To stay in the solarsystem example, this is something that won't work well:
The generated struct now looks like
My very first rough idea is that an extension as data input could just accept json (compressed to a single line of course). This is not very human editable/readble of course, but it would be very dynamic and one could hand in the struct as an extension right into the initial comment (this is already working). Handing in json could look like this...?