-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_serialize.go
47 lines (38 loc) · 1.04 KB
/
cmd_serialize.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
package flowstate
import (
"encoding/base64"
"encoding/json"
"fmt"
)
func Serialize(serializableStateCtx, stateCtx *StateCtx, annotation string) *SerializeCommand {
return &SerializeCommand{
SerializableStateCtx: serializableStateCtx,
StateCtx: stateCtx,
Annotation: annotation,
}
}
type SerializeCommand struct {
command
SerializableStateCtx *StateCtx
StateCtx *StateCtx
Annotation string
}
var DefaultSerializerDoer DoerFunc = func(cmd0 Command) error {
cmd, ok := cmd0.(*SerializeCommand)
if !ok {
return ErrCommandNotSupported
}
if cmd.Annotation == `` {
return fmt.Errorf("store annotation name empty")
}
if cmd.StateCtx.Current.Annotations[cmd.Annotation] != `` {
return fmt.Errorf("store annotation already set")
}
b, err := json.Marshal(cmd.SerializableStateCtx)
if err != nil {
return fmt.Errorf("json marshal prev state ctx: %s", err)
}
serialized := base64.StdEncoding.EncodeToString(b)
cmd.StateCtx.Current.SetAnnotation(cmd.Annotation, serialized)
return nil
}