Giter VIP home page Giter VIP logo

enumer's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

enumer's Issues

Add build constraints to the exported file

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

Fork force use for go generate

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?

Feature Request: Different transformation methods for different marshaling types

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.

Make PillString case insensitive

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.

Feature req: support string enums

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"
)

Transformer and other operations when 'linecomment'

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 ๐Ÿ˜„

generate ToString() method

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.

BSON support

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.

Optimize IsAXXX method

we can optimize IsAXXX method to use switch statement instead of for loop.
with this, we dont need stringBelongsMethodSet

feature request: integration with validator/gin bindings

Firstly, I'd like to thank you for great work, I found enumer really helpful!

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.

generate failed on go 1.18 beta2

hi, just got an issue with go.18 beta2, reproduce steps:

  1. build enumer from source with go 1.18beta2
  2. use enumer from step1 to generate code
  3. got error: enumer: internal error: package "bytes" without types was imported from ...."

enumer unit test also failed on go1.18

The err of func %[1]sString(s string) print a lower case of string

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,

Enable interoperability with ent

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.