Browse Source

text null

ls 4 years ago
parent
commit
b83386bc7a
1 changed files with 27 additions and 9 deletions
  1. 27 9
      types/string.go

+ 27 - 9
types/string.go

@@ -22,19 +22,37 @@ func (s TextNull) Value() (driver.Value, error) {
 // Scan implements the sql.Scanner interface,
 // and turns the bytes incoming from MySQL into a string
 func (s *TextNull) Scan(src interface{}) error {
-	if src != nil {
-		v, ok := src.([]byte)
-		if !ok {
-			return errors.New("bad []byte type assertion")
+	switch t := src.(type) {
+	case string:
+		*s = TextNull(src.(string))
+	case []byte:
+		if len(t) == 0 {
+			*s = TextNull("")
+		} else {
+			*s = TextNull(src.([]byte))
 		}
-
-		*s = TextNull(v)
-		return nil
+	case nil:
+		*s = TextNull("")
+	default:
+		return errors.New("Incompatible type for JSONText")
 	}
+	return nil
 
-	*s = TextNull("")
+	/*
+		if src != nil {
+			v, ok := src.([]byte)
+			if !ok {
+				return errors.New("bad []byte type assertion")
+			}
 
-	return nil
+			*s = TextNull(v)
+			return nil
+		}
+
+		*s = TextNull("")
+
+		return nil
+		//*/
 }
 
 var errNilPtr = errors.New("destination pointer is nil") // embedded in descriptive error