From d7ef4859342960fe9aa17393d46124fb2a15b676 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Fri, 7 Mar 2025 21:21:43 -0300 Subject: [PATCH] comments on sonic functions. --- envelopes_sonic.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/envelopes_sonic.go b/envelopes_sonic.go index 29101d7..db0db25 100644 --- a/envelopes_sonic.go +++ b/envelopes_sonic.go @@ -483,6 +483,8 @@ type sonicMessageParser struct { reusableIntArray []int } +// NewSonicMessageParser returns a sonicMessageParser object that is intended to be reused many times. +// It is not goroutine-safe. func NewSonicMessageParser() sonicMessageParser { return sonicMessageParser{ reusableFilterArray: make([]Filter, 0, 1000), @@ -536,6 +538,13 @@ func (smp *sonicMessageParser) doneWithIntSlice(slice []int) { } } +// ParseMessage parses a message like ["EVENT", ...] or ["REQ", ...] and returns an Envelope. +// The returned envelopes, filters and events' slices should not be appended to, otherwise stuff +// will break. +// +// When an unexpected message (like ["NEG-OPEN", ...]) is found, the error UnknownLabel will be +// returned. Other errors will be returned if the JSON is malformed or the objects are not exactly +// as they should. func (smp sonicMessageParser) ParseMessage(message []byte) (Envelope, error) { sv := &sonicVisitor{smp: &smp} sv.whereWeAre = inEnvelope