]> Sergey Matveev's repositories - btrtrc.git/blobdiff - bencode/tags.go
Drop support for go 1.20
[btrtrc.git] / bencode / tags.go
index 0943b41d214daf304d0860b8f1ed5cbf25334c36..d4adeb24275ce96fc7ffa9773caba56d5d9d5ca2 100644 (file)
@@ -1,34 +1,44 @@
 package bencode
 
 import (
+       "reflect"
        "strings"
 )
 
-type tag_options string
+func getTag(st reflect.StructTag) tag {
+       return parseTag(st.Get("bencode"))
+}
 
-func parse_tag(tag string) (string, tag_options) {
-       if idx := strings.Index(tag, ","); idx != -1 {
-               return tag[:idx], tag_options(tag[idx+1:])
-       }
-       return tag, tag_options("")
+type tag []string
+
+func parseTag(tagStr string) tag {
+       return strings.Split(tagStr, ",")
+}
+
+func (me tag) Ignore() bool {
+       return me[0] == "-"
+}
+
+func (me tag) Key() string {
+       return me[0]
 }
 
-func (this tag_options) contains(option_name string) bool {
-       if len(this) == 0 {
+func (me tag) HasOpt(opt string) bool {
+       if len(me) < 1 {
                return false
        }
-
-       s := string(this)
-       for s != "" {
-               var next string
-               i := strings.Index(s, ",")
-               if i != -1 {
-                       s, next = s[:i], s[i+1:]
-               }
-               if s == option_name {
+       for _, s := range me[1:] {
+               if s == opt {
                        return true
                }
-               s = next
        }
        return false
 }
+
+func (me tag) OmitEmpty() bool {
+       return me.HasOpt("omitempty")
+}
+
+func (me tag) IgnoreUnmarshalTypeError() bool {
+       return me.HasOpt("ignore_unmarshal_type_error")
+}