Skip to content

Commit 0754e20

Browse files
committed
MakeComments refined with support of Anonymous embedded types in struct.
1 parent e93b928 commit 0754e20

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

database/comment.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package database
33
import (
44
"context"
55
"github.com/dipdup-net/go-lib/hasura"
6+
"github.com/pkg/errors"
67
"reflect"
78
"strings"
89
)
@@ -47,6 +48,12 @@ func MakeComments(ctx context.Context, sc SchemeCommenter, models ...interface{}
4748
continue
4849
}
4950

51+
if fieldType.Anonymous {
52+
if err := makeEmbeddedComments(ctx, sc, tableName, fieldType.Type); err != nil {
53+
return err
54+
}
55+
}
56+
5057
comment, ok := getComment(fieldType)
5158
if !ok || comment == "" {
5259
continue
@@ -70,6 +77,37 @@ func MakeComments(ctx context.Context, sc SchemeCommenter, models ...interface{}
7077
return nil
7178
}
7279

80+
func makeEmbeddedComments(ctx context.Context, sc SchemeCommenter, tableName string, t reflect.Type) error {
81+
for i := 0; i < t.NumField(); i++ {
82+
fieldType := t.Field(i)
83+
84+
if fieldType.Name == "tableName" {
85+
return errors.New("Embedded type must not have tableName field.")
86+
}
87+
88+
comment, ok := getComment(fieldType)
89+
if !ok || comment == "" {
90+
continue
91+
}
92+
93+
columnName, ok := getPgName(fieldType)
94+
95+
if columnName == "-" {
96+
continue
97+
}
98+
99+
if !ok {
100+
columnName = hasura.ToSnakeCase(fieldType.Name)
101+
}
102+
103+
if err := sc.MakeColumnComment(ctx, tableName, columnName, comment); err != nil {
104+
return err
105+
}
106+
}
107+
108+
return nil
109+
}
110+
73111
func getPgName(fieldType reflect.StructField) (name string, ok bool) {
74112
pgTag, ok := fieldType.Tag.Lookup("pg")
75113
if !ok {

0 commit comments

Comments
 (0)