diff options
Diffstat (limited to 'internal/command')
-rw-r--r-- | internal/command/commandtype_string.go | 25 | ||||
-rw-r--r-- | internal/command/mutations.go | 21 | ||||
-rw-r--r-- | internal/command/objectid.go | 47 |
3 files changed, 93 insertions, 0 deletions
diff --git a/internal/command/commandtype_string.go b/internal/command/commandtype_string.go new file mode 100644 index 0000000..ef5080a --- /dev/null +++ b/internal/command/commandtype_string.go @@ -0,0 +1,25 @@ +// Code generated by "stringer -type=CommandType"; DO NOT EDIT. + +package command + +import "strconv" + +func _() { + // An "invalid array index" compiler error signifies that the constant values have changed. + // Re-run the stringer command to generate them again. + var x [1]struct{} + _ = x[Create-0] + _ = x[Merge-1] + _ = x[Remove-2] +} + +const _CommandType_name = "CreateMergeRemove" + +var _CommandType_index = [...]uint8{0, 6, 11, 17} + +func (i CommandType) String() string { + if i < 0 || i >= CommandType(len(_CommandType_index)-1) { + return "CommandType(" + strconv.FormatInt(int64(i), 10) + ")" + } + return _CommandType_name[_CommandType_index[i]:_CommandType_index[i+1]] +} diff --git a/internal/command/mutations.go b/internal/command/mutations.go new file mode 100644 index 0000000..3fe1e3f --- /dev/null +++ b/internal/command/mutations.go @@ -0,0 +1,21 @@ +package command + +import ( + "go.neonxp.dev/objectid" +) + +type Mutation struct { + ID objectid.ID + Type CommandType + Path []string + Data string +} + +//go:generate stringer -type=CommandType +type CommandType int + +const ( + Create CommandType = iota + Merge + Remove +) diff --git a/internal/command/objectid.go b/internal/command/objectid.go new file mode 100644 index 0000000..6eda868 --- /dev/null +++ b/internal/command/objectid.go @@ -0,0 +1,47 @@ +package command + +import ( + "encoding/binary" + "encoding/hex" + "math/rand" + "time" +) + +var iterator uint64 + +func init() { + rand.Seed(time.Now().UnixMicro()) + iterator = rand.Uint64() +} + +func NewObjectID() ObjectID { + iterator++ + p1 := uint64(time.Now().UnixMicro()) + p2 := iterator + p3 := rand.Uint64() + r := ObjectID{} + r = binary.BigEndian.AppendUint64(r, p1) + r = binary.BigEndian.AppendUint64(r, p2) + r = binary.BigEndian.AppendUint64(r, p3) + return r +} + +type ObjectID []byte + +func (o ObjectID) MarshalJSON() ([]byte, error) { + res := make([]byte, 0, hex.EncodedLen(len(o))) + hex.Encode(res, o) + return res, nil +} + +func (o ObjectID) String() string { + return hex.EncodeToString(o) +} + +func (o ObjectID) Time() time.Time { + if len(o) < 8 { + return time.Time{} + } + t := o[:8] + return time.UnixMicro(int64(binary.BigEndian.Uint64(t))) +} |