From f6c3f2b46028b36c4da78eff78f778f492804de1 Mon Sep 17 00:00:00 2001 From: Hein Date: Sun, 4 Jan 2026 22:11:44 +0200 Subject: [PATCH] =?UTF-8?q?feat(bun):=20=F0=9F=8E=89=20Enhance=20nullabili?= =?UTF-8?q?ty=20handling=20in=20column=20parsing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Introduce explicit nullability markers in column tags. * Update logic to infer nullability based on Go types when no markers are present. * Ensure correct tags are generated for nullable and non-nullable fields. --- pkg/readers/bun/reader.go | 23 ++++++++++++++--------- pkg/writers/bun/type_mapper.go | 3 +++ 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/pkg/readers/bun/reader.go b/pkg/readers/bun/reader.go index 50753c0..b3281de 100644 --- a/pkg/readers/bun/reader.go +++ b/pkg/readers/bun/reader.go @@ -632,6 +632,9 @@ func (r *Reader) parseColumn(fieldName string, fieldType ast.Expr, tag string, s column.Name = parts[0] } + // Track if we found explicit nullability markers + hasExplicitNullableMarker := false + // Parse tag attributes for _, part := range parts[1:] { kv := strings.SplitN(part, ":", 2) @@ -649,6 +652,10 @@ func (r *Reader) parseColumn(fieldName string, fieldType ast.Expr, tag string, s column.IsPrimaryKey = true case "notnull": column.NotNull = true + hasExplicitNullableMarker = true + case "nullzero": + column.NotNull = false + hasExplicitNullableMarker = true case "autoincrement": column.AutoIncrement = true case "default": @@ -664,17 +671,15 @@ func (r *Reader) parseColumn(fieldName string, fieldType ast.Expr, tag string, s // Determine if nullable based on Go type and bun tags // In Bun: - // - nullzero tag means the field is nullable (can be NULL in DB) - // - absence of nullzero means the field is NOT NULL - // - primitive types (int64, bool, string) are NOT NULL by default - column.NotNull = true - // Primary keys are always NOT NULL - - if strings.Contains(bunTag, "nullzero") { - column.NotNull = false - } else { + // - explicit "notnull" tag means NOT NULL + // - explicit "nullzero" tag means nullable + // - absence of explicit markers: infer from Go type + if !hasExplicitNullableMarker { + // Infer from Go type if no explicit marker found column.NotNull = !r.isNullableGoType(fieldType) } + + // Primary keys are always NOT NULL if column.IsPrimaryKey { column.NotNull = true } diff --git a/pkg/writers/bun/type_mapper.go b/pkg/writers/bun/type_mapper.go index 58dc830..d9e1b59 100644 --- a/pkg/writers/bun/type_mapper.go +++ b/pkg/writers/bun/type_mapper.go @@ -192,8 +192,11 @@ func (tm *TypeMapper) BuildBunTag(column *models.Column, table *models.Table) st } // Nullable (Bun uses nullzero for nullable fields) + // and notnull tag for explicitly non-nullable fields if !column.NotNull && !column.IsPrimaryKey { parts = append(parts, "nullzero") + } else if column.NotNull && !column.IsPrimaryKey { + parts = append(parts, "notnull") } // Check for indexes (unique indexes should be added to tag)