forked from goadesign/gorma
/
relationalfield.go
148 lines (130 loc) · 3.73 KB
/
relationalfield.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
package gorma
import (
"fmt"
"strings"
"github.com/goadesign/goa/dslengine"
"bitbucket.org/pkg/inflect"
)
// Context returns the generic definition name used in error messages.
func (f *RelationalFieldDefinition) Context() string {
if f.Name != "" {
return fmt.Sprintf("RelationalField %#v", f.Name)
}
return "unnamed RelationalField"
}
// DSL returns this object's DSL
func (f *RelationalFieldDefinition) DSL() func() {
return f.DefinitionDSL
}
// Children returnsa slice of this objects children
func (f RelationalFieldDefinition) Children() []dslengine.Definition {
// no children yet
return []dslengine.Definition{}
}
// FieldDefinition returns the field's struct definition
func (f *RelationalFieldDefinition) FieldDefinition() string {
var comment string
if f.Description != "" {
comment = "// " + f.Description
}
def := fmt.Sprintf("%s\t%s %s %s\n", f.Name, goDatatype(f), tags(f), comment)
return def
}
// Tags returns the sql and gorm struct tags for the Definition
func (f *RelationalFieldDefinition) Tags() string {
return tags(f)
}
// LowerName returns the field name as a lowercase string.
func (f *RelationalFieldDefinition) LowerName() string {
return strings.ToLower(f.Name)
}
// Underscore returns the field name as a lowercase string in snake case
func (f *RelationalFieldDefinition) Underscore() string {
return inflect.Underscore(f.Name)
}
func goDatatype(f *RelationalFieldDefinition) string {
var ptr string
if f.Nullable {
ptr = "*"
}
switch f.Datatype {
case Boolean:
return ptr + "bool"
case Integer, BigInteger:
return ptr + "int"
case AutoInteger, AutoBigInteger:
return ptr + "int " // sql/gorm tags later
case Decimal, BigDecimal:
return ptr + "float"
case String:
return ptr + "string"
case Text:
return ptr + "string"
case UUID:
return ptr + "string" // what to do about UUIDS?
case PKInteger:
return ptr + "int"
case PKBigInteger:
return ptr + "int"
case PKUUID:
return ptr + "string " // TBD
case Timestamp, NullableTimestamp:
return ptr + "time.Time"
case BelongsTo:
return ptr + "int"
case HasMany:
return fmt.Sprintf("[]%s", f.HasMany)
case HasManyKey, HasOneKey:
return ptr + "int"
case HasOne:
return fmt.Sprintf("%s", f.HasOne)
default:
if f.Many2Many != "" {
return fmt.Sprintf("[]%s", f.Many2Many)
}
}
return "UNKNOWN TYPE"
}
func tags(f *RelationalFieldDefinition) string {
var sqltags []string
if f.SQLTag != "" {
sqltags = append(sqltags, f.SQLTag)
}
var gormtags []string
if f.Alias != "" {
gormtags = append(gormtags, "column:"+f.Alias)
}
if f.PrimaryKey {
gormtags = append(gormtags, "primary_key")
}
var tags []string
if len(sqltags) > 0 {
sqltag := "sql:\"" + strings.Join(sqltags, ";") + "\""
tags = append(tags, sqltag)
}
if len(gormtags) > 0 {
gormtag := "gorm:\"" + strings.Join(gormtags, ";") + "\""
tags = append(tags, gormtag)
}
if len(tags) > 0 {
return "`" + strings.Join(tags, " ") + "`"
}
return ""
}
/*
Boolean FieldType = "bool"
Integer FieldType = "integer"
BigInteger FieldType = "biginteger"
AutoInteger FieldType = "auto_integer"
AutoBigInteger FieldType = "auto_biginteger"
Decimal FieldType = "decimal"
BigDecimal FieldType = "bigdecimal"
String FieldType = "string"
Text FieldType = "text"
UUID FieldType = "uuid"
PKInteger FieldType = "pkinteger"
PKBigInteger FieldType = "pkbiginteger"
PKUUID FieldType = "pkuuid"
Timestamp FieldType = "timestamp"
NullableTimestamp FieldType = "nulltimestamp"
*/