-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathsqlx.go
140 lines (113 loc) · 2.79 KB
/
sqlx.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
// Copyright © 2022 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package x
import (
"database/sql"
"database/sql/driver"
"encoding/json"
"fmt"
"time"
"github.com/pkg/errors"
"github.com/ory/x/errorsx"
jose "github.com/go-jose/go-jose/v3"
)
// swagger:type JSONWebKeySet
type JoseJSONWebKeySet struct {
// swagger:ignore
*jose.JSONWebKeySet
}
func (n *JoseJSONWebKeySet) Scan(value interface{}) error {
v := fmt.Sprintf("%s", value)
if len(v) == 0 {
return nil
}
return errorsx.WithStack(json.Unmarshal([]byte(v), n))
}
func (n *JoseJSONWebKeySet) Value() (driver.Value, error) {
value, err := json.Marshal(n)
if err != nil {
return nil, errorsx.WithStack(err)
}
return string(value), nil
}
type Duration time.Duration
// MarshalJSON returns m as the JSON encoding of m.
func (ns Duration) MarshalJSON() ([]byte, error) {
return json.Marshal(time.Duration(ns).String())
}
// UnmarshalJSON sets *m to a copy of data.
func (ns *Duration) UnmarshalJSON(data []byte) error {
if ns == nil {
return errors.New("json.RawMessage: UnmarshalJSON on nil pointer")
}
if len(data) == 0 || string(data) == "null" {
return nil
}
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
p, err := time.ParseDuration(s)
if err != nil {
return err
}
*ns = Duration(p)
return nil
}
// swagger:model NullDuration
//
//lint:ignore U1000 Used to generate Swagger and OpenAPI definitions
type swaggerNullDuration string
// NullDuration represents a nullable JSON and SQL compatible time.Duration.
//
// TODO delete this type and replace it with ory/x/sqlxx/NullDuration when applying the custom client token TTL patch to Hydra 2.x
//
// swagger:ignore
type NullDuration struct {
Duration time.Duration
Valid bool
}
// Scan implements the Scanner interface.
func (ns *NullDuration) Scan(value interface{}) error {
var d = sql.NullInt64{}
if err := d.Scan(value); err != nil {
return err
}
ns.Duration = time.Duration(d.Int64)
ns.Valid = d.Valid
return nil
}
// Value implements the driver Valuer interface.
func (ns NullDuration) Value() (driver.Value, error) {
if !ns.Valid {
return nil, nil
}
return int64(ns.Duration), nil
}
// MarshalJSON returns m as the JSON encoding of m.
func (ns NullDuration) MarshalJSON() ([]byte, error) {
if !ns.Valid {
return []byte("null"), nil
}
return json.Marshal(ns.Duration.String())
}
// UnmarshalJSON sets *m to a copy of data.
func (ns *NullDuration) UnmarshalJSON(data []byte) error {
if ns == nil {
return errors.New("json.RawMessage: UnmarshalJSON on nil pointer")
}
if len(data) == 0 || string(data) == "null" {
return nil
}
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
p, err := time.ParseDuration(s)
if err != nil {
return err
}
ns.Duration = p
ns.Valid = true
return nil
}