Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ go.work.sum
# Editor/IDE
# .idea/
# .vscode/

# Do not track generated example code in order to ensure tests pass.
example/*_gen.go
11 changes: 10 additions & 1 deletion cmd/enumify/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
package main

func main() {}
import (
"fmt"
"os"
)

func main() {
fname := os.Getenv("GOFILE")
pkg := os.Getenv("GOPACKAGE")
fmt.Println(fname, pkg)
}
38 changes: 38 additions & 0 deletions example/days.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package example

// Day is an enum type that should be implemented by the enumify generator.
// It uses a 1D array of strings for the names, which should be discovered by the
// enumify generator due to the go:generate directive above the Day type declaration.
//
//go:generate go run ../cmd/enumify
type Day uint8

// Constants for the Day enum values.
// These values should be discovered by the enumify generator since they use the same
// type as the Day enum, which is the type being generated.
const (
Unknown Day = iota
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
)

// 1D array of strings for the names of the Day enum values.
// This should be discovered by the enumify generator due to the go:generate directive
// and because it matches the dayNames pattern to connect it with the Day enum.
//
//lint:ignore U1000 this is used by the enumify generator
var dayNames = []string{
"unknown",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday",
}
56 changes: 56 additions & 0 deletions example/example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package example

import (
"math/rand"
"time"
)

// This is an unrelated type that should be ignored by the enumify generator.
type Example struct {
Name string
Day Day
Status Status
Date time.Time
Tags []string
}

// This is an unrelated function that should be ignored by the enumify generator.
func New() (*Example, error) {
adjectives := exampleNames[0]
nouns := exampleNames[1]
name := adjectives[rand.Intn(len(adjectives))] + " " + nouns[rand.Intn(len(nouns))]

day := Day(rand.Intn(int(Sunday) + 1))
status := Status(rand.Intn(int(StatusCancelled) + 1))

now := time.Now()
date := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)

n := rand.Intn(6) // 0–5 tags
tags := make([]string, n)
for i := range tags {
tags[i] = exampleTags[rand.Intn(len(exampleTags))]
}

return &Example{
Name: name,
Day: day,
Status: status,
Date: date,
Tags: tags,
}, nil
}

// This is an unrelated variable that should be ignored by the enumify generator.
var exampleTags = []string{
"low", "medium", "high",
"red", "green", "blue",
"primary", "secondary", "success", "danger", "warning", "info",
"foo", "bar", "baz",
}

// This is an unrelated 2D array that should be ignored by the enumify generator.
var exampleNames = [][]string{
{"curious", "ancient", "vibrant", "subtle", "brittle", "serene", "chaotic", "luminous", "hollow", "nimble", "terse", "ornate"},
{"mountain", "river", "castle", "lighthouse", "violin", "compass", "telescope", "orchard", "glacier", "harbor", "cathedral", "parchment"},
}
17 changes: 17 additions & 0 deletions example/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package example_test

import (
"testing"

"github.com/stretchr/testify/require"
"go.rtnl.ai/enumify/example"
)

// This is an unrelated test that should be ignored by the enumify generator.
func TestExample(t *testing.T) {
example, err := example.New()
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test variable shadows imported package name

Low Severity

The local example variable shadows the imported example package, making the package inaccessible for further calls within TestExample. This is a confusing anti-pattern, especially in example code, and makes the test fragile if extended.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 45ed293. Configure here.

require.NoError(t, err)
require.NotNil(t, example)
require.NotZero(t, example.Name)
require.False(t, example.Date.IsZero())
}
30 changes: 30 additions & 0 deletions example/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package example

// Status is an enum type that should be implemented by the enumify generator.
// It uses a 2D array of strings for the names, which should be discovered by the
// enumify generator due to the go:generate directive above the Status type declaration.
//
//go:generate go run ../cmd/enumify -names statusTable
type Status uint8

// Constants for the Status enum values.
// These values should be discovered by the enumify generator since they use the same
// type as the Status enum, which is the type being generated.
const (
StatusUnknown Status = iota
StatusPending
StatusRunning
StatusFailed
StatusSuccess
StatusCancelled
)

// 2D array of strings for the names of the Status enum values.
// This should be discovered by the enumify generator due to the go:generate directive
// that specifies this variable as the names for the Status enum.
//
//lint:ignore U1000 this is used by the enumify generator
var statusTable = [][]string{
{"unknown", "pending", "running", "failed", "success", "cancelled"},
{"text-secondary", "text-info", "text-primary", "text-danger", "text-success", "text-warning"},
}
Loading