-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.go
55 lines (45 loc) · 1.11 KB
/
models.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"encoding/json"
"fmt"
"strings"
)
type User struct {
ID int `json:"id"`
Email string `json:"email"`
Name string `json:"name"`
}
type Users []User
func (u Users) Refine(term string) (result Users) {
emailMatches, otherMatches := Users{}, Users{}
termL := strings.ToLower(term)
for _, user := range u {
nameL := strings.ToLower(user.Name)
for _, namePart := range strings.Split(nameL, " ") {
if namePart == termL {
result = append(result, user)
break
}
}
// Prevent a user with a name match from being added again
if len(result) > 0 && result[len(result)-1].ID == user.ID {
continue
}
if strings.Contains(user.Email, termL) {
emailMatches = append(emailMatches, user)
}
if strings.Contains(nameL, termL) {
otherMatches = append(otherMatches, user)
}
}
result = append(result, emailMatches...)
result = append(result, otherMatches...)
return result
}
func (u Users) JSON() (string, error) {
json, err := json.Marshal(u)
if err != nil {
return "", fmt.Errorf("Users.JSON(): error marshaling JSON: %v", err)
}
return string(json), nil
}