-
Notifications
You must be signed in to change notification settings - Fork 6
/
aliasValue.go
77 lines (67 loc) · 1.74 KB
/
aliasValue.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
package query
import (
"fmt"
time2 "github.com/goradd/goradd/pkg/time"
"log"
"strconv"
"time"
)
// An AliasValue is returned by the GetAlias function that is generated for each type. You then convert the alias to a
// particular type to use it.
type AliasValue struct {
value string
isNil bool
}
// NewAliasValue is used by the ORM to wrap an aliased operation or computed value that was returned by a query.
// You would not normally call this function.
func NewAliasValue(a interface{}) AliasValue {
switch v := a.(type) {
case nil:
return AliasValue{"", true}
case []byte:
return AliasValue{string(v[:]), false}
default:
return AliasValue{fmt.Sprint(a), false}
}
}
// IsNil returns true if the value returned was a NULL value from the database.
func (a AliasValue) IsNil() bool {
return a.isNil
}
// IsNull is the same as IsNil, and returns true if the operation return a NULL value from the database.
func (a AliasValue) IsNull() bool {
return a.isNil
}
// String returns the value as a string. A NULL value will be an empty string.
func (a AliasValue) String() string {
return string(a.value)
}
// Int returns the value as an integer.
func (a AliasValue) Int() int {
i, err := strconv.ParseInt(a.String(), 10, 64)
if err != nil {
log.Panic(err)
}
return int(i)
}
// Time returns the value as a time.Time value.
func (a AliasValue) Time() time.Time {
t := time2.FromSqlDateTime(a.String())
return t
}
// Float returns the value as a float64
func (a AliasValue) Float() float64 {
f, err := strconv.ParseFloat(a.String(), 64)
if err != nil {
log.Panic(err)
}
return f
}
// Bool returns the value as a bool.
func (a AliasValue) Bool() bool {
b, err := strconv.ParseBool(a.String())
if err != nil {
log.Panic(err)
}
return b
}