File tree Expand file tree Collapse file tree 3 files changed +384
-81
lines changed
Expand file tree Collapse file tree 3 files changed +384
-81
lines changed Original file line number Diff line number Diff line change 1+ package proto
2+
3+ import (
4+ "bytes"
5+ "database/sql/driver"
6+ "encoding/json"
7+ "fmt"
8+ )
9+
10+ // TxnArgs preserves JSON number precision when decoding from the database.
11+ type TxnArgs map [string ]interface {}
12+
13+ func (t TxnArgs ) MarshalJSON () ([]byte , error ) {
14+ if t == nil {
15+ return []byte ("null" ), nil
16+ }
17+ return json .Marshal (map [string ]interface {}(t ))
18+ }
19+
20+ func (t * TxnArgs ) UnmarshalJSON (data []byte ) error {
21+ if len (data ) == 0 || string (data ) == "null" {
22+ * t = nil
23+ return nil
24+ }
25+
26+ dec := json .NewDecoder (bytes .NewReader (data ))
27+ dec .UseNumber ()
28+
29+ var out map [string ]interface {}
30+ if err := dec .Decode (& out ); err != nil {
31+ return err
32+ }
33+ * t = TxnArgs (out )
34+ return nil
35+ }
36+
37+ func (t TxnArgs ) Value () (driver.Value , error ) {
38+ if t == nil {
39+ return nil , nil
40+ }
41+
42+ payload , err := json .Marshal (map [string ]interface {}(t ))
43+ if err != nil {
44+ return nil , err
45+ }
46+ return payload , nil
47+ }
48+
49+ func (t * TxnArgs ) Scan (src interface {}) error {
50+ if src == nil {
51+ * t = nil
52+ return nil
53+ }
54+
55+ switch value := src .(type ) {
56+ case []byte :
57+ return t .UnmarshalJSON (value )
58+ case string :
59+ return t .UnmarshalJSON ([]byte (value ))
60+ case json.RawMessage :
61+ return t .UnmarshalJSON (value )
62+ case map [string ]interface {}:
63+ * t = TxnArgs (value )
64+ return nil
65+ default :
66+ return fmt .Errorf ("TxnArgs.Scan: unsupported type %T" , src )
67+ }
68+ }
You can’t perform that action at this time.
0 commit comments