Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Generate structs for composite types
  • Loading branch information
wickwirew committed Sep 11, 2025
commit 88809133d56928a4c4ca49183440f92107529702
25 changes: 25 additions & 0 deletions internal/cmd/shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,34 @@ func pluginCatalog(c *catalog.Catalog) *plugin.Catalog {
Vals: typ.Vals,
})
case *catalog.CompositeType:
var columns []*plugin.Column
for _, c := range typ.Columns {
l := -1
if c.Length != nil {
l = *c.Length
}
columns = append(columns, &plugin.Column{
Name: c.Name,
Type: &plugin.Identifier{
Catalog: c.Type.Catalog,
Schema: c.Type.Schema,
Name: c.Type.Name,
},
Comment: c.Comment,
NotNull: c.IsNotNull,
Unsigned: c.IsUnsigned,
IsArray: c.IsArray,
ArrayDims: int32(c.ArrayDims),
Length: int32(l),
Table: &plugin.Identifier{
Name: typ.Name,
},
})
}
cts = append(cts, &plugin.CompositeType{
Name: typ.Name,
Comment: typ.Comment,
Columns: columns,
})
}
}
Expand Down
84 changes: 50 additions & 34 deletions internal/codegen/golang/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,40 +67,11 @@ func buildStructs(req *plugin.GenerateRequest, options *opts.Options) []Struct {
continue
}
for _, table := range schema.Tables {
var tableName string
if schema.Name == req.Catalog.DefaultSchema {
tableName = table.Rel.Name
} else {
tableName = schema.Name + "_" + table.Rel.Name
}
structName := tableName
if !options.EmitExactTableNames {
structName = inflection.Singular(inflection.SingularParams{
Name: structName,
Exclusions: options.InflectionExcludeTableNames,
})
}
s := Struct{
Table: &plugin.Identifier{Schema: schema.Name, Name: table.Rel.Name},
Name: StructName(structName, options),
Comment: table.Comment,
}
for _, column := range table.Columns {
tags := map[string]string{}
if options.EmitDbTags {
tags["db"] = column.Name
}
if options.EmitJsonTags {
tags["json"] = JSONTagName(column.Name, options)
}
addExtraGoStructTags(tags, req, options, column)
s.Fields = append(s.Fields, Field{
Name: StructName(column.Name, options),
Type: goType(req, options, column),
Tags: tags,
Comment: column.Comment,
})
}
s := buildStruct(req, options, schema, table.Rel.Name, table.Comment, table.Columns)
structs = append(structs, s)
}
for _, ty := range schema.CompositeTypes {
s := buildStruct(req, options, schema, ty.Name, ty.Comment, ty.Columns)
structs = append(structs, s)
}
}
Expand All @@ -110,6 +81,51 @@ func buildStructs(req *plugin.GenerateRequest, options *opts.Options) []Struct {
return structs
}

func buildStruct(
req *plugin.GenerateRequest,
options *opts.Options,
schema *plugin.Schema,
rawName string,
comment string,
columns []*plugin.Column,
) Struct {
var tableName string
if schema.Name == req.Catalog.DefaultSchema {
tableName = rawName
} else {
tableName = schema.Name + "_" + rawName
}
structName := tableName
if !options.EmitExactTableNames {
structName = inflection.Singular(inflection.SingularParams{
Name: structName,
Exclusions: options.InflectionExcludeTableNames,
})
}
s := Struct{
Table: &plugin.Identifier{Schema: schema.Name, Name: rawName},
Name: StructName(structName, options),
Comment: comment,
}
for _, column := range columns {
tags := map[string]string{}
if options.EmitDbTags {
tags["db"] = column.Name
}
if options.EmitJsonTags {
tags["json"] = JSONTagName(column.Name, options)
}
addExtraGoStructTags(tags, req, options, column)
s.Fields = append(s.Fields, Field{
Name: StructName(column.Name, options),
Type: goType(req, options, column),
Tags: tags,
Comment: column.Comment,
})
}
return s
}

type goColumn struct {
id int
*plugin.Column
Expand Down
Loading