From 3867b87b8b84b35d7e3c65bdae6be423c2e1abdc Mon Sep 17 00:00:00 2001 From: mleku Date: Sat, 30 Aug 2025 13:34:04 +0100 Subject: [PATCH] create new tag constructors for more flexibility --- pkg/encoders/filter/filter.go | 6 +++--- pkg/encoders/filter/filter_test.go | 2 +- pkg/encoders/tag/tag.go | 26 +++++++++++++++++--------- pkg/protocol/auth/nip42.go | 4 ++-- 4 files changed, 23 insertions(+), 15 deletions(-) diff --git a/pkg/encoders/filter/filter.go b/pkg/encoders/filter/filter.go index 6f9427f..143fe50 100644 --- a/pkg/encoders/filter/filter.go +++ b/pkg/encoders/filter/filter.go @@ -302,7 +302,7 @@ func (f *F) Unmarshal(b []byte) (r []byte, err error) { return } ff = append([][]byte{k}, ff...) - s := append(*f.Tags, tag.New(ff...)) + s := append(*f.Tags, tag.NewFromByteSlice(ff...)) f.Tags = &s // f.Tags.F = append(f.Tags.F, tag.New(ff...)) // } @@ -317,7 +317,7 @@ func (f *F) Unmarshal(b []byte) (r []byte, err error) { ); chk.E(err) { return } - f.Ids = tag.New(ff...) + f.Ids = tag.NewFromByteSlice(ff...) state = betweenKV case Kinds[0]: if len(key) < len(Kinds) { @@ -338,7 +338,7 @@ func (f *F) Unmarshal(b []byte) (r []byte, err error) { ); chk.E(err) { return } - f.Authors = tag.New(ff...) + f.Authors = tag.NewFromByteSlice(ff...) state = betweenKV case Until[0]: if len(key) < len(Until) { diff --git a/pkg/encoders/filter/filter_test.go b/pkg/encoders/filter/filter_test.go index cc8b89c..4902d1b 100644 --- a/pkg/encoders/filter/filter_test.go +++ b/pkg/encoders/filter/filter_test.go @@ -88,7 +88,7 @@ func GenFilter() (f *F, err error) { idb = append(idb, id) } idb = append([][]byte{{'#', byte(b)}}, idb...) - *f.Tags = append(*f.Tags, tag.New(idb...)) + *f.Tags = append(*f.Tags, tag.NewFromByteSlice(idb...)) // f.Tags.F = append(f.Tags.F, tag.FromBytesSlice(idb...)) } tn := int(timestamp.Now().I64()) diff --git a/pkg/encoders/tag/tag.go b/pkg/encoders/tag/tag.go index c91436b..4acf3f5 100644 --- a/pkg/encoders/tag/tag.go +++ b/pkg/encoders/tag/tag.go @@ -23,18 +23,26 @@ type T struct { b bufpool.B } -func New(t ...any) *T { - var bs [][]byte +func New() *T { return &T{b: bufpool.Get()} } + +func NewFromByteSlice(t ...[]byte) (tt *T) { + tt = &T{T: t, b: bufpool.Get()} + return +} + +func NewFromAny(t ...any) (tt *T) { + tt = &T{b: bufpool.Get()} for _, v := range t { - if vb, ok := v.([]byte); ok { - bs = append(bs, vb) - } else if vs, ok := v.(string); ok { - bs = append(bs, []byte(vs)) - } else { - panic("programmer error: type of tag element is not []byte or string") + switch vv := v.(type) { + case []byte: + tt.T = append(tt.T, vv) + case string: + tt.T = append(tt.T, []byte(vv)) + default: + panic("invalid type for tag fields, must be []byte or string") } } - return &T{T: bs, b: bufpool.Get()} + return } func NewWithCap(c int) *T { diff --git a/pkg/protocol/auth/nip42.go b/pkg/protocol/auth/nip42.go index a01471b..8cdaf6c 100644 --- a/pkg/protocol/auth/nip42.go +++ b/pkg/protocol/auth/nip42.go @@ -32,8 +32,8 @@ func CreateUnsigned(pubkey, challenge []byte, relayURL string) (ev *event.E) { CreatedAt: time.Now().Unix(), Kind: kind.ClientAuthentication.K, Tags: tag.NewS( - tag.New("relay", relayURL), - tag.New("challenge", string(challenge)), + tag.NewFromAny("relay", relayURL), + tag.NewFromAny("challenge", string(challenge)), ), } }