Skip to content

Commit 8880913

Browse files
committed
Generate structs for composite types
1 parent 07812e4 commit 8880913

File tree

5 files changed

+218
-163
lines changed

5 files changed

+218
-163
lines changed

internal/cmd/shim.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,34 @@ func pluginCatalog(c *catalog.Catalog) *plugin.Catalog {
7373
Vals: typ.Vals,
7474
})
7575
case *catalog.CompositeType:
76+
var columns []*plugin.Column
77+
for _, c := range typ.Columns {
78+
l := -1
79+
if c.Length != nil {
80+
l = *c.Length
81+
}
82+
columns = append(columns, &plugin.Column{
83+
Name: c.Name,
84+
Type: &plugin.Identifier{
85+
Catalog: c.Type.Catalog,
86+
Schema: c.Type.Schema,
87+
Name: c.Type.Name,
88+
},
89+
Comment: c.Comment,
90+
NotNull: c.IsNotNull,
91+
Unsigned: c.IsUnsigned,
92+
IsArray: c.IsArray,
93+
ArrayDims: int32(c.ArrayDims),
94+
Length: int32(l),
95+
Table: &plugin.Identifier{
96+
Name: typ.Name,
97+
},
98+
})
99+
}
76100
cts = append(cts, &plugin.CompositeType{
77101
Name: typ.Name,
78102
Comment: typ.Comment,
103+
Columns: columns,
79104
})
80105
}
81106
}

internal/codegen/golang/result.go

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -67,40 +67,11 @@ func buildStructs(req *plugin.GenerateRequest, options *opts.Options) []Struct {
6767
continue
6868
}
6969
for _, table := range schema.Tables {
70-
var tableName string
71-
if schema.Name == req.Catalog.DefaultSchema {
72-
tableName = table.Rel.Name
73-
} else {
74-
tableName = schema.Name + "_" + table.Rel.Name
75-
}
76-
structName := tableName
77-
if !options.EmitExactTableNames {
78-
structName = inflection.Singular(inflection.SingularParams{
79-
Name: structName,
80-
Exclusions: options.InflectionExcludeTableNames,
81-
})
82-
}
83-
s := Struct{
84-
Table: &plugin.Identifier{Schema: schema.Name, Name: table.Rel.Name},
85-
Name: StructName(structName, options),
86-
Comment: table.Comment,
87-
}
88-
for _, column := range table.Columns {
89-
tags := map[string]string{}
90-
if options.EmitDbTags {
91-
tags["db"] = column.Name
92-
}
93-
if options.EmitJsonTags {
94-
tags["json"] = JSONTagName(column.Name, options)
95-
}
96-
addExtraGoStructTags(tags, req, options, column)
97-
s.Fields = append(s.Fields, Field{
98-
Name: StructName(column.Name, options),
99-
Type: goType(req, options, column),
100-
Tags: tags,
101-
Comment: column.Comment,
102-
})
103-
}
70+
s := buildStruct(req, options, schema, table.Rel.Name, table.Comment, table.Columns)
71+
structs = append(structs, s)
72+
}
73+
for _, ty := range schema.CompositeTypes {
74+
s := buildStruct(req, options, schema, ty.Name, ty.Comment, ty.Columns)
10475
structs = append(structs, s)
10576
}
10677
}
@@ -110,6 +81,51 @@ func buildStructs(req *plugin.GenerateRequest, options *opts.Options) []Struct {
11081
return structs
11182
}
11283

84+
func buildStruct(
85+
req *plugin.GenerateRequest,
86+
options *opts.Options,
87+
schema *plugin.Schema,
88+
rawName string,
89+
comment string,
90+
columns []*plugin.Column,
91+
) Struct {
92+
var tableName string
93+
if schema.Name == req.Catalog.DefaultSchema {
94+
tableName = rawName
95+
} else {
96+
tableName = schema.Name + "_" + rawName
97+
}
98+
structName := tableName
99+
if !options.EmitExactTableNames {
100+
structName = inflection.Singular(inflection.SingularParams{
101+
Name: structName,
102+
Exclusions: options.InflectionExcludeTableNames,
103+
})
104+
}
105+
s := Struct{
106+
Table: &plugin.Identifier{Schema: schema.Name, Name: rawName},
107+
Name: StructName(structName, options),
108+
Comment: comment,
109+
}
110+
for _, column := range columns {
111+
tags := map[string]string{}
112+
if options.EmitDbTags {
113+
tags["db"] = column.Name
114+
}
115+
if options.EmitJsonTags {
116+
tags["json"] = JSONTagName(column.Name, options)
117+
}
118+
addExtraGoStructTags(tags, req, options, column)
119+
s.Fields = append(s.Fields, Field{
120+
Name: StructName(column.Name, options),
121+
Type: goType(req, options, column),
122+
Tags: tags,
123+
Comment: column.Comment,
124+
})
125+
}
126+
return s
127+
}
128+
113129
type goColumn struct {
114130
id int
115131
*plugin.Column

0 commit comments

Comments
 (0)