Skip to content

Commit

Permalink
deprecate storing extra fields in events.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Oct 16, 2024
1 parent 9e0a86d commit 14fa7a0
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 64 deletions.
16 changes: 6 additions & 10 deletions event_extra.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
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)
}
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
}
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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
54 changes: 0 additions & 54 deletions event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 14fa7a0

Please sign in to comment.