From abe84f9089c097f7f9e0bd7ae118be05bc5b2d5a Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Mon, 20 May 2024 08:26:13 -0300 Subject: [PATCH] improve COUNT envelope codec and add a test. --- envelopes.go | 5 ++++- envelopes_test.go | 13 +++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/envelopes.go b/envelopes.go index 967b43e..7bcacac 100644 --- a/envelopes.go +++ b/envelopes.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "fmt" + "strconv" "github.com/mailru/easyjson" jwriter "github.com/mailru/easyjson/jwriter" @@ -190,7 +191,9 @@ func (v CountEnvelope) MarshalJSON() ([]byte, error) { w.RawString(`["COUNT",`) w.RawString(`"` + v.SubscriptionID + `"`) if v.Count != nil { - w.RawString(fmt.Sprintf(`,{"count":%d}`, *v.Count)) + w.RawString(`,{"count":`) + w.RawString(strconv.FormatInt(*v.Count, 10)) + w.RawString(`}`) } else { for _, filter := range v.Filters { w.RawString(`,`) diff --git a/envelopes_test.go b/envelopes_test.go index 06ed8e3..0f01065 100644 --- a/envelopes_test.go +++ b/envelopes_test.go @@ -63,6 +63,19 @@ func TestEoseEnvelopeEncodingAndDecoding(t *testing.T) { } } +func TestCountEnvelopeEncodingAndDecoding(t *testing.T) { + src := `["COUNT","z",{"count":12}]` + var env CountEnvelope + json.Unmarshal([]byte(src), &env) + if *env.Count != 12 { + t.Error("failed to decode COUNT") + } + + if res, _ := json.Marshal(env); string(res) != src { + t.Errorf("failed to encode COUNT: expected '%s', got '%s'", src, string(res)) + } +} + func TestOKEnvelopeEncodingAndDecoding(t *testing.T) { okEnvelopes := []string{ `["OK","3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefaaaaa",false,"error: could not connect to the database"]`,