]> git.lizzy.rs Git - mt.git/commitdiff
Improve item meta handling
authoranon5 <anon5clam@protonmail.com>
Wed, 24 Feb 2021 12:23:42 +0000 (12:23 +0000)
committeranon5 <anon5clam@protonmail.com>
Wed, 24 Feb 2021 12:23:42 +0000 (12:23 +0000)
itemmeta.go [new file with mode: 0644]
mt.go [new file with mode: 0644]
stack.go

diff --git a/itemmeta.go b/itemmeta.go
new file mode 100644 (file)
index 0000000..2282dff
--- /dev/null
@@ -0,0 +1,74 @@
+package mt
+
+import "strings"
+
+type ItemMeta string
+
+var sanitizer = strings.NewReplacer(
+       string(1), "",
+       string(2), "",
+       string(3), "",
+)
+
+func NewItemMeta(fields []Field) ItemMeta {
+       if len(fields) == 0 {
+               return ""
+       }
+
+       b := new(strings.Builder)
+       b.WriteByte(1)
+       for _, f := range fields {
+               sanitizer.WriteString(b, f.Name)
+               b.WriteByte(2)
+               sanitizer.WriteString(b, f.Value)
+               b.WriteByte(3)
+       }
+       return ItemMeta(b.String())
+}
+
+func (m ItemMeta) Fields() []Field {
+       var f []Field
+       if len(m) > 0 && m[0] == 1 {
+               m = m[1:]
+               eat := func(stop byte) string {
+                       for i := 0; i < len(m); i++ {
+                               if m[i] == stop {
+                                       defer func() {
+                                               m = m[i+1:]
+                                       }()
+                                       return string(m[:i])
+                               }
+                       }
+                       defer func() {
+                               m = ""
+                       }()
+                       return string(m)
+               }
+               for len(m) > 0 {
+                       f = append(f, Field{eat(2), eat(3)})
+               }
+               return f
+       }
+
+       return []Field{{"", string(m)}}
+}
+
+func (m ItemMeta) Field(name string) (s string, ok bool) {
+       for _, f := range m.Fields() {
+               if f.Name == name {
+                       s, ok = f.Value, true
+               }
+       }
+       return
+}
+
+func (m *ItemMeta) SetField(name, value string) {
+       var fields []Field
+       for _, f := range m.Fields() {
+               if f.Name != name {
+                       fields = append(fields, f)
+               }
+       }
+       fields = append(fields, Field{name, value})
+       *m = NewItemMeta(fields)
+}
diff --git a/mt.go b/mt.go
new file mode 100644 (file)
index 0000000..c5d665d
--- /dev/null
+++ b/mt.go
@@ -0,0 +1,5 @@
+package mt
+
+type Field struct {
+       Name, Value string
+}
index 12c6338e9abfc5797c58395d4947bca267e9b936..4e7cc18c57a2d566b7c7a26367a9767c1df84e15 100644 (file)
--- a/stack.go
+++ b/stack.go
@@ -22,41 +22,6 @@ type Item struct {
        ItemMeta
 }
 
-type ItemMeta string
-
-func (m ItemMeta) Field(name string) (s string, ok bool) {
-       if len(m) > 0 && m[0] == 1 {
-               m = m[1:]
-               eat := func(stop byte) string {
-                       for i := 0; i < len(m); i++ {
-                               if m[i] == stop {
-                                       defer func() {
-                                               m = m[i+1:]
-                                       }()
-                                       return string(m[:i])
-                               }
-                       }
-                       defer func() {
-                               m = ""
-                       }()
-                       return string(m)
-               }
-               for len(m) > 0 {
-                       if eat(2) == name {
-                               s = eat(3)
-                               ok = true
-                       }
-               }
-               return
-       }
-
-       if name == "" {
-               return string(m), true
-       }
-
-       return "", false
-}
-
 func (s Stack) String() string {
        if s.Count == 0 {
                return ""