func TestValidSlice(t *testing.T) { letters := []string{"a", "b", "c", "d"} letters = utils.ReverseSlice(letters).([]string) if letters[len(letters)-1] != "a" { t.Errorf("Last letter should be 'a' but got: %v", letters[len(letters)-1]) } if utils.InSlice("c", letters) == false { t.Errorf("Letter c is present in letters slice") } var horse, nope Amazing horse.Animal = "weebl" nope.Animal = "nerdz" complexData := []ComplexData{ {One: 1, Two: "lol", Horse: horse}, {One: 2, Two: "asd", Horse: nope}} complexData = utils.ReverseSlice(complexData).([]ComplexData) if complexData[0].Horse.Animal != "nerdz" { t.Errorf("Animal should be nerdz, but got: %v", complexData[0].Horse.Animal) } if utils.InSlice(complexData[1], complexData) == false { t.Errorf("This value is present in complexData slice") } if utils.InSlice("banana", complexData) { t.Errorf("Banana is not into complexData slice (and have different type)") } }
// Comments returns the full comments list, or the selected range of comments // Comments() returns the full comments list // Comments(N) returns at most the last N comments // Comments(N, X) returns at most N comments, before the last comment + X func (post *UserPost) Comments(interval ...uint) interface{} { var comments []UserPostComment switch len(interval) { default: //full list case 0: db.Find(&comments, &UserPostComment{Hpid: post.Hpid}) case 1: // Get last interval[0] comments [ LIMIT interval[0] ] db.Order("hcid DESC").Limit(interval[0]).Find(&comments, &UserPostComment{Hpid: post.Hpid}) comments = utils.ReverseSlice(comments).([]UserPostComment) case 2: // Get last interval[0] comments, starting from interval[1] [ LIMIT interval[0] OFFSET interval[1] ] db.Order("hcid DESC").Limit(interval[0]).Offset(interval[1]).Find(&comments, &UserPostComment{Hpid: post.Hpid}) comments = utils.ReverseSlice(comments).([]UserPostComment) } return comments }
// Comments returns the full comments list, or the selected range of comments // Comments(options) returns the comment list, using selected options func (post *UserPost) Comments(options CommentlistOptions) *[]ExistingComment { var comments []UserPostComment query := Db().Where(&UserPostComment{Hpid: post.ID()}) query = commentlistQueryBuilder(query, options) query.Scan(&comments) comments = utils.ReverseSlice(comments).([]UserPostComment) var ret []ExistingComment for _, c := range comments { comment := c ret = append(ret, ExistingComment(&comment)) } return &ret }