/
naming.go
70 lines (57 loc) · 1.57 KB
/
naming.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
package config
import (
"fmt"
"github.com/gertd/go-pluralize"
"strings"
)
var (
pluralizeClient = pluralize.NewClient()
namingStrategy = &NamingStrategy{
ParentTable: defaultParentTableNamer,
ParentColumn: defaultParentColumnNamer,
}
)
// Namer is a function type which is given a string and return a string
type Namer func(string) string
// NamingStrategy represents naming strategies
type NamingStrategy struct {
ParentTable Namer
ParentColumn Namer
}
// SelectNamingStrategy sets the naming strategy
func SelectNamingStrategy(name string) bool {
switch name {
case "":
// TODO: Add case if added naming strategy
default:
fmt.Printf("Naming strategy does not exist. strategy: %s\n", name)
return false
}
return true
}
// ParentTableName alters the given name by Table
func (ns *NamingStrategy) ParentTableName(name string) string {
return ns.ParentTable(name)
}
// ParentColumnName alters the given name by Column
func (ns *NamingStrategy) ParentColumnName(name string) string {
return ns.ParentColumn(name)
}
// ToParentTableName convert string to table name
func ToParentTableName(name string) string {
return namingStrategy.ParentTableName(name)
}
// ToParentColumnName convert string to column name
func ToParentColumnName(name string) string {
return namingStrategy.ParentColumnName(name)
}
func defaultParentTableNamer(name string) string {
index := strings.LastIndex(name, "_")
if index == -1 || name[index+1:] != "id" {
return ""
}
return pluralizeClient.Plural(name[:index])
}
func defaultParentColumnNamer(name string) string {
return "id"
}