-
Notifications
You must be signed in to change notification settings - Fork 301
/
types.go
130 lines (114 loc) · 3.17 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
// Copyright © 2019 The Things Network Foundation, The Things Industries B.V.
//
// 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 store
import (
"database/sql/driver"
"fmt"
"github.com/lib/pq"
"go.thethings.network/lorawan-stack/v3/pkg/ttnpb"
"go.thethings.network/lorawan-stack/v3/pkg/types"
)
// EUI64 adds methods on a types.EUI64 so that it can be stored in an SQL database.
type EUI64 types.EUI64
var zeroEUI64 EUI64
// Value returns the value to store in the database.
func (eui EUI64) Value() (driver.Value, error) {
return types.EUI64(eui).String(), nil
}
func eui(eui *types.EUI64) *EUI64 {
if eui == nil {
return nil
}
converted := EUI64(*eui)
return &converted
}
func (eui *EUI64) toPB() *types.EUI64 {
if eui == nil {
return nil
}
converted := types.EUI64(*eui)
return &converted
}
// Scan reads the value from the database into the EUI.
func (eui *EUI64) Scan(src interface{}) (err error) {
var dto types.EUI64
switch src := src.(type) {
case []byte:
err = dto.UnmarshalText(src)
case string:
err = dto.UnmarshalText([]byte(src))
case nil:
*eui = EUI64{}
default:
err = fmt.Errorf("cannot convert %T to EUI64", src)
}
if err != nil {
return
}
*eui = EUI64(dto)
return nil
}
// Grants adds methods on a []ttnpb.GrantType so that it can be stored in an SQL database.
type Grants []ttnpb.GrantType
// Value returns the value to store in the database.
func (g Grants) Value() (driver.Value, error) {
ints := make([]int64, len(g))
for i, grant := range g {
ints[i] = int64(grant)
}
return pq.Int64Array(ints).Value()
}
// Scan reads the value from the database into the Grants.
func (g *Grants) Scan(src interface{}) error {
var ints pq.Int64Array
if err := ints.Scan(src); err != nil {
return err
}
grants := make(Grants, len(ints))
for i, grant := range ints {
grants[i] = ttnpb.GrantType(grant)
}
*g = grants
return nil
}
// Rights adds methods on a ttnpb.Rights so that it can be stored in an SQL database.
type Rights ttnpb.Rights
// Value returns the value to store in the database.
func (r Rights) Value() (driver.Value, error) {
ints := make([]int64, len(r.Rights))
for i, right := range r.Rights {
ints[i] = int64(right)
}
return pq.Int64Array(ints).Value()
}
// Scan reads the value from the database into the Rights.
func (r *Rights) Scan(src interface{}) error {
var ints pq.Int64Array
if err := ints.Scan(src); err != nil {
return err
}
rights := make([]ttnpb.Right, len(ints))
for i, right := range ints {
rights[i] = ttnpb.Right(right)
}
*r = Rights{Rights: rights}
return nil
}
// Location can be embedded in other models.
type Location struct {
Latitude float64
Longitude float64
Altitude int32
Accuracy int32
}