-
Notifications
You must be signed in to change notification settings - Fork 2
/
fieldtype.go
92 lines (80 loc) · 2.56 KB
/
fieldtype.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
// Copyright 2017 NDP Systèmes. All Rights Reserved.
// See LICENSE file for full licensing details.
package fieldtype
import (
"reflect"
"github.com/labneco/doxa/doxa/models/types/dates"
)
// A Type defines a type of a model's field
type Type string
// Types for model fields
const (
NoType Type = ""
Binary Type = "binary"
Boolean Type = "boolean"
Char Type = "char"
Date Type = "date"
DateTime Type = "datetime"
Float Type = "float"
HTML Type = "html"
Integer Type = "integer"
Many2Many Type = "many2many"
Many2One Type = "many2one"
One2Many Type = "one2many"
One2One Type = "one2one"
Rev2One Type = "rev2one"
Reference Type = "reference"
Selection Type = "selection"
Text Type = "text"
)
// IsRelationType returns true if this type is a relation.
func (t Type) IsRelationType() bool {
return t == Many2Many || t == Many2One || t == One2Many || t == One2One || t == Rev2One
}
// IsFKRelationType returns true for relation types
// that are stored in the model's table (i.e. M2O and O2O)
func (t Type) IsFKRelationType() bool {
return t == Many2One || t == One2One
}
// IsNonStoredRelationType returns true for relation types
// that are not stored in the model's table (i.e. M2M, O2M and R2O)
func (t Type) IsNonStoredRelationType() bool {
return t == Many2Many || t == One2Many || t == Rev2One
}
// IsReverseRelationType returns true for relation types
// that are stored in the comodel's table (i.e. O2M and R2O)
func (t Type) IsReverseRelationType() bool {
return t == One2Many || t == Rev2One
}
// Is2OneRelationType returns true for relation types
// that point to a single comodel record (i.e. M2O, O2O and R2O)
func (t Type) Is2OneRelationType() bool {
return t == Many2One || t == One2One || t == Rev2One
}
// Is2ManyRelationType returns true for relation types
// that point to multiple comodel records (i.e. M2M and O2M)
func (t Type) Is2ManyRelationType() bool {
return t == Many2Many || t == One2Many
}
// DefaultGoType returns this Type's default Go type
func (t Type) DefaultGoType() reflect.Type {
switch t {
case NoType:
return reflect.TypeOf(nil)
case Binary, Char, Text, HTML, Selection:
return reflect.TypeOf(*new(string))
case Boolean:
return reflect.TypeOf(true)
case Date:
return reflect.TypeOf(*new(dates.Date))
case DateTime:
return reflect.TypeOf(*new(dates.DateTime))
case Float:
return reflect.TypeOf(*new(float64))
case Integer, Many2One, One2One, Rev2One:
return reflect.TypeOf(*new(int64))
case One2Many, Many2Many:
return reflect.TypeOf(*new([]int64))
}
return reflect.TypeOf(nil)
}