fix: event marshalling

This commit is contained in:
Jon Lundy 2022-08-14 10:56:00 -06:00
parent ad57c89945
commit 353b15ce61
Signed by untrusted user who does not match committer: xuu
GPG Key ID: C63E6D61F3035024
7 changed files with 87 additions and 38 deletions

View File

@ -1,8 +1,10 @@
package domain
import (
"bytes"
"context"
"crypto/sha256"
"encoding"
"fmt"
"log"
"strings"
@ -51,14 +53,14 @@ func (a *SaltyUser) OnUserRegister(name string, pubkey *keys.EdX25519PublicKey)
}
type UserRegistered struct {
Name string
Pubkey *keys.EdX25519PublicKey
Endpoint ulid.ULID
Name string
Pubkey *keys.EdX25519PublicKey
eventMeta event.Meta
}
var _ event.Event = (*UserRegistered)(nil)
var _ encoding.TextMarshaler = (*UserRegistered)(nil)
func (e *UserRegistered) EventMeta() event.Meta {
if e == nil {
@ -72,3 +74,23 @@ func (e *UserRegistered) SetEventMeta(m event.Meta) {
e.eventMeta = m
}
}
func (e *UserRegistered) MarshalText() (text []byte, err error) {
var b bytes.Buffer
b.WriteString(e.Name)
b.WriteRune('\t')
b.WriteString(e.Pubkey.String())
return b.Bytes(), nil
}
func (e *UserRegistered) UnmarshalText(b []byte) error {
name, pub, ok := bytes.Cut(b, []byte{'\t'})
if !ok {
return fmt.Errorf("parse error")
}
var err error
e.Name = string(name)
e.Pubkey, err = keys.NewEdX25519PublicKeyFromID(keys.ID(pub))
return err
}

View File

@ -2,6 +2,7 @@ package es_test
import (
"context"
"encoding/json"
"errors"
"fmt"
"testing"
@ -110,6 +111,12 @@ func (e *ValueSet) SetEventMeta(eventMeta event.Meta) {
}
e.eventMeta = eventMeta
}
func (e *ValueSet) MarshalText() ([]byte, error) {
return json.Marshal(e)
}
func (e *ValueSet) UnmarshalText(b []byte) error {
return json.Unmarshal(b, e)
}
var (
_ event.Event = (*ValueSet)(nil)

View File

@ -1,6 +1,7 @@
package event_test
import (
"encoding/json"
"testing"
"github.com/sour-is/ev/pkg/es/event"
@ -50,6 +51,13 @@ func (e *ValueApplied) SetEventMeta(m event.Meta) {
}
}
func (e *ValueApplied) MarshalText() ([]byte, error) {
return json.Marshal(e)
}
func (e *ValueApplied) UnmarshalText(b []byte) error {
return json.Unmarshal(b, e)
}
func TestAggregate(t *testing.T) {
agg := &Agg{}
event.Append(agg, &ValueApplied{Value: "one"})

View File

@ -3,6 +3,8 @@ package event
import (
"bytes"
"crypto/rand"
"encoding"
"encoding/json"
"fmt"
"io"
"strings"
@ -28,6 +30,9 @@ func getULID() ulid.ULID {
type Event interface {
EventMeta() Meta
SetEventMeta(Meta)
encoding.TextMarshaler
encoding.TextUnmarshaler
}
// Events is a list of events
@ -136,12 +141,18 @@ func (m Meta) Created() time.Time {
}
func (m Meta) GetEventID() string { return m.EventID.String() }
type nilEvent struct{}
func (nilEvent) EventMeta() Meta {
func (*nilEvent) EventMeta() Meta {
return Meta{}
}
func (nilEvent) SetEventMeta(eventMeta Meta) {}
func (*nilEvent) SetEventMeta(eventMeta Meta) {}
var NilEvent nilEvent
var NilEvent = &nilEvent{}
func (e *nilEvent) MarshalText() ([]byte, error) {
return json.Marshal(e)
}
func (e *nilEvent) UnmarshalText(b []byte) error {
return json.Unmarshal(b, e)
}

View File

@ -3,6 +3,7 @@ package event_test
import (
"bytes"
"context"
"encoding/json"
"testing"
"github.com/matryer/is"
@ -28,6 +29,12 @@ func (e *DummyEvent) SetEventMeta(eventMeta event.Meta) {
}
e.eventMeta = eventMeta
}
func (e *DummyEvent) MarshalText() ([]byte, error) {
return json.Marshal(e)
}
func (e *DummyEvent) UnmarshalText(b []byte) error {
return json.Unmarshal(b, e)
}
func TestEventEncode(t *testing.T) {
is := is.New(t)

View File

@ -30,8 +30,6 @@ type UnknownEvent struct {
}
var _ Event = (*UnknownEvent)(nil)
var _ json.Marshaler = (*UnknownEvent)(nil)
var _ json.Unmarshaler = (*UnknownEvent)(nil)
func NewUnknownEventFromValues(eventType string, meta Meta, values url.Values) *UnknownEvent {
jsonValues := make(map[string]json.RawMessage, len(values))
@ -61,10 +59,10 @@ func (u UnknownEvent) EventType() string { return u.eventType }
func (u *UnknownEvent) SetEventMeta(em Meta) {
u.eventMeta = em
}
func (u *UnknownEvent) UnmarshalJSON(b []byte) error {
func (u *UnknownEvent) UnmarshalText(b []byte) error {
return json.Unmarshal(b, &u.values)
}
func (u *UnknownEvent) MarshalJSON() ([]byte, error) {
func (u *UnknownEvent) MarshalText() ([]byte, error) {
return json.Marshal(u.values)
}
@ -102,17 +100,17 @@ func GetContainer(ctx context.Context, s string) Event {
var e Event
eventTypes.Modify(ctx, func(c *config) error {
typ, ok := c.eventTypes[s]
if !ok {
return fmt.Errorf("not defined")
}
newType := reflect.New(typ)
newInterface := newType.Interface()
if iface, ok := newInterface.(Event); ok {
e = iface
return nil
}
return fmt.Errorf("failed")
typ, ok := c.eventTypes[s]
if !ok {
return fmt.Errorf("not defined")
}
newType := reflect.New(typ)
newInterface := newType.Interface()
if iface, ok := newInterface.(Event); ok {
e = iface
return nil
}
return fmt.Errorf("failed")
})
if e == nil {
e = &UnknownEvent{eventType: s}
@ -136,14 +134,8 @@ func MarshalText(e Event) (txt []byte, err error) {
return nil, err
}
b.WriteRune('\t')
if enc, ok := e.(encoding.TextMarshaler); ok {
if txt, err = enc.MarshalText(); err != nil {
return nil, err
}
} else {
if txt, err = json.Marshal(e); err != nil {
return nil, err
}
if txt, err = e.MarshalText(); err != nil {
return nil, err
}
_, err = b.Write(txt)
@ -167,15 +159,10 @@ func UnmarshalText(ctx context.Context, txt []byte, pos uint64) (e Event, err er
eventType := string(sp[2])
e = GetContainer(ctx, eventType)
if enc, ok := e.(encoding.TextUnmarshaler); ok {
if err = enc.UnmarshalText(sp[3]); err != nil {
return nil, err
}
} else {
if err = json.Unmarshal(sp[3], e); err != nil {
return nil, err
}
if err = e.UnmarshalText(sp[3]); err != nil {
return nil, err
}
e.SetEventMeta(m)
return e, nil

View File

@ -293,6 +293,13 @@ func (e *PostEvent) SetEventMeta(eventMeta event.Meta) {
}
e.eventMeta = eventMeta
}
func (e *PostEvent) MarshalText() ([]byte, error) {
return json.Marshal(e)
}
func (e *PostEvent) UnmarshalText(b []byte) error {
return json.Unmarshal(b, e)
}
func (e *PostEvent) String() string {
var b bytes.Buffer