fix(type_mapper): adjust array tag handling in BuildBunTag

This commit is contained in:
2026-05-03 17:18:58 +02:00
parent 096815fe49
commit 6e470a9239
2 changed files with 27 additions and 3 deletions

View File

@@ -323,7 +323,7 @@ func (tm *TypeMapper) BuildBunTag(column *models.Column, table *models.Table) st
} }
} }
parts = append(parts, fmt.Sprintf("type:%s", typeStr)) parts = append(parts, fmt.Sprintf("type:%s", typeStr))
if isArray { if isArray && tm.typeStyle == writers.NullableTypeStdlib {
parts = append(parts, "array") parts = append(parts, "array")
} }
} }

View File

@@ -696,7 +696,7 @@ func TestTypeMapper_BuildBunTag(t *testing.T) {
Type: "text[]", Type: "text[]",
NotNull: false, NotNull: false,
}, },
want: []string{"tags,", "type:text[],", "array,"}, want: []string{"tags,", "type:text[],"},
}, },
{ {
name: "integer array type", name: "integer array type",
@@ -705,7 +705,7 @@ func TestTypeMapper_BuildBunTag(t *testing.T) {
Type: "integer[]", Type: "integer[]",
NotNull: true, NotNull: true,
}, },
want: []string{"scores,", "type:integer[],", "array,"}, want: []string{"scores,", "type:integer[],"},
}, },
} }
@@ -717,6 +717,30 @@ func TestTypeMapper_BuildBunTag(t *testing.T) {
t.Errorf("BuildBunTag() = %q, missing %q", result, part) t.Errorf("BuildBunTag() = %q, missing %q", result, part)
} }
} }
// resolvespec mode must NOT add "array" — SqlXxxArray uses sql.Scanner
if strings.Contains(result, ",array,") || strings.HasSuffix(result, ",array,") {
t.Errorf("BuildBunTag() = %q, must not contain 'array' in resolvespec mode", result)
}
})
}
}
func TestTypeMapper_BuildBunTag_StdlibArrayHasArrayTag(t *testing.T) {
mapper := NewTypeMapper(writers.NullableTypeStdlib)
cases := []struct {
name string
column *models.Column
}{
{name: "text array", column: &models.Column{Name: "tags", Type: "text[]"}},
{name: "integer array", column: &models.Column{Name: "scores", Type: "integer[]", NotNull: true}},
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
result := mapper.BuildBunTag(tt.column, nil)
if !strings.Contains(result, "array") {
t.Errorf("BuildBunTag() = %q, expected 'array' in stdlib mode", result)
}
}) })
} }
} }