deprecate storing extra fields in events.
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
package nostr
|
package nostr
|
||||||
|
|
||||||
// SetExtra sets an out-of-the-spec value under the given key into the event object.
|
// Deprecated: this was never a good idea, stop using.
|
||||||
func (evt *Event) SetExtra(key string, value any) {
|
func (evt *Event) SetExtra(key string, value any) {
|
||||||
if evt.extra == nil {
|
if evt.extra == nil {
|
||||||
evt.extra = make(map[string]any)
|
evt.extra = make(map[string]any)
|
||||||
@@ -8,7 +8,7 @@ func (evt *Event) SetExtra(key string, value any) {
|
|||||||
evt.extra[key] = value
|
evt.extra[key] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
// RemoveExtra removes an out-of-the-spec value under the given key from the event object.
|
// Deprecated: this was never a good idea, stop using.
|
||||||
func (evt *Event) RemoveExtra(key string) {
|
func (evt *Event) RemoveExtra(key string) {
|
||||||
if evt.extra == nil {
|
if evt.extra == nil {
|
||||||
return
|
return
|
||||||
@@ -16,15 +16,13 @@ func (evt *Event) RemoveExtra(key string) {
|
|||||||
delete(evt.extra, key)
|
delete(evt.extra, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetExtra tries to get a value under the given key that may be present in the event object
|
// Deprecated: this was never a good idea, stop using.
|
||||||
// but is hidden in the basic type since it is out of the spec.
|
|
||||||
func (evt Event) GetExtra(key string) any {
|
func (evt Event) GetExtra(key string) any {
|
||||||
ival, _ := evt.extra[key]
|
ival, _ := evt.extra[key]
|
||||||
return ival
|
return ival
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetExtraString is like [Event.GetExtra], but only works if the value is a string,
|
// Deprecated: this was never a good idea, stop using.
|
||||||
// otherwise returns the zero-value.
|
|
||||||
func (evt Event) GetExtraString(key string) string {
|
func (evt Event) GetExtraString(key string) string {
|
||||||
ival, ok := evt.extra[key]
|
ival, ok := evt.extra[key]
|
||||||
if !ok {
|
if !ok {
|
||||||
@@ -37,8 +35,7 @@ func (evt Event) GetExtraString(key string) string {
|
|||||||
return val
|
return val
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetExtraNumber is like [Event.GetExtra], but only works if the value is a float64,
|
// Deprecated: this was never a good idea, stop using.
|
||||||
// otherwise returns the zero-value.
|
|
||||||
func (evt Event) GetExtraNumber(key string) float64 {
|
func (evt Event) GetExtraNumber(key string) float64 {
|
||||||
ival, ok := evt.extra[key]
|
ival, ok := evt.extra[key]
|
||||||
if !ok {
|
if !ok {
|
||||||
@@ -57,8 +54,7 @@ func (evt Event) GetExtraNumber(key string) float64 {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetExtraBoolean is like [Event.GetExtra], but only works if the value is a boolean,
|
// Deprecated: this was never a good idea, stop using.
|
||||||
// otherwise returns the zero-value.
|
|
||||||
func (evt Event) GetExtraBoolean(key string) bool {
|
func (evt Event) GetExtraBoolean(key string) bool {
|
||||||
ival, ok := evt.extra[key]
|
ival, ok := evt.extra[key]
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|||||||
@@ -74,60 +74,6 @@ func TestEventSerialization(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEventSerializationWithExtraFields(t *testing.T) {
|
|
||||||
evt := Event{
|
|
||||||
ID: "92570b321da503eac8014b23447301eb3d0bbdfbace0d11a4e4072e72bb7205d",
|
|
||||||
PubKey: "e9142f724955c5854de36324dab0434f97b15ec6b33464d56ebe491e3f559d1b",
|
|
||||||
Kind: KindReaction,
|
|
||||||
CreatedAt: Timestamp(1671028682),
|
|
||||||
Content: "there is an extra field here",
|
|
||||||
Sig: "ed08d2dd5b0f7b6a3cdc74643d4adee3158ddede9cc848e8cd97630c097001acc2d052d2d3ec2b7ac4708b2314b797106d1b3c107322e61b5e5cc2116e099b79",
|
|
||||||
}
|
|
||||||
evt.SetExtra("glub", true)
|
|
||||||
evt.SetExtra("plik", nil)
|
|
||||||
evt.SetExtra("elet", 77)
|
|
||||||
evt.SetExtra("malf", "hello")
|
|
||||||
|
|
||||||
b, err := json.Marshal(evt)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
|
|
||||||
var re Event
|
|
||||||
err = json.Unmarshal(b, &re)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
|
|
||||||
assert.Condition(t, func() (success bool) {
|
|
||||||
if evt.ID != re.ID || evt.PubKey != re.PubKey || evt.Content != re.Content ||
|
|
||||||
evt.CreatedAt != re.CreatedAt || evt.Sig != re.Sig ||
|
|
||||||
len(evt.Tags) != len(re.Tags) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}, "reparsed event differs from original")
|
|
||||||
|
|
||||||
assert.Condition(t, func() (success bool) {
|
|
||||||
if evt.GetExtra("malf").(string) != evt.GetExtraString("malf") || evt.GetExtraString("malf") != "hello" {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}, "failed to parse extra string")
|
|
||||||
|
|
||||||
assert.Condition(t, func() (success bool) {
|
|
||||||
if float64(evt.GetExtra("elet").(int)) != evt.GetExtraNumber("elet") || evt.GetExtraNumber("elet") != 77 {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}, "failed to parse extra number")
|
|
||||||
|
|
||||||
assert.Condition(t, func() (success bool) {
|
|
||||||
if evt.GetExtra("glub").(bool) != evt.GetExtraBoolean("glub") || evt.GetExtraBoolean("glub") != true {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}, "failed to parse extra boolean")
|
|
||||||
|
|
||||||
assert.Nil(t, evt.GetExtra("plik"))
|
|
||||||
}
|
|
||||||
|
|
||||||
func mustSignEvent(t *testing.T, privkey string, event *Event) {
|
func mustSignEvent(t *testing.T, privkey string, event *Event) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
if err := event.Sign(privkey); err != nil {
|
if err := event.Sign(privkey); err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user