File size: 2,620 Bytes
1e92f2d |
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 |
package gadb
import (
"crypto/sha256"
"encoding/json"
"fmt"
)
func NewDestV1(typeID string, args ...string) DestV1 {
d := DestV1{
Type: typeID,
}
if len(args)%2 != 0 {
panic("args must be key-value pairs")
}
for i := 0; i < len(args); i += 2 {
d.SetArg(args[i], args[i+1])
}
return d
}
type DestHashV1 [32]byte
func (ns DestV1) DestHash() DestHashV1 {
data, err := json.Marshal(ns)
if err != nil {
panic(err)
}
return sha256.Sum256(data)
}
func (ns DestV1) String() string {
return fmt.Sprintf("DestV1{Type: %s, Args: %v}", ns.Type, ns.Args)
}
type DestV1 struct {
Args map[string]string
Type string
}
func (ns DestV1) Equal(other DestV1) bool {
if ns.Type != other.Type {
return false
}
if len(ns.Args) != len(other.Args) {
return false
}
for k, v := range ns.Args {
if other.Args[k] != v {
return false
}
}
return true
}
func (ns DestV1) Arg(name string) string {
if ns.Args == nil {
return ""
}
return ns.Args[name]
}
func (ns *DestV1) SetArg(name, value string) {
if ns.Args == nil {
ns.Args = make(map[string]string)
}
ns.Args[name] = value
}
// Scan implements the Scanner interface.
func (ns *DestV1) Scan(value interface{}) error {
switch v := value.(type) {
case json.RawMessage:
err := json.Unmarshal(v, ns)
if err != nil {
return err
}
case []byte:
err := json.Unmarshal(v, ns)
if err != nil {
return err
}
case string:
err := json.Unmarshal([]byte(v), ns)
if err != nil {
return err
}
default:
return fmt.Errorf("unsupported scan for DestV1 type: %T", value)
}
return nil
}
// Value implements the driver Valuer interface.
func (ns DestV1) Value() (interface{}, error) {
if ns.Args == nil {
ns.Args = map[string]string{}
}
data, err := json.Marshal(ns)
if err != nil {
return nil, err
}
return json.RawMessage(data), nil
}
type NullDestV1 struct {
Valid bool
DestV1 DestV1
}
func (ns NullDestV1) MarshalJSON() ([]byte, error) {
if !ns.Valid {
return []byte("null"), nil
}
return json.Marshal(ns.DestV1)
}
func (ns *NullDestV1) UnmarshalJSON(data []byte) error {
if string(data) == "null" {
ns.Valid = false
return nil
}
ns.Valid = true
return json.Unmarshal(data, &ns.DestV1)
}
// Scan implements the Scanner interface.
func (ns *NullDestV1) Scan(value interface{}) error {
if value == nil {
ns.DestV1, ns.Valid = DestV1{}, false
return nil
}
ns.Valid = true
return ns.DestV1.Scan(value)
}
// Value implements the driver Valuer interface.
func (ns NullDestV1) Value() (interface{}, error) {
if !ns.Valid {
return nil, nil
}
return ns.DestV1.Value()
}
|