-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathdemo_docs.go
144 lines (133 loc) · 5.78 KB
/
demo_docs.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2020 Kuei-chun Chen. All rights reserved.
package examples
import (
"fmt"
"math/rand"
"strconv"
"strings"
"time"
"go.mongodb.org/mongo-driver/bson"
)
/*
* copied from keyhole to remove dependency
*/
// FavoritesSchema -
type FavoritesSchema struct {
ID string `json:"_id" bson:"_id"`
FavoriteBook string `json:"favoriteBook" bson:"favoriteBook"`
FavoriteCity string `json:"favoriteCity" bson:"favoriteCity"`
FavoriteMovie string `json:"favoriteMovie" bson:"favoriteMovie"`
FavoriteMusic string `json:"favoriteMusic" bson:"favoriteMusic"`
FavoriteSport string `json:"favoriteSport" bson:"favoriteSport"`
}
// FavoritesDoc -
type FavoritesDoc struct {
Sports []string
Music []string
Cities []string
Books []string
Movies []string
}
// GetDemoDoc returns a demo document
func getDemoDoc() bson.M {
var n = len(Favorites.Sports)
favoriteSports := []string{Favorites.Sports[rand.Intn(n)], Favorites.Sports[rand.Intn(n)], Favorites.Sports[rand.Intn(n)]}
favoriteSports = unique(append(favoriteSports, Favorites.Sports[0:3]...), 3)
n = len(Favorites.Music)
favoriteMusic := []string{Favorites.Music[rand.Intn(n)], Favorites.Music[rand.Intn(n)], Favorites.Music[rand.Intn(n)]}
favoriteMusic = unique(append(favoriteMusic, Favorites.Music[0:3]...), 3)
n = len(Favorites.Cities)
favoriteCities := []string{Favorites.Cities[rand.Intn(n)], Favorites.Cities[rand.Intn(n)], Favorites.Cities[rand.Intn(n)]}
favoriteCities = unique(append(favoriteCities, Favorites.Cities[0:3]...), 3)
n = len(Favorites.Books)
x := rand.Intn(n)
favoriteBooks := []string{Favorites.Books[x], Favorites.Books[rand.Intn(n)], Favorites.Books[rand.Intn(n)]}
favoriteBooks = unique(append(favoriteBooks, Favorites.Books[0:3]...), 3)
n = len(Favorites.Movies)
favoriteMovies := []string{Favorites.Movies[rand.Intn(n)], Favorites.Movies[rand.Intn(n)], Favorites.Movies[rand.Intn(n)]}
favoriteMovies = unique(append(favoriteMovies, Favorites.Movies[0:3]...), 3)
favoritesList := []bson.M{
bson.M{"sport": favoriteSports[0], "music": favoriteMusic[0], "city": favoriteCities[0], "book": favoriteBooks[0], "movie": favoriteMovies[0]},
bson.M{"sport": favoriteSports[1], "music": favoriteMusic[1], "city": favoriteCities[1], "book": favoriteBooks[1], "movie": favoriteMovies[1]},
bson.M{"sport": favoriteSports[2], "music": favoriteMusic[2], "city": favoriteCities[2], "book": favoriteBooks[2], "movie": favoriteMovies[2]},
}
favoritesKVList := []bson.M{}
for i := 0; i < 3; i++ {
favoritesKVList = append(favoritesKVList,
bson.M{
"level": int32(i + 1),
"categories": []bson.M{
bson.M{"key": "sport", "value": favoriteSports[i]},
bson.M{"key": "music", "value": favoriteMusic[i]},
bson.M{"key": "city", "value": favoriteCities[i]},
bson.M{"key": "book", "value": favoriteBooks[i]},
bson.M{"key": "movie", "value": favoriteMovies[i]},
}})
}
favoritesKVSet := []bson.M{}
favoritesKVSet = append(favoritesKVSet, bson.M{"key": "sport", "value": favoriteSports})
favoritesKVSet = append(favoritesKVSet, bson.M{"key": "music", "value": favoriteMusic})
favoritesKVSet = append(favoritesKVSet, bson.M{"key": "city", "value": favoriteCities})
favoritesKVSet = append(favoritesKVSet, bson.M{"key": "book", "value": favoriteBooks})
favoritesKVSet = append(favoritesKVSet, bson.M{"key": "movie", "value": favoriteMovies})
email := getEmailAddress()
s := strings.Split(strings.Split(email, "@")[0], ".")
doc := bson.M{
"_search": strconv.FormatInt(rand.Int63(), 16),
"email": email,
"firstName": s[0],
"lastName": s[2],
"favoritesAll": bson.M{
"sports": favoriteSports,
"musics": favoriteMusic,
"cities": favoriteCities,
"books": favoriteBooks,
"movies": favoriteMovies,
},
"favoritesList": favoritesList,
"favoritesKVList": favoritesKVList,
"favoritesKVSet": favoritesKVSet,
"favoriteBookId": 1100 + x,
"favoriteBook": favoriteBooks[0],
"favoriteBooks": favoriteBooks,
"favoriteCity": favoriteCities[0],
"favoriteCities": favoriteCities,
"favoriteMovie": favoriteMovies[0],
"favoriteMovies": favoriteMovies,
"favoriteMusic": favoriteMusic[0],
"favoriteMusics": favoriteMusic,
"favoriteSport": favoriteSports[0],
"favoriteSports": favoriteSports,
"number": rand.Intn(1000),
"numbers": []int{rand.Intn(1000), rand.Intn(1000), rand.Intn(1000), rand.Intn(1000), rand.Intn(1000)},
"ts": time.Now(),
}
return doc
}
func unique(s []string, n int) []string {
arr := make(map[string]bool, len(s))
us := make([]string, len(arr))
for _, elem := range s {
if len(elem) != 0 {
if !arr[elem] {
us = append(us, elem)
arr[elem] = true
}
}
}
return us[:n]
}
// GetEmailAddress exposes getEmailAddress()
func getEmailAddress() string {
return fmt.Sprintf("%s.%s.%s@%s",
fnames[rand.Intn(len(fnames)-1)], string(fnames[rand.Intn(len(fnames)-1)][0]),
lnames[rand.Intn(len(lnames)-1)], domains[rand.Intn(len(domains)-1)])
}
// Favorites constance
var Favorites = FavoritesDoc{
Sports: []string{"Baseball", "Boxing", "Dodgeball", "Figure skating", "Football", "Horse racing", "Mountaineering", "Skateboard", "Ski", "Soccer"},
Music: []string{"Blues", "Classical", "Country", "Easy Listening", "Electronic", "Hip Pop", "Jazz", "Opera", "Soul", "Rock"},
Cities: []string{"Atlanta", "Bangkok", "Beijing", "London", "Paris", "Singapore", "New York", "Istanbul", "Dubai", "Taipei"},
Books: []string{"Journey to the West", "The Lord of the Rings", "In Search of Lost Time", "Don Quixote", "Ulysses", "The Great Gatsby", "Moby Dick", "Hamlet", "War and Peace", "The Odyssey"},
Movies: []string{"The Godfather", "The Shawshank Redemption", "Schindler's List", "Raging Bull", "Casablanca", "Citizen Kane", "Gone with the Wind", "The Wizard of Oz", "One Flew Over the Cuckoo's Nest", "Lawrence of Arabia"},
}