-
Notifications
You must be signed in to change notification settings - Fork 36
/
plugin.go
76 lines (63 loc) · 1.8 KB
/
plugin.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
package main
import (
"github.com/iancoleman/strcase"
"github.com/stephenafamo/bob/gen"
"github.com/stephenafamo/bob/gen/bobgen-prisma/driver"
"github.com/stephenafamo/bob/gen/drivers"
)
// This plugin sets the default Alias of relationships based on the field name
// we do this after building the relationships based on the keys
type relAliasPlugin struct {
models []driver.Model
aliases gen.Aliases
config driver.Config
}
func (p relAliasPlugin) Name() string {
return "relationshipAliaser"
}
//nolint:unparam
func (p relAliasPlugin) PlugTemplateData(data *gen.TemplateData[driver.Extra]) error {
tblFilter := drivers.ParseTableFilter(p.config.Only, p.config.Except)
colFilter := drivers.ParseColumnFilter(
driver.TableNamesFromFilter(p.models, drivers.Filter{
Only: tblFilter.Only,
Except: tblFilter.Except,
}), p.config.Only, p.config.Except)
for _, model := range p.models {
if drivers.Skip(model.TableName(), tblFilter.Only, tblFilter.Except) {
continue
}
var table drivers.Table
for _, t := range data.Tables {
if t.Key == model.TableName() {
table = t
break
}
}
if table.Key == "" {
continue
}
tableName := model.TableName()
allfilter := colFilter["*"]
filter := colFilter[tableName]
include := append(allfilter.Only, filter.Only...)
exclude := append(allfilter.Except, filter.Except...)
for _, field := range model.Fields {
if drivers.Skip(field.Name, include, exclude) {
continue
}
if field.Kind != driver.FieldKindObject {
continue
}
for _, rel := range data.Relationships[table.Key] {
if rel.Name != field.RelationName {
continue
}
if p.aliases[table.Key].Relationships[rel.Name] == "" {
data.Aliases[table.Key].Relationships[rel.Name] = strcase.ToCamel(field.Name)
}
}
}
}
return nil
}