-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Turns out CBOR by default serializes date time into unix timestamp and drops the serialization information about it. This PR adds new struct and enforces tag to be serialized and deserialized along with the values.
- Loading branch information
Showing
6 changed files
with
128 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package core | ||
|
||
import "github.com/fxamacker/cbor/v2" | ||
|
||
var CBORTags = cbor.NewTagSet() | ||
|
||
type ReplicableEvent[T any] interface { | ||
Wrap() (T, error) | ||
Unwrap() (T, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,49 @@ | ||
package logstream | ||
|
||
import "github.com/fxamacker/cbor/v2" | ||
import ( | ||
"github.com/fxamacker/cbor/v2" | ||
"github.com/maxpert/marmot/core" | ||
) | ||
|
||
type ReplicationEvent[T any] struct { | ||
type ReplicationEvent[T core.ReplicableEvent[T]] struct { | ||
FromNodeId uint64 | ||
Payload *T | ||
Payload T | ||
} | ||
|
||
func (e *ReplicationEvent[T]) Marshal() ([]byte, error) { | ||
return cbor.Marshal(e) | ||
wrappedPayload, err := e.Payload.Wrap() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ev := ReplicationEvent[T]{ | ||
FromNodeId: e.FromNodeId, | ||
Payload: wrappedPayload, | ||
} | ||
|
||
em, err := cbor.EncOptions{}.EncModeWithTags(core.CBORTags) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return em.Marshal(ev) | ||
} | ||
|
||
func (e *ReplicationEvent[T]) Unmarshal(data []byte) error { | ||
return cbor.Unmarshal(data, e) | ||
dm, err := cbor.DecOptions{}.DecModeWithTags(core.CBORTags) | ||
if err != nil { | ||
return nil | ||
} | ||
|
||
err = dm.Unmarshal(data, e) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
e.Payload, err = e.Payload.Unwrap() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters