Skip to content

Commit e76293d

Browse files
committed
fix: add support for negative enum values
Copilot review identified that negative values like -1 were being incorrectly parsed as 0. Added UnaryExpr handling in processExplicitValue to properly support negative number literals. Also fixed test expectation for SQL NULL handling with Priority enum where PriorityLow (0) is the zero value, not PriorityNone (-1).
1 parent 6c08a6e commit e76293d

File tree

4 files changed

+16
-5
lines changed

4 files changed

+16
-5
lines changed

enum

5.2 MB
Binary file not shown.

internal/generator/generator.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,18 @@ func (g *Generator) processExplicitValue(expr ast.Expr, state *constParseState)
249249
state.iotaOp = nil
250250
return val
251251
}
252+
case *ast.UnaryExpr:
253+
// handle negative numbers like -1
254+
if e.Op == token.SUB {
255+
if lit, ok := e.X.(*ast.BasicLit); ok {
256+
if val, err := ConvertLiteralToInt(lit); err == nil {
257+
state.lastExprType = exprTypePlain
258+
state.lastValue = -val
259+
state.iotaOp = nil
260+
return -val
261+
}
262+
}
263+
}
252264
}
253265
return 0
254266
}

internal/generator/testdata/integration/enum_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ func TestGeneratedEnumWithSQL(t *testing.T) {
9797
row = db.QueryRow("SELECT status, priority FROM records WHERE id = ?", 2)
9898
err = row.Scan(&status, &priority)
9999
require.NoError(t, err)
100-
assert.Equal(t, StatusUnknown, status) // zero value
101-
assert.Equal(t, PriorityNone, priority) // zero value for priority (-1)
100+
assert.Equal(t, StatusUnknown, status) // zero value
101+
assert.Equal(t, PriorityLow, priority) // zero value for priority (0)
102102
}
103103

104104
func TestGeneratedEnumWithYAML(t *testing.T) {

internal/generator/testdata/integration/priority_enum.go

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

0 commit comments

Comments
 (0)