create new tag constructors for more flexibility

This commit is contained in:
2025-08-30 13:34:04 +01:00
parent acee5e3a90
commit 3867b87b8b
4 changed files with 23 additions and 15 deletions

View File

@@ -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) {

View File

@@ -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())

View File

@@ -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 {

View File

@@ -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)),
),
}
}