forked from nbd-wtf/go-nostr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent_extra.go
72 lines (64 loc) · 1.61 KB
/
event_extra.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
61
62
63
64
65
66
67
68
69
70
71
72
package nostr
// SetExtra sets an out-of-the-spec value under the given key into the event object.
func (evt *Event) SetExtra(key string, value any) {
if evt.extra == nil {
evt.extra = make(map[string]any)
}
evt.extra[key] = value
}
// RemoveExtra removes an out-of-the-spec value under the given key from the event object.
func (evt *Event) RemoveExtra(key string) {
if evt.extra == nil {
return
}
delete(evt.extra, key)
}
// GetExtra tries to get a value under the given key that may be present in the event object
// but is hidden in the basic type since it is out of the spec.
func (evt Event) GetExtra(key string) any {
ival, _ := evt.extra[key]
return ival
}
// GetExtraString is like [Event.GetExtra], but only works if the value is a string,
// otherwise returns the zero-value.
func (evt Event) GetExtraString(key string) string {
ival, ok := evt.extra[key]
if !ok {
return ""
}
val, ok := ival.(string)
if !ok {
return ""
}
return val
}
// GetExtraNumber is like [Event.GetExtra], but only works if the value is a float64,
// otherwise returns the zero-value.
func (evt Event) GetExtraNumber(key string) float64 {
ival, ok := evt.extra[key]
if !ok {
return 0
}
switch val := ival.(type) {
case float64:
return val
case int:
return float64(val)
case int64:
return float64(val)
}
return 0
}
// GetExtraBoolean is like [Event.GetExtra], but only works if the value is a boolean,
// otherwise returns the zero-value.
func (evt Event) GetExtraBoolean(key string) bool {
ival, ok := evt.extra[key]
if !ok {
return false
}
val, ok := ival.(bool)
if !ok {
return false
}
return val
}