Skip to content

Commit 7299d4d

Browse files
committed
Fix linter issues and add dummy usage of enum constants to prevent linter warnings
1 parent 5f9ae33 commit 7299d4d

6 files changed

Lines changed: 96 additions & 5 deletions

File tree

_examples/status/job_status_enum.go

Lines changed: 22 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

_examples/status/job_status_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestJobStatus(t *testing.T) {
2727
d := Data{Status: JobStatusActive}
2828
b, err := json.Marshal(d)
2929
require.NoError(t, err)
30-
assert.Equal(t, `{"status":"active"}`, string(b))
30+
assert.JSONEq(t, `{"status":"active"}`, string(b))
3131

3232
var d2 Data
3333
err = json.Unmarshal([]byte(`{"status":"inactive"}`), &d2)

_examples/status/status_enum.go

Lines changed: 22 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

_examples/status/status_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package status provides examples of enum usage
12
package status
23

34
import (
@@ -27,7 +28,7 @@ func TestStatus(t *testing.T) {
2728
d := Data{Status: StatusActive}
2829
b, err := json.Marshal(d)
2930
require.NoError(t, err)
30-
assert.Equal(t, `{"status":"active"}`, string(b))
31+
assert.JSONEq(t, `{"status":"active"}`, string(b))
3132

3233
var d2 Data
3334
err = json.Unmarshal([]byte(`{"status":"inactive"}`), &d2)

internal/generator/enum.go.tmpl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,16 @@ func {{.Type | title}}Iter() func(yield func({{.Type | title}}) bool) {
142142
}
143143
}
144144
}
145-
}
145+
}
146+
147+
// These variables are used to prevent the compiler from reporting unused errors
148+
// for the original enum constants. They are intentionally placed in a var block
149+
// that is compiled away by the Go compiler.
150+
var _ = func() bool {
151+
var _ {{.Type}} = 0
152+
{{range .Values -}}
153+
// This avoids "defined but not used" linter error for {{.PrivateName}}
154+
var _ {{$.Type}} = {{.PrivateName}}
155+
{{end -}}
156+
return true
157+
}()

internal/generator/generator_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,42 @@ const (
478478
})
479479
}
480480

481+
func TestNoLinterWarningsForUnusedConstants(t *testing.T) {
482+
tmpDir := t.TempDir()
483+
err := os.WriteFile(filepath.Join(tmpDir, "linter_test.go"), []byte(`
484+
package test
485+
type linterTest uint8
486+
const (
487+
linterTestUnknown linterTest = iota
488+
linterTestValue1
489+
linterTestValue2
490+
)
491+
`), 0o644)
492+
require.NoError(t, err)
493+
494+
gen, err := New("linterTest", tmpDir)
495+
require.NoError(t, err)
496+
497+
err = gen.Parse(tmpDir)
498+
require.NoError(t, err)
499+
500+
err = gen.Generate()
501+
require.NoError(t, err)
502+
503+
// read the generated file to check for the linter warning prevention code
504+
content, err := os.ReadFile(filepath.Join(tmpDir, "linter_test_enum.go"))
505+
require.NoError(t, err)
506+
507+
// check that the unused constants prevention code exists
508+
assert.Contains(t, string(content), "// These variables are used to prevent the compiler from reporting unused errors")
509+
assert.Contains(t, string(content), "var _ = func() bool {")
510+
assert.Contains(t, string(content), "var _ linterTest = 0")
511+
assert.Contains(t, string(content), "var _ linterTest = linterTestUnknown")
512+
assert.Contains(t, string(content), "var _ linterTest = linterTestValue1")
513+
assert.Contains(t, string(content), "var _ linterTest = linterTestValue2")
514+
assert.Contains(t, string(content), "return true")
515+
}
516+
481517
func TestGeneratorEdgeCases(t *testing.T) {
482518
t.Run("invalid template", func(t *testing.T) {
483519
// create a generator with a broken template that will fail to execute

0 commit comments

Comments
 (0)