-
Notifications
You must be signed in to change notification settings - Fork 0
/
serializer.go
83 lines (67 loc) · 1.45 KB
/
serializer.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
package pgx
import (
"bytes"
"encoding/gob"
"errors"
a "github.com/dalthon/ana"
pgx "github.com/jackc/pgx/v5"
)
func serialize[S any](value *S) []byte {
if value == nil {
return []byte{}
}
var buffer bytes.Buffer
encoder := gob.NewEncoder(&buffer)
if encoder.Encode(value) != nil {
panic("Could not encode data.")
}
return buffer.Bytes()
}
func deserialize[S any](encoded []byte) *S {
if len(encoded) == 0 {
return nil
}
decoder := gob.NewDecoder(bytes.NewBuffer(encoded))
var decoded S
if decoder.Decode(&decoded) != nil {
panic("Could not decode data.")
}
return &decoded
}
func rowsToTrackedOperation[P any, R any](rows pgx.Rows) *a.TrackedOperation[P, R] {
var operation a.TrackedOperation[P, R]
var status string
var errorMessage string
var encodedPayload []byte
var encodedResult []byte
rows.Next()
rows.Scan(
&status,
&operation.Key,
&operation.Target,
&encodedPayload,
&operation.ReferenceTime,
&operation.StartedAt,
&operation.Timeout,
&operation.Expiration,
&encodedResult,
&errorMessage,
)
rows.Close()
switch status {
case "ready":
operation.Status = a.Ready
case "running":
operation.Status = a.Running
case "finished":
operation.Status = a.Finished
case "failed":
operation.Status = a.Failed
}
if errorMessage != "" {
operation.Err = errors.New(errorMessage)
}
operation.Payload = deserialize[P](encodedPayload)
operation.Result = deserialize[R](encodedResult)
return &operation
}