[Groonga-commit] groonga/grnci at 1624c8a [master] Add parseFieldTag() for parsing field tags

Back to archive index

Susumu Yata null+****@clear*****
Thu Feb 11 13:01:33 JST 2016


Susumu Yata	2016-02-11 13:01:33 +0900 (Thu, 11 Feb 2016)

  New Revision: 1624c8a89836f844742e6294fb9ce42ad1ae82a1
  https://github.com/groonga/grnci/commit/1624c8a89836f844742e6294fb9ce42ad1ae82a1

  Message:
    Add parseFieldTag() for parsing field tags
    
    GitHub: #23

  Modified files:
    grnci.go

  Modified: grnci.go (+45 -1)
===================================================================
--- grnci.go    2016-02-11 11:12:08 +0900 (c586c30)
+++ grnci.go    2016-02-11 13:01:33 +0900 (c6e040e)
@@ -251,6 +251,49 @@ func splitValues(s string, sep byte) []string {
 	}
 }
 
+// parseFieldTag() parses a struct field tag value.
+func parseFieldTag(s string) ([]string, error) {
+	s = strings.TrimSpace(s)
+	var vals []string
+	for len(s) != 0 {
+		i := 0
+		for i < len(s) {
+			if s[i] == '"' {
+				for i++; i < len(s); i++ {
+					if s[i] == '"' {
+						break
+					} else if s[i] == '\\' {
+						if i == (len(s) - 1) {
+							return nil, fmt.Errorf("invalid '\\' in field tag")
+						}
+						i++
+					}
+				}
+				if i == len(s) {
+					return nil, fmt.Errorf("invalid '\"' in field tag")
+				}
+			} else if s[i] == '\\' {
+				if i == (len(s) - 1) {
+					return nil, fmt.Errorf("invalid '\\' in field tag")
+				}
+				i++
+			} else if s[i] == ';' {
+				break
+			}
+			i++
+		}
+		vals = append(vals, s[:i])
+		if i < len(s) {
+			i++
+		}
+		s = s[i:]
+	}
+	for i, _ := range vals {
+		vals[i] = strings.TrimSpace(vals[i])
+	}
+	return vals, nil
+}
+
 //
 // Library management
 //
@@ -691,7 +734,8 @@ func newFieldInfo(id int, field *reflect.StructField) *FieldInfo {
 	if len(tag) == 0 {
 		tag = field.Tag.Get(oldTagKey)
 	}
-	info.tags = splitValues(tag, tagSep)
+	// TODO: error handling.
+	info.tags, _ = parseFieldTag(tag)
 	info.typ = field.Type
 	for {
 		if info.typ.Kind() == reflect.Ptr {
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index