lib/events: Fix unmarshaling of EventType
skip-check: authors GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/4540
This commit is contained in:
parent
67c39b2512
commit
b2af8f135b
@ -8,6 +8,7 @@
|
|||||||
package events
|
package events
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"runtime"
|
"runtime"
|
||||||
"time"
|
"time"
|
||||||
@ -118,6 +119,18 @@ func (t EventType) MarshalText() ([]byte, error) {
|
|||||||
return []byte(t.String()), nil
|
return []byte(t.String()), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *EventType) UnmarshalJSON(b []byte) error {
|
||||||
|
var s string
|
||||||
|
|
||||||
|
if err := json.Unmarshal(b, &s); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
*t = UnmarshalEventType(s)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func UnmarshalEventType(s string) EventType {
|
func UnmarshalEventType(s string) EventType {
|
||||||
switch s {
|
switch s {
|
||||||
case "Starting":
|
case "Starting":
|
||||||
|
|||||||
@ -7,6 +7,7 @@
|
|||||||
package events
|
package events
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@ -321,3 +322,20 @@ func TestSinceUsesSubscriptionId(t *testing.T) {
|
|||||||
t.Fatal("Incorrect number of events:", len(events))
|
t.Fatal("Incorrect number of events:", len(events))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUnmarshalEvent(t *testing.T) {
|
||||||
|
var event Event
|
||||||
|
|
||||||
|
s := `
|
||||||
|
{
|
||||||
|
"id": 1,
|
||||||
|
"globalID": 1,
|
||||||
|
"time": "2006-01-02T15:04:05.999999999Z",
|
||||||
|
"type": "Starting",
|
||||||
|
"data": {}
|
||||||
|
}`
|
||||||
|
|
||||||
|
if err := json.Unmarshal([]byte(s), &event); err != nil {
|
||||||
|
t.Fatal("Failed to unmarshal event:", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user