More data on subscriptions

This commit is contained in:
2022-10-17 05:00:35 +02:00
parent 3699479c5e
commit 4a26fc791c
7 changed files with 199 additions and 50 deletions

View File

@@ -25,10 +25,14 @@ func (e Event) ToStorageType() storage.Event {
Endtime: e.Endtime,
Allday: e.Allday,
MaxSubscribers: e.MaxSubscribers,
Subscribers: e.Subscribers,
Subscriptions: []storage.Subscription{},
Data: map[string]any{},
}
for _, v := range e.Subscriptions {
event.Subscriptions = append(event.Subscriptions, v.ToStorageType())
}
for k, d := range e.Data.GetFields() {
jsondata, err := protojson.Marshal(d)
if err != nil {
@@ -43,6 +47,15 @@ func (e Event) ToStorageType() storage.Event {
return event
}
func (s Subscription) ToStorageType() storage.Subscription {
subscription := storage.Subscription{
Subscriber: s.Subscriber,
Tags: s.Tags,
}
return subscription
}
func EventFromStorageType(event *storage.Event) (*Event, error) {
d, err := sanitizeData(event.Data)
@@ -56,6 +69,13 @@ func EventFromStorageType(event *storage.Event) (*Event, error) {
return nil, err
}
subscriptions := []*Subscription{}
for _, v := range event.Subscriptions {
sub, _ := SubscriptionFromStorageType(v)
subscriptions = append(subscriptions, sub)
}
return &Event{
Id: event.ID,
Namespace: event.Namespace,
@@ -70,11 +90,33 @@ func EventFromStorageType(event *storage.Event) (*Event, error) {
Endtime: event.Endtime,
Allday: event.Allday,
MaxSubscribers: event.MaxSubscribers,
Subscribers: event.Subscribers,
Subscriptions: subscriptions,
Data: data,
}, nil
}
func SubscriptionFromStorageType(s storage.Subscription) (*Subscription, error) {
d, err := sanitizeData(s.Data)
if err != nil {
return nil, err
}
data, err := structpb.NewStruct(d)
if err != nil {
fmt.Println(err)
return nil, err
}
subscription := Subscription{
Subscriber: s.Subscriber,
Tags: s.Tags,
Data: data,
}
return &subscription, nil
}
func sanitizeData(data map[string]any) (d map[string]any, err error) {
j, err := json.Marshal(data)
if err != nil {