mvrahden / go-enumer Goto Github PK
View Code? Open in Web Editor NEW✨ Go-Enums with Magic ✨
License: BSD 2-Clause "Simplified" License
✨ Go-Enums with Magic ✨
License: BSD 2-Clause "Simplified" License
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:
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.
Here I want to propose an approach of how to tackle the assertion of those constant values.
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
)
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.
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.
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.