Fix some bugs

This commit is contained in:
Eden Kirin
2025-10-31 18:33:22 +01:00
parent f9f67b6c93
commit 499f59ff12
2 changed files with 29 additions and 4 deletions

View File

@ -3,10 +3,20 @@ package generator
import ( import (
"fmt" "fmt"
"strings" "strings"
"unicode"
"github.com/entity-maker/entity-maker/internal/naming" "github.com/entity-maker/entity-maker/internal/naming"
) )
// sanitizePythonIdentifier ensures the identifier is valid Python syntax
func sanitizePythonIdentifier(identifier string) string {
// If identifier starts with a digit, prefix with underscore
if len(identifier) > 0 && unicode.IsDigit(rune(identifier[0])) {
return "_" + identifier
}
return identifier
}
// GenerateEnum generates the enum types file // GenerateEnum generates the enum types file
func GenerateEnum(ctx *Context) (string, error) { func GenerateEnum(ctx *Context) (string, error) {
var b strings.Builder var b strings.Builder
@ -24,12 +34,24 @@ func GenerateEnum(ctx *Context) (string, error) {
if len(enumType.Values) == 0 { if len(enumType.Values) == 0 {
b.WriteString(" pass\n") b.WriteString(" pass\n")
} else { } else {
// Track seen identifiers to avoid duplicates
seenIdentifiers := make(map[string]bool)
for _, value := range enumType.Values { for _, value := range enumType.Values {
// Convert value to valid Python identifier // Convert value to valid Python identifier
// Usually enum values are already uppercase like "OPEN", "IN_PROGRESS" // Usually enum values are already uppercase like "OPEN", "IN_PROGRESS"
identifier := strings.ToUpper(strings.ReplaceAll(value, " ", "_")) identifier := strings.ToUpper(strings.ReplaceAll(value, " ", "_"))
identifier = strings.ReplaceAll(identifier, "-", "_") identifier = strings.ReplaceAll(identifier, "-", "_")
// Ensure identifier doesn't start with a digit
identifier = sanitizePythonIdentifier(identifier)
// Skip if we've already seen this identifier
if seenIdentifiers[identifier] {
continue
}
seenIdentifiers[identifier] = true
b.WriteString(fmt.Sprintf(" %s = \"%s\"\n", identifier, value)) b.WriteString(fmt.Sprintf(" %s = \"%s\"\n", identifier, value))
} }
} }

View File

@ -38,12 +38,15 @@ func GenerateFilter(ctx *Context) (string, error) {
// Boolean fields // Boolean fields
if col.DataType == "boolean" { if col.DataType == "boolean" {
defaultVal := "None"
if col.Name == "alive" { if col.Name == "alive" {
defaultVal = "True" // Special case: alive field defaults to True
b.WriteString(fmt.Sprintf(" %s: bool | None = filterfield(operator=EQ, default=True)\n",
col.Name))
} else {
// Other boolean fields don't need explicit default=None
b.WriteString(fmt.Sprintf(" %s: bool | None = filterfield(operator=EQ)\n",
col.Name))
} }
b.WriteString(fmt.Sprintf(" %s: bool | None = filterfield(operator=EQ, default=%s)\n",
col.Name, defaultVal))
} }
// ID fields (both EQ and IN) // ID fields (both EQ and IN)