Skip to content

Commit

Permalink
Fix delete events missing key value
Browse files Browse the repository at this point in the history
  • Loading branch information
NHAS committed Jan 21, 2024
1 parent 93c7e1c commit d6243b6
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion internal/data/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"log"
"sync"
"time"
Expand Down Expand Up @@ -165,7 +166,16 @@ func watchEvents() {

func makeBasicEvent[T any](event *clientv3.Event) (BasicEvent[T], error) {
var d T
err := json.Unmarshal(event.Kv.Value, &d)

value := event.Kv.Value
if event.Type == clientv3.EventTypeDelete {
if event.PrevKv != nil {
return BasicEvent[T]{}, fmt.Errorf("key was deleted and has no previous state: %s", event.Kv.Key)
}
value = event.PrevKv.Value
}

err := json.Unmarshal(value, &d)
if err != nil {
return BasicEvent[T]{}, err
}
Expand Down

0 comments on commit d6243b6

Please sign in to comment.