GithubHelp home page GithubHelp logo

go-enumer's People

Contributors

alvaroloes avatar amanbolat avatar boromisp avatar carlsverre avatar dmarkham avatar dterei avatar gjrtimmer avatar godsboss avatar ltpquang avatar marcobeierer avatar mgaffney avatar mrgossett avatar mvrahden avatar mvrhov avatar pascaldekloe avatar pdf avatar prashantv avatar serjlee avatar techmexdev avatar vladimiroff avatar wlbr avatar wttw avatar xescugc avatar xopherus avatar

Stargazers

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

Watchers

 avatar  avatar  avatar

go-enumer's Issues

RFC: Linter integration

A Linter integration could help avoid some of the most common issues with enums, that one would only run into at code-generation time.

Issues to cover:

  • Numeric range of enums (e.g. start values, order, steady increments, ...)
  • Prefixing
  • Magic comment evaluation
  • Sources out-of-sync issue (e.g. constants where changed but no new code was generated)
  • CSV sources vs. constants out-of sync issue (see #2 )
  • other CSV sources issues?

RFC: Add Assertion for enum constants when using CSV-sources

When using CSV sources, one might want to define individual constants, referencing some of the enums defined within the CSV source.
But if the CSV source changes (e.g. a new row is filled somewhere in between) the numeric order of enums might change, affecting the meaning of previously defined enum constants.
We already have out-of-range checks in place, but we still need to integrate out-of-sync checks.

From the colors example:

//go:enum -from=colors.csv
type Color uint

const (
	ColorMagenta Color = 7
)

Now if a new color would be added to (or removed from) the CSV at any of the lines 2-7, the numeric value of the magenta enum would shift and not match the one of our defined constant ColorMagenta, effectively leaving us with a wrong constant value.

Therefore it is crucial to integrate assertions into the code generation process, to expose these kinds of out-of-sync bugs and ensure that our enum constants are always in-sync with our CSV sources.

Please feel free to comment and bring in other ideas.

Approaches

Here I want to propose an approach of how to tackle the assertion of those constant values.

Assert during code generation time with special command in magic comment

The developer could add assert command to the //go:enum magic comment of each enum (or one of the enums) and define the actual values of those constants, he expects.

//go:enum -from=colors.csv
type Color uint

const (
	// ColorCyan represents Cyan.
	//go:enum assert={"6":"Cyan","7":"Magenta"}
	ColorCyan    Color = 6
	ColorMagenta Color = 7

	//go:enum assert={"0":"Black"}
	ColorBlack Color = 0
	//go:enum assert={"1":"White"}
	ColorWhite Color = 1
)

Optional: Assert during test execution

From those magic comment annotations, go-enumer could additionally generate a test file types_enumer_test.go which could include similar assertions for execution during test time, to assert that values still haven't changed.

Hint: The benefit of this "assert during test time approach" is limited, as (if done right) changes should only apply at code generation time. Therefore checking at that time should be sufficient enough and render the additional test file obsolete.
But for completeness, I wanted to bring up this approach as well.

Provide a Linter integration

Linters like golangci-lint could help unveiling these kind of bugs before any code generation is performed and help to effectively and non-invasively mitigate those issues.

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.