ev/app/peerfinder/ev-info.go
2023-04-02 16:45:17 -06:00

65 lines
1.2 KiB
Go

package peerfinder
import (
"bytes"
"encoding/json"
"github.com/tj/go-semver"
"go.sour.is/ev/pkg/es/event"
)
type Info struct {
ScriptVersion string `json:"script_version"`
event.AggregateRoot
}
var _ event.Aggregate = (*Info)(nil)
func (a *Info) ApplyEvent(lis ...event.Event) {
for _, e := range lis {
switch e := e.(type) {
case *VersionChanged:
a.ScriptVersion = e.ScriptVersion
}
}
}
func (a *Info) MarshalEnviron() ([]byte, error) {
var b bytes.Buffer
b.WriteString("SCRIPT_VERSION=")
b.WriteString(a.ScriptVersion)
b.WriteRune('\n')
return b.Bytes(), nil
}
func (a *Info) OnUpsert() error {
if a.StreamVersion() == 0 {
event.Raise(a, &VersionChanged{ScriptVersion: initVersion})
}
current, _ := semver.Parse(initVersion)
previous, _ := semver.Parse(a.ScriptVersion)
if current.Compare(previous) > 0 {
event.Raise(a, &VersionChanged{ScriptVersion: initVersion})
}
return nil
}
type VersionChanged struct {
ScriptVersion string `json:"script_version"`
event.IsEvent
}
var _ event.Event = (*VersionChanged)(nil)
func (e *VersionChanged) MarshalBinary() (text []byte, err error) {
return json.Marshal(e)
}
func (e *VersionChanged) UnmarshalBinary(b []byte) error {
return json.Unmarshal(b, e)
}