forked from SkygearIO/skygear-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
183 lines (149 loc) · 4.11 KB
/
types.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright 2015-present Oursky Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package pq
import (
"database/sql/driver"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/paulmach/go.geo"
"github.com/skygeario/skygear-server/skydb"
)
// This file implements Record related operations of the
// skydb/pq implementation.
// Different data types that can be saved in and loaded from postgreSQL
// NOTE(limouren): varchar is missing because text can replace them,
// see the docs here: http://www.postgresql.org/docs/9.4/static/datatype-character.html
const (
TypeString = "text"
TypeNumber = "double precision"
TypeBoolean = "boolean"
TypeJSON = "jsonb"
TypeTimestamp = "timestamp without time zone"
TypeLocation = "geometry(Point)"
TypeInteger = "integer"
TypeSerial = "serial UNIQUE"
)
type nullJSON struct {
JSON interface{}
Valid bool
}
func (nj *nullJSON) Scan(value interface{}) error {
data, ok := value.([]byte)
if value == nil || !ok {
nj.JSON = nil
nj.Valid = false
return nil
}
err := json.Unmarshal(data, &nj.JSON)
nj.Valid = err == nil
return err
}
// nullJSONStringSlice will reject empty member, since pq will give [null]
// array if we use `array_to_json` on null column. So the result slice will be
// []string{}, but not []string{""}
type nullJSONStringSlice struct {
slice []string
Valid bool
}
func (njss *nullJSONStringSlice) Scan(value interface{}) error {
data, ok := value.([]byte)
if value == nil || !ok {
njss.slice = nil
njss.Valid = false
return nil
}
njss.slice = []string{}
allSlice := []string{}
err := json.Unmarshal(data, &allSlice)
for _, s := range allSlice {
if s != "" {
njss.slice = append(njss.slice, s)
}
}
njss.Valid = err == nil
return err
}
type assetValue skydb.Asset
func (asset assetValue) Value() (driver.Value, error) {
return asset.Name, nil
}
type nullAsset struct {
Asset *skydb.Asset
Valid bool
}
func (na *nullAsset) Scan(value interface{}) error {
if value == nil {
na.Asset = &skydb.Asset{}
na.Valid = false
return nil
}
assetName, ok := value.([]byte)
if !ok {
return fmt.Errorf("failed to scan Asset: got type(value) = %T, expect []byte", value)
}
na.Asset = &skydb.Asset{
Name: string(assetName),
}
na.Valid = true
return nil
}
type nullLocation struct {
Location skydb.Location
Valid bool
}
func (nl *nullLocation) Scan(value interface{}) error {
if value == nil {
nl.Location = skydb.Location{}
nl.Valid = false
return nil
}
src, ok := value.([]byte)
if !ok {
return fmt.Errorf("failed to scan Location: got type(value) = %T, expect []byte", value)
}
// TODO(limouren): instead of decoding a str-encoded hex, we should utilize
// ST_AsBinary to perform the SELECT
decoded := make([]byte, hex.DecodedLen(len(src)))
_, err := hex.Decode(decoded, src)
if err != nil {
return fmt.Errorf("failed to scan Location: malformed wkb")
}
err = (*geo.Point)(&nl.Location).Scan(decoded)
nl.Valid = err == nil
return err
}
type referenceValue skydb.Reference
func (ref referenceValue) Value() (driver.Value, error) {
return ref.ID.Key, nil
}
type jsonSliceValue []interface{}
func (s jsonSliceValue) Value() (driver.Value, error) {
return json.Marshal([]interface{}(s))
}
type jsonMapValue map[string]interface{}
func (m jsonMapValue) Value() (driver.Value, error) {
return json.Marshal(map[string]interface{}(m))
}
type aclValue skydb.RecordACL
func (acl aclValue) Value() (driver.Value, error) {
if acl == nil {
return nil, nil
}
return json.Marshal(acl)
}
type locationValue skydb.Location
func (loc locationValue) Value() (driver.Value, error) {
return geo.Point(loc).ToWKT(), nil
}