Skip to content

Commit 1618dd6

Browse files
authored
Add a code generator for enum boilerplate (#401)
Go does not have enums, but they are quite useful, so we emulate them with int-kinded types and use switches and whatnot to convert them to and from strings. There are enough of these in the new compiler stack at this point that maintaining all of these stringification functions is getting tedious and error-prone, so this PR contains a very simple, 200-line `//go:generate` helper for generating those functions. I surveyed other existing packages of this kind, but unfortunately they all want to put varying strong constraints on how we organize and name our "enums", so I felt that we should just write the simplest possible thing to fit our use-case.
1 parent b04f563 commit 1618dd6

File tree

24 files changed

+1545
-675
lines changed

24 files changed

+1545
-675
lines changed

experimental/ast/decl.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,6 @@ import (
2222
"github.com/bufbuild/protocompile/internal/arena"
2323
)
2424

25-
const (
26-
DeclKindInvalid DeclKind = iota
27-
DeclKindEmpty
28-
DeclKindSyntax
29-
DeclKindPackage
30-
DeclKindImport
31-
DeclKindDef
32-
DeclKindBody
33-
DeclKindRange
34-
)
35-
36-
// DeclKind is a kind of declaration. There is one value of DeclKind for each
37-
// Decl* type in this package.
38-
type DeclKind int8
39-
4025
// DeclAny is any Decl* type in this package.
4126
//
4227
// Values of this type can be obtained by calling an AsAny method on a Decl*

experimental/ast/decl_def.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,6 @@ import (
2020
"github.com/bufbuild/protocompile/internal/arena"
2121
)
2222

23-
const (
24-
DefKindInvalid DefKind = iota
25-
DefKindMessage
26-
DefKindEnum
27-
DefKindService
28-
DefKindExtend
29-
DefKindField
30-
DefKindOneof
31-
DefKindGroup
32-
DefKindEnumValue
33-
DefKindMethod
34-
DefKindOption
35-
)
36-
37-
// DefKind is the kind of definition a [DeclDef] contains.
38-
//
39-
// See [DeclDef.Classify].
40-
type DefKind int8
41-
4223
// DeclDef is a general Protobuf definition.
4324
//
4425
// This [Decl] represents the union of several similar AST nodes, to aid in permissive

experimental/ast/doc.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,5 @@
9292
// library. Outside of this file, documentation is written assuming this has
9393
// already happened.
9494
package ast
95+
96+
//go:generate go run github.com/bufbuild/protocompile/internal/enum enums.yaml

experimental/ast/enums.go

Lines changed: 343 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)