-
Notifications
You must be signed in to change notification settings - Fork 2
/
event_record.go
60 lines (46 loc) · 1.74 KB
/
event_record.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package sdktypes
import (
"errors"
"time"
"google.golang.org/protobuf/types/known/timestamppb"
"go.autokitteh.dev/autokitteh/internal/kittehs"
eventv1 "go.autokitteh.dev/autokitteh/proto/gen/go/autokitteh/events/v1"
)
type EventRecord struct {
object[*EventRecordPB, EventRecordTraits]
}
type EventRecordPB = eventv1.EventRecord
type EventRecordTraits struct{}
func (EventRecordTraits) Validate(m *EventRecordPB) error {
return errors.Join(
idField[EventID]("event_id", m.EventId),
enumField[EventState]("state", m.State),
)
}
func (t EventRecordTraits) StrictValidate(m *EventRecordPB) error {
return errors.Join(
mandatory("event_id", m.EventId),
mandatory("state", m.State),
mandatory("created_at", m.CreatedAt),
)
}
func EventRecordFromProto(m *EventRecordPB) (EventRecord, error) { return FromProto[EventRecord](m) }
func StrictEventRecordFromProto(m *EventRecordPB) (EventRecord, error) {
return Strict(EventRecordFromProto(m))
}
func (p EventRecord) EventID() (_ EventID) { return kittehs.Must1(ParseEventID(p.read().EventId)) }
func (p EventRecord) WithSeq(seq uint32) EventRecord {
return EventRecord{p.forceUpdate(func(pb *EventRecordPB) { pb.Seq = seq })}
}
func (p EventRecord) WithCreatedAt(createdAt time.Time) EventRecord {
return EventRecord{p.forceUpdate(func(pb *EventRecordPB) { pb.CreatedAt = timestamppb.New(createdAt) })}
}
func (p EventRecord) Seq() uint32 { return p.read().Seq }
func (p EventRecord) State() EventState { return forceEnumFromProto[EventState](p.read().State) }
func NewEventRecord(eventID EventID, state EventState) EventRecord {
return kittehs.Must1(EventRecordFromProto(&EventRecordPB{
EventId: eventID.String(),
State: state.ToProto(),
CreatedAt: timestamppb.New(time.Now()),
}))
}