dmarkham / enumer Goto Github PK
View Code? Open in Web Editor NEWA Go tool to auto generate methods for your enums
License: Other
A Go tool to auto generate methods for your enums
License: Other
Title says it all :)
Sometime there is already a String()
method.
I propose enhancing enumer to allow specifying different transformation methods for different marshaling types.
Our team currently implements custom GraphQL and JSON marshaling methods based on UPPER_SNAKE_CASE and snake_case representations, respectively. It'd be extremely helpful if enumer can generate both for us while keeping the current representations.
I suppose the feature will accept a transformation method via the value of each marshaling flag as shown below.
-gqlgen=snake-upper -json=snake
If this sounds like a good feature to add, I am ready and willing to contribute to its implementation.
The readme does a good job of explaining the use of enumer, comprehensive enough that you might not need more, but the actual use of the package is sort of below-the-fold or below what would be my first scroll. I'd like to add a very succinct example and the simplest instructions to install at the very start without removing any of the existing explanation(s). This would hopefully lower the bar for new and returning devs.
First of all i'm a happy user of the enumer :) But i've an situation in which i need a bit of help.
When my enum is in code that is only build when certain constraints are met. (See: here) i'd like to have the constraints also in the generated code. If the build constraints not get exported the compile would generated errors as the generate file is build without the source containing the enum base definition.
I wrote a small PR in which i added a command line argument to add the build constraints. This works fine.
I was thinking whether it would be possible to deduct the build tags from the original source file and copy them into the generated one. But could not think of an easy way to that.
Note the comment option does not work as it adds a space between //
and the first char of the comment
It would be useful to perform string -> enum conversion in a case-insensitive fashion. While I could convert my string to lowercase and have all of my values lowercase as well, some of them are acronyms which looks ugly if not written with capital letters.
We need this in order to be a better drop in replacement. We are currently just missing this feature!
I sometimes want to convert an int item specified by enum to a string, e.g. to specify RESTFullAPI request parameters, etc.
It would be nice to have an option to generate a ToString() method.
Now we have two libraries with similar features but not the same. And when I add annotations like
//go:generate enumer -type=From -json -transform=snake
Binary can be from any of them and result can be different. Is there any way to make sure that code will be generated only by your binary? For example, add different binary name for ensure or some other tricks that provide incompatibility with original version?
in my case, i'm converting iota to 1 character string and they includes both lower and upper version of some alphabets. i've read #21 , so maybe add a flag to enable case sensitive in <Type>String(s string)
.
When using the -linecomment=true
I think that operations that change the text should not apply, for example -transform=
-trimprefix=
-addprefix=
as you are saying how do you want it to look like:
-linecomment
use line comment text as printed text when present
We had an error due to that (but was our fault) as we had a type like:
package enumer
type DeltaType int
//go:generate enumer -type=DeltaType -transform=snake -output=delta_type_string.go -linecomment=true
const (
Common DeltaType = iota + 1 // =
LeftOnly // -
RightOnly // +
Unknown // ?
)
It was a copy paste error but it ended up having a .String()
that always returned ""
as the linecomment
was not "valid" to snake
(which could potentially rise errors if a type is gonna be transformed to just ""
? ๐ค ).
If we want to change this behavior I'm open to do a PR which will also solve #27 ๐
This can be prevented by adding a tools.go
file:
//go:build tools
package tools
import (
// Protect this entry in go.mod from being removed by go mod tidy.
_ "github.com/dmarkham/enumer"
)
Should probably go into README.md
.
I am not sure about proper solution, however I think that may be doable by another flag/parameter.
In perfect scenario it would either implement validator interface, or return validator.ValidationErrors.
If not, maybe there is possibility to generate helper validator functions to bind for API models.
Hi @dmarkham
While digging in the previous PR of the project I noticed that you had made some work on BSON support back in 2019 but did not finish it.
If I was to open a PR to make a -bson
flag again would you be willing to merge it ?
I understood that you did not like the fact that the generated code would be tied to the external mongodb package but I don't think there is any alternative way to implement a BSON marshaller for now.
hi, just got an issue with go.18 beta2, reproduce steps:
enumer: internal error: package "bytes" without types was imported from ...."
enumer unit test also failed on go1.18
Hi, to improve interoperability with ent and support its enum type-safety, I'd propose to add the Values() []string
method (from the EnumValues interface) to the generated enum types, next to String() string
.
This feature should likely be opt-in, so it would be beneficial to add a flag for it. I'd propose -values
(or -valuesFunc
) flag.
I've seen, there is already a generated ***Strings() []string
-func, which would just need to be wrapped by the Values() []string
method in question.
@dmarkham Would you be open for a PR for this?
An Example:
func (Pill) Values() []string {
return PillStrings()
}
P.S.: I understand there's a potential for confusion, due to the pre-existing PillValues() []Pill
function. But to have typesafety with enums in ent, this proposal is the only way to go from what I see.
we can optimize IsAXXX method to use switch statement instead of for loop.
with this, we dont need stringBelongsMethodSet
-trimprefix
accepts a comma-separated list of prefixes, but the README/help doesn't mention this.
Hi! I have a use-case where I want to use enumer to map postgres enums with the -sql
flag. There may be conflicts in my enums, hence I cannot declare multiple enums in the same package.
Example:
//go:generate ...
type EnumA int
const (
Test EnumA = iota
Cool EnumA
)
//go:generate ...
type EnumB int
const (
VeryCool EnumB = iota
Cool EnumB // this will cause issues!
)
It would be neat if we could instead declare the values as strings, e.g.:
//go:generate ...
type EnumA string
const (
ATest EnumA = "TEST"
ACool EnumA = "COOL"
)
//go:generate ...
type EnumB string
const (
BVeryCool EnumB = "VERY_COOL"
BCool EnumB = "COOL"
)
In my project, I wrote a test to fully cover enumer-generated code.
I would be happy/willing to take that test and add a flag to enumer so that enumer will generate tests too.
Let me know.
See https://github.com/muir/xop-go/blob/main/internal/enumer_test.go
Hi team,
I was trying to do some unhappy path test case using
//go:generate enumer -type=BusinessType -json -transform upper
And when I test a "does not belong to BusinessType values" case:
I entered ABC, and expect to get "ABC does not belong to BusinessType values",
but what I get is "abc does not belong to BusinessType values"
I think it's because the generated function BusinessTypeString transformed it into lower case, it's not impacting the code logic, but my test case becomes a little weird:
Input: "ABC"
Output: "abc does not belong to BusinessType values"
The question is, is there any way to generate without the toLower() transform in function BusinessTypeString?
Many thanks!
Best regards,
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.