2022-11-23 13:51:55 -07:00
|
|
|
package peerfinder
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"github.com/tj/go-semver"
|
|
|
|
|
2023-02-26 22:33:01 -07:00
|
|
|
"go.sour.is/ev/pkg/es/event"
|
2022-11-23 13:51:55 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
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"`
|
|
|
|
|
2023-04-02 16:45:17 -06:00
|
|
|
event.IsEvent
|
2022-11-23 13:51:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|