-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_deserialize.go
48 lines (38 loc) · 1.06 KB
/
cmd_deserialize.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
package flowstate
import (
"encoding/base64"
"encoding/json"
"fmt"
)
func Deserialize(stateCtx, deserializedStateCtx *StateCtx, annotation string) *DeserializeCommand {
return &DeserializeCommand{
StateCtx: stateCtx,
DeserializedStateCtx: deserializedStateCtx,
Annotation: annotation,
}
}
type DeserializeCommand struct {
command
StateCtx *StateCtx
DeserializedStateCtx *StateCtx
Annotation string
}
var DefaultDeserializeDoer DoerFunc = func(cmd0 Command) error {
cmd, ok := cmd0.(*DeserializeCommand)
if !ok {
return ErrCommandNotSupported
}
serializedState := cmd.StateCtx.Current.Annotations[cmd.Annotation]
if serializedState == `` {
return fmt.Errorf("store annotation value empty")
}
b, err := base64.StdEncoding.DecodeString(serializedState)
if err != nil {
return fmt.Errorf("base64 decode: %s", err)
}
if err := json.Unmarshal(b, cmd.DeserializedStateCtx); err != nil {
return fmt.Errorf("json unmarshal: %s", err)
}
cmd.StateCtx.Current.Annotations[cmd.Annotation] = ``
return nil
}