deprecate storing extra fields in events.

This commit is contained in:
fiatjaf
2024-10-16 09:37:41 -03:00
parent 9e0a86d48e
commit 14fa7a0358
2 changed files with 6 additions and 64 deletions

View File

@@ -1,6 +1,6 @@
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) {
if evt.extra == nil {
evt.extra = make(map[string]any)
@@ -8,7 +8,7 @@ func (evt *Event) SetExtra(key string, value any) {
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) {
if evt.extra == nil {
return
@@ -16,15 +16,13 @@ func (evt *Event) RemoveExtra(key string) {
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.
// Deprecated: this was never a good idea, stop using.
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.
// Deprecated: this was never a good idea, stop using.
func (evt Event) GetExtraString(key string) string {
ival, ok := evt.extra[key]
if !ok {
@@ -37,8 +35,7 @@ func (evt Event) GetExtraString(key string) string {
return val
}
// GetExtraNumber is like [Event.GetExtra], but only works if the value is a float64,
// otherwise returns the zero-value.
// Deprecated: this was never a good idea, stop using.
func (evt Event) GetExtraNumber(key string) float64 {
ival, ok := evt.extra[key]
if !ok {
@@ -57,8 +54,7 @@ func (evt Event) GetExtraNumber(key string) float64 {
return 0
}
// GetExtraBoolean is like [Event.GetExtra], but only works if the value is a boolean,
// otherwise returns the zero-value.
// Deprecated: this was never a good idea, stop using.
func (evt Event) GetExtraBoolean(key string) bool {
ival, ok := evt.extra[key]
if !ok {

View File

@@ -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) {
t.Helper()
if err := event.Sign(privkey); err != nil {