-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserde.go
44 lines (37 loc) · 985 Bytes
/
serde.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
//go:build !durable
package dispatch
import (
"github.com/dispatchrun/coroutine/types"
"github.com/dispatchrun/dispatch-go/dispatchproto"
)
func init() {
types.Register(dispatchSerializer, dispatchDeserializer)
}
type serializedDispatch struct {
opts []Option
functions dispatchproto.FunctionMap
}
func dispatchSerializer(s *types.Serializer, d *Dispatch) error {
opts := make([]Option, 0, len(d.opts))
for _, opt := range d.opts {
if _, ok := opt.(AnyFunction); ok {
// No need to serialize these options, since we serialize the
// map of registered functions directly.
continue
}
opts = append(opts, opt)
}
types.SerializeT(s, serializedDispatch{opts, d.functions})
return nil
}
func dispatchDeserializer(d *types.Deserializer, c *Dispatch) error {
var sd serializedDispatch
types.DeserializeTo(d, &sd)
dispatch, err := New(sd.opts...)
if err != nil {
return err
}
dispatch.functions = sd.functions
*c = *dispatch //nolint
return nil
}