Fix some bugs
This commit is contained in:
@ -3,10 +3,20 @@ package generator
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"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
|
||||
func GenerateEnum(ctx *Context) (string, error) {
|
||||
var b strings.Builder
|
||||
@ -24,12 +34,24 @@ func GenerateEnum(ctx *Context) (string, error) {
|
||||
if len(enumType.Values) == 0 {
|
||||
b.WriteString(" pass\n")
|
||||
} else {
|
||||
// Track seen identifiers to avoid duplicates
|
||||
seenIdentifiers := make(map[string]bool)
|
||||
|
||||
for _, value := range enumType.Values {
|
||||
// Convert value to valid Python identifier
|
||||
// Usually enum values are already uppercase like "OPEN", "IN_PROGRESS"
|
||||
identifier := strings.ToUpper(strings.ReplaceAll(value, " ", "_"))
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
||||
@ -38,12 +38,15 @@ func GenerateFilter(ctx *Context) (string, error) {
|
||||
|
||||
// Boolean fields
|
||||
if col.DataType == "boolean" {
|
||||
defaultVal := "None"
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user