-
Notifications
You must be signed in to change notification settings - Fork 6
/
comment.go
156 lines (122 loc) · 2.99 KB
/
comment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package database
import (
"context"
"reflect"
"strings"
"github.com/dipdup-net/go-lib/hasura"
"github.com/pkg/errors"
)
const (
fieldTableName = "tableName"
fieldBaseModel = "BaseModel"
)
// MakeComments -
func MakeComments(ctx context.Context, sc SchemeCommenter, models ...interface{}) error {
if len(models) == 0 {
return nil
}
for _, model := range models {
if model == nil {
continue
}
modelType := reflect.TypeOf(model)
if reflect.ValueOf(model).Kind() == reflect.Ptr {
modelType = modelType.Elem()
}
var tableName string
for i := 0; i < modelType.NumField(); i++ {
fieldType := modelType.Field(i)
if fieldType.Name == fieldTableName || fieldType.Name == fieldBaseModel {
var ok bool
tableName, ok = getDatabaseTagName(fieldType)
if !ok {
tableName = hasura.ToSnakeCase(modelType.Name())
}
comment, ok := getComment(fieldType)
if !ok {
continue
}
if err := sc.MakeTableComment(ctx, tableName, comment); err != nil {
return err
}
continue
}
if fieldType.Anonymous {
if err := makeEmbeddedComments(ctx, sc, tableName, fieldType.Type); err != nil {
return err
}
continue
}
if err := makeFieldComment(ctx, sc, tableName, fieldType); err != nil {
return err
}
}
}
return nil
}
func makeEmbeddedComments(ctx context.Context, sc SchemeCommenter, tableName string, t reflect.Type) error {
for i := 0; i < t.NumField(); i++ {
fieldType := t.Field(i)
if fieldType.Anonymous {
if err := makeEmbeddedComments(ctx, sc, tableName, fieldType.Type); err != nil {
return err
}
continue
}
if fieldType.Name == fieldTableName {
return errors.New("Embedded type must not have tableName field.")
}
if err := makeFieldComment(ctx, sc, tableName, fieldType); err != nil {
return err
}
}
return nil
}
func makeFieldComment(ctx context.Context, sc SchemeCommenter, tableName string, fieldType reflect.StructField) error {
comment, ok := getComment(fieldType)
if !ok || comment == "" {
return nil
}
columnName, ok := getDatabaseTagName(fieldType)
if columnName == "-" {
return nil
}
if !ok {
columnName = hasura.ToSnakeCase(fieldType.Name)
}
if err := sc.MakeColumnComment(ctx, tableName, columnName, comment); err != nil {
return err
}
return nil
}
func getDatabaseTagName(fieldType reflect.StructField) (name string, ok bool) {
pgTag, pgOk := fieldType.Tag.Lookup("pg")
bunTag, bunOk := fieldType.Tag.Lookup("bun")
ok = pgOk || bunOk
var tag string
switch {
case !pgOk && !bunOk:
return "", false
case pgOk && pgTag != "-":
tag = pgTag
case bunOk && bunTag != "-":
tag = strings.TrimPrefix(bunTag, "table:")
case pgOk:
tag = pgTag
case bunOk:
tag = bunTag
}
tags := strings.Split(tag, ",")
if tags[0] != "" {
name = tags[0]
return name, ok
}
return "", false
}
func getComment(fieldType reflect.StructField) (string, bool) {
commentTag, ok := fieldType.Tag.Lookup("comment")
if ok {
return commentTag, ok
}
return "", false
}