Browse Source

create new tag constructors for more flexibility

main
mleku 4 months ago
parent
commit
3867b87b8b
No known key found for this signature in database
  1. 6
      pkg/encoders/filter/filter.go
  2. 2
      pkg/encoders/filter/filter_test.go
  3. 26
      pkg/encoders/tag/tag.go
  4. 4
      pkg/protocol/auth/nip42.go

6
pkg/encoders/filter/filter.go

@ -302,7 +302,7 @@ func (f *F) Unmarshal(b []byte) (r []byte, err error) {
return return
} }
ff = append([][]byte{k}, ff...) ff = append([][]byte{k}, ff...)
s := append(*f.Tags, tag.New(ff...)) s := append(*f.Tags, tag.NewFromByteSlice(ff...))
f.Tags = &s f.Tags = &s
// f.Tags.F = append(f.Tags.F, tag.New(ff...)) // 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) { ); chk.E(err) {
return return
} }
f.Ids = tag.New(ff...) f.Ids = tag.NewFromByteSlice(ff...)
state = betweenKV state = betweenKV
case Kinds[0]: case Kinds[0]:
if len(key) < len(Kinds) { if len(key) < len(Kinds) {
@ -338,7 +338,7 @@ func (f *F) Unmarshal(b []byte) (r []byte, err error) {
); chk.E(err) { ); chk.E(err) {
return return
} }
f.Authors = tag.New(ff...) f.Authors = tag.NewFromByteSlice(ff...)
state = betweenKV state = betweenKV
case Until[0]: case Until[0]:
if len(key) < len(Until) { if len(key) < len(Until) {

2
pkg/encoders/filter/filter_test.go

@ -88,7 +88,7 @@ func GenFilter() (f *F, err error) {
idb = append(idb, id) idb = append(idb, id)
} }
idb = append([][]byte{{'#', byte(b)}}, idb...) 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...)) // f.Tags.F = append(f.Tags.F, tag.FromBytesSlice(idb...))
} }
tn := int(timestamp.Now().I64()) tn := int(timestamp.Now().I64())

26
pkg/encoders/tag/tag.go

@ -23,18 +23,26 @@ type T struct {
b bufpool.B b bufpool.B
} }
func New(t ...any) *T { func New() *T { return &T{b: bufpool.Get()} }
var bs [][]byte
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 { for _, v := range t {
if vb, ok := v.([]byte); ok { switch vv := v.(type) {
bs = append(bs, vb) case []byte:
} else if vs, ok := v.(string); ok { tt.T = append(tt.T, vv)
bs = append(bs, []byte(vs)) case string:
} else { tt.T = append(tt.T, []byte(vv))
panic("programmer error: type of tag element is not []byte or string") 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 { func NewWithCap(c int) *T {

4
pkg/protocol/auth/nip42.go

@ -32,8 +32,8 @@ func CreateUnsigned(pubkey, challenge []byte, relayURL string) (ev *event.E) {
CreatedAt: time.Now().Unix(), CreatedAt: time.Now().Unix(),
Kind: kind.ClientAuthentication.K, Kind: kind.ClientAuthentication.K,
Tags: tag.NewS( Tags: tag.NewS(
tag.New("relay", relayURL), tag.NewFromAny("relay", relayURL),
tag.New("challenge", string(challenge)), tag.NewFromAny("challenge", string(challenge)),
), ),
} }
} }

Loading…
Cancel
Save