mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-09-05 20:50:34 +02:00
deprecate all useless tag methods, implement Find() and friends.
This commit is contained in:
102
tags.go
102
tags.go
@@ -9,18 +9,8 @@ import (
|
||||
|
||||
type Tag []string
|
||||
|
||||
// StartsWith checks if a tag contains a prefix.
|
||||
// for example,
|
||||
//
|
||||
// ["p", "abcdef...", "wss://relay.com"]
|
||||
//
|
||||
// would match against
|
||||
//
|
||||
// ["p", "abcdef..."]
|
||||
//
|
||||
// or even
|
||||
//
|
||||
// ["p", "abcdef...", "wss://"]
|
||||
// Deprecated: this is too cumbersome for no reason when what we actually want is
|
||||
// the simpler logic present in Find and FindWithValue.
|
||||
func (tag Tag) StartsWith(prefix []string) bool {
|
||||
prefixLen := len(prefix)
|
||||
|
||||
@@ -37,6 +27,7 @@ func (tag Tag) StartsWith(prefix []string) bool {
|
||||
return strings.HasPrefix(tag[prefixLen-1], prefix[prefixLen-1])
|
||||
}
|
||||
|
||||
// Deprecated: write these inline instead
|
||||
func (tag Tag) Key() string {
|
||||
if len(tag) > 0 {
|
||||
return tag[0]
|
||||
@@ -44,6 +35,7 @@ func (tag Tag) Key() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// Deprecated: write these inline instead
|
||||
func (tag Tag) Value() string {
|
||||
if len(tag) > 1 {
|
||||
return tag[1]
|
||||
@@ -51,6 +43,7 @@ func (tag Tag) Value() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// Deprecated: write these inline instead
|
||||
func (tag Tag) Relay() string {
|
||||
if len(tag) > 2 && (tag[0] == "e" || tag[0] == "p") {
|
||||
return NormalizeURL(tag[2])
|
||||
@@ -70,7 +63,7 @@ func (tags Tags) GetD() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// GetFirst gets the first tag in tags that matches the prefix, see [Tag.StartsWith]
|
||||
// Deprecated: use Find or FindWithValue instead
|
||||
func (tags Tags) GetFirst(tagPrefix []string) *Tag {
|
||||
for _, v := range tags {
|
||||
if v.StartsWith(tagPrefix) {
|
||||
@@ -80,7 +73,7 @@ func (tags Tags) GetFirst(tagPrefix []string) *Tag {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetLast gets the last tag in tags that matches the prefix, see [Tag.StartsWith]
|
||||
// Deprecated: use FindLast or FindLastWithValue instead
|
||||
func (tags Tags) GetLast(tagPrefix []string) *Tag {
|
||||
for i := len(tags) - 1; i >= 0; i-- {
|
||||
v := tags[i]
|
||||
@@ -91,7 +84,7 @@ func (tags Tags) GetLast(tagPrefix []string) *Tag {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetAll gets all the tags that match the prefix, see [Tag.StartsWith]
|
||||
// Deprecated: use FindAll instead
|
||||
func (tags Tags) GetAll(tagPrefix []string) Tags {
|
||||
result := make(Tags, 0, len(tags))
|
||||
for _, v := range tags {
|
||||
@@ -102,7 +95,7 @@ func (tags Tags) GetAll(tagPrefix []string) Tags {
|
||||
return result
|
||||
}
|
||||
|
||||
// All returns an iterator for all the tags that match the prefix, see [Tag.StartsWith]
|
||||
// Deprecated: use FindAll instead
|
||||
func (tags Tags) All(tagPrefix []string) iter.Seq2[int, Tag] {
|
||||
return func(yield func(int, Tag) bool) {
|
||||
for i, v := range tags {
|
||||
@@ -115,7 +108,7 @@ func (tags Tags) All(tagPrefix []string) iter.Seq2[int, Tag] {
|
||||
}
|
||||
}
|
||||
|
||||
// FilterOut returns a new slice with only the elements that match the prefix, see [Tag.StartsWith]
|
||||
// Deprecated: this is useless, write your own
|
||||
func (tags Tags) FilterOut(tagPrefix []string) Tags {
|
||||
filtered := make(Tags, 0, len(tags))
|
||||
for _, v := range tags {
|
||||
@@ -126,7 +119,7 @@ func (tags Tags) FilterOut(tagPrefix []string) Tags {
|
||||
return filtered
|
||||
}
|
||||
|
||||
// FilterOutInPlace removes all tags that match the prefix, but potentially reorders the tags in unpredictable ways, see [Tag.StartsWith]
|
||||
// Deprecated: this is useless, write your own
|
||||
func (tags *Tags) FilterOutInPlace(tagPrefix []string) {
|
||||
for i := 0; i < len(*tags); i++ {
|
||||
tag := (*tags)[i]
|
||||
@@ -140,8 +133,7 @@ func (tags *Tags) FilterOutInPlace(tagPrefix []string) {
|
||||
}
|
||||
}
|
||||
|
||||
// AppendUnique appends a tag if it doesn't exist yet, otherwise does nothing.
|
||||
// the uniqueness comparison is done based only on the first 2 elements of the tag.
|
||||
// Deprecated: write your own instead with Find() and append()
|
||||
func (tags Tags) AppendUnique(tag Tag) Tags {
|
||||
n := len(tag)
|
||||
if n > 2 {
|
||||
@@ -154,6 +146,49 @@ func (tags Tags) AppendUnique(tag Tag) Tags {
|
||||
return tags
|
||||
}
|
||||
|
||||
// Find returns the first tag with the given key/tagName that also has one value (i.e. at least 2 items)
|
||||
func (tags Tags) Find(key string) Tag {
|
||||
for _, v := range tags {
|
||||
if len(v) >= 2 && v[0] == key {
|
||||
return v
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// FindWithValue is like Find, but also checks if the value (the second item) matches
|
||||
func (tags Tags) FindWithValue(key, value string) Tag {
|
||||
for _, v := range tags {
|
||||
if len(v) >= 2 && v[1] == value && v[0] == key {
|
||||
return v
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// FindLast is like Find, but starts at the end
|
||||
func (tags Tags) FindLast(key string) Tag {
|
||||
for i := len(tags) - 1; i >= 0; i-- {
|
||||
v := tags[i]
|
||||
if len(v) >= 2 && v[0] == key {
|
||||
return v
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// FindLastWithValue is like FindLast, but starts at the end
|
||||
func (tags Tags) FindLastWithValue(key, value string) Tag {
|
||||
for i := len(tags) - 1; i >= 0; i-- {
|
||||
v := tags[i]
|
||||
if len(v) >= 2 && v[1] == value && v[0] == key {
|
||||
return v
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// this exists to satisfy Postgres and stuff and should probably be removed in the future since it's too specific
|
||||
func (t *Tags) Scan(src any) error {
|
||||
var jtags []byte
|
||||
|
||||
@@ -187,30 +222,3 @@ func (tags Tags) ContainsAny(tagName string, values []string) bool {
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// Marshal Tag. Used for Serialization so string escaping should be as in RFC8259.
|
||||
func (tag Tag) marshalTo(dst []byte) []byte {
|
||||
dst = append(dst, '[')
|
||||
for i, s := range tag {
|
||||
if i > 0 {
|
||||
dst = append(dst, ',')
|
||||
}
|
||||
dst = escapeString(dst, s)
|
||||
}
|
||||
dst = append(dst, ']')
|
||||
return dst
|
||||
}
|
||||
|
||||
// MarshalTo appends the JSON encoded byte of Tags as [][]string to dst.
|
||||
// String escaping is as described in RFC8259.
|
||||
func (tags Tags) marshalTo(dst []byte) []byte {
|
||||
dst = append(dst, '[')
|
||||
for i, tag := range tags {
|
||||
if i > 0 {
|
||||
dst = append(dst, ',')
|
||||
}
|
||||
dst = tag.marshalTo(dst)
|
||||
}
|
||||
dst = append(dst, ']')
|
||||
return dst
|
||||
}
|
||||
|
Reference in New Issue
Block a user