Browse Source

refactor notificationsForUser since xorm In support slice of customerize type (#956)

Lunny Xiao 2 years ago
parent
commit
46320f9630
3 changed files with 27 additions and 14 deletions
  1. 4 7
      models/notification.go
  2. 20 4
      vendor/github.com/go-xorm/builder/cond_in.go
  3. 3 3
      vendor/vendor.json

+ 4 - 7
models/notification.go

@@ -189,23 +189,20 @@ func NotificationsForUser(user *User, statuses []NotificationStatus, page, perPa
189 189
 	return notificationsForUser(x, user, statuses, page, perPage)
190 190
 }
191 191
 func notificationsForUser(e Engine, user *User, statuses []NotificationStatus, page, perPage int) (notifications []*Notification, err error) {
192
-	// FIXME: Xorm does not support aliases types (like NotificationStatus) on In() method
193
-	s := make([]uint8, len(statuses))
194
-	for i, status := range statuses {
195
-		s[i] = uint8(status)
192
+	if len(statuses) == 0 {
193
+		return
196 194
 	}
197 195
 
198 196
 	sess := e.
199 197
 		Where("user_id = ?", user.ID).
200
-		In("status", s).
198
+		In("status", statuses).
201 199
 		OrderBy("updated_unix DESC")
202 200
 
203 201
 	if page > 0 && perPage > 0 {
204 202
 		sess.Limit(perPage, (page-1)*perPage)
205 203
 	}
206 204
 
207
-	err = sess.
208
-		Find(&notifications)
205
+	err = sess.Find(&notifications)
209 206
 	return
210 207
 }
211 208
 

+ 20 - 4
vendor/github.com/go-xorm/builder/cond_in.go

@@ -6,6 +6,7 @@ package builder
6 6
 
7 7
 import (
8 8
 	"fmt"
9
+	"reflect"
9 10
 	"strings"
10 11
 )
11 12
 
@@ -195,11 +196,26 @@ func (condIn condIn) WriteTo(w Writer) error {
195 196
 		if len(condIn.vals) <= 0 {
196 197
 			return ErrNoInConditions
197 198
 		}
198
-		questionMark := strings.Repeat("?,", len(condIn.vals))
199
-		if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
200
-			return err
199
+
200
+		v := reflect.ValueOf(condIn.vals[0])
201
+		if v.Kind() == reflect.Slice {
202
+			l := v.Len()
203
+
204
+			questionMark := strings.Repeat("?,", l)
205
+			if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
206
+				return err
207
+			}
208
+
209
+			for i := 0; i < l; i++ {
210
+				w.Append(v.Index(i).Interface())
211
+			}
212
+		} else {
213
+			questionMark := strings.Repeat("?,", len(condIn.vals))
214
+			if _, err := fmt.Fprintf(w, "%s IN (%s)", condIn.col, questionMark[:len(questionMark)-1]); err != nil {
215
+				return err
216
+			}
217
+			w.Append(condIn.vals...)
201 218
 		}
202
-		w.Append(condIn.vals...)
203 219
 	}
204 220
 	return nil
205 221
 }

+ 3 - 3
vendor/vendor.json

@@ -437,10 +437,10 @@
437 437
 			"revisionTime": "2016-11-01T11:13:14Z"
438 438
 		},
439 439
 		{
440
-			"checksumSHA1": "sDRC697KuCbpI+1i2VPhyqgAvjs=",
440
+			"checksumSHA1": "Fh6Svimt+QyXHbaVxgSV7qwUHL8=",
441 441
 			"path": "github.com/go-xorm/builder",
442
-			"revision": "db75972580de4a7c6c20fff5b16a924c3de3fa12",
443
-			"revisionTime": "2016-12-14T02:05:24Z"
442
+			"revision": "9c357861b643b7dd1023551fdf116b8d42030146",
443
+			"revisionTime": "2017-02-16T03:03:40Z"
444 444
 		},
445 445
 		{
446 446
 			"checksumSHA1": "OCcksAYN5m0kc5yJF/Ba4VVHqeA=",