nson: test encoder with events from the wild.

This commit is contained in:
fiatjaf
2023-07-05 08:42:36 -03:00
parent 6f03e6b82c
commit 7ba3844a6f
2 changed files with 72 additions and 33 deletions
+3 -5
View File
@@ -45,7 +45,7 @@ const (
var NotNSON = fmt.Errorf("not nson") var NotNSON = fmt.Errorf("not nson")
// Unmarshal turns a NSON string into a nostr.Event struct // Unmarshal turns a NSON string into a nostr.Event struct
func Unmarshal(data string) (evt *nostr.Event, err error) { func Unmarshal(data string, evt *nostr.Event) (err error) {
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
err = fmt.Errorf("failed to decode nson: %v", r) err = fmt.Errorf("failed to decode nson: %v", r)
@@ -54,14 +54,12 @@ func Unmarshal(data string) (evt *nostr.Event, err error) {
// check if it's nson // check if it's nson
if data[NSON_MARKER_START:NSON_MARKER_END] != ",\"nson\":" { if data[NSON_MARKER_START:NSON_MARKER_END] != ",\"nson\":" {
return nil, NotNSON return NotNSON
} }
// nson values // nson values
nsonSize, nsonDescriptors := parseDescriptors(data) nsonSize, nsonDescriptors := parseDescriptors(data)
evt = &nostr.Event{}
// static fields // static fields
evt.ID = data[ID_START:ID_END] evt.ID = data[ID_START:ID_END]
evt.PubKey = data[PUBKEY_START:PUBKEY_END] evt.PubKey = data[PUBKEY_START:PUBKEY_END]
@@ -104,7 +102,7 @@ func Unmarshal(data string) (evt *nostr.Event, err error) {
evt.Tags[t] = tag evt.Tags[t] = tag
} }
return evt, err return err
} }
func Marshal(evt nostr.Event) (string, error) { func Marshal(evt nostr.Event) (string, error) {
+69 -28
View File
File diff suppressed because one or more lines are too long