forked from davemachado/public-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.go
More file actions
56 lines (51 loc) · 1.64 KB
/
Copy pathutil.go
File metadata and controls
56 lines (51 loc) · 1.64 KB
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
package main
import (
"encoding/json"
"os"
"strconv"
"strings"
)
// getList initializes an Entries struct filled from the public-apis project
func getList(jsonFile string) {
file, err := os.OpenFile(jsonFile, os.O_RDONLY, 0644)
if err != nil {
panic("failed to open file: " + err.Error())
}
err = json.NewDecoder(file).Decode(&apiList)
if err != nil {
panic("failed to decode JSON from file: " + err.Error())
}
file.Close()
}
// getCategories initializes a string slice containing
// all unique categories from a given slice of Entries
func parseCategories(entries []Entry) []string {
var cats []string
set := make(map[string]struct{})
for _, entry := range entries {
if _, exists := set[entry.Category]; !exists {
cats = append(cats, entry.Category)
set[entry.Category] = struct{}{}
}
}
return cats
}
// checkEntryMatches checks if the given entry matches the given request's parameters.
// it returns true if the entry matches, and returns false otherwise.
func checkEntryMatches(entry Entry, request *SearchRequest) bool {
if strings.Contains(strings.ToLower(entry.API), strings.ToLower(request.Title)) &&
strings.Contains(strings.ToLower(entry.Description), strings.ToLower(request.Description)) &&
strings.Contains(strings.ToLower(entry.Auth), strings.ToLower(request.Auth)) &&
strings.Contains(strings.ToLower(entry.Cors), strings.ToLower(request.Cors)) &&
strings.Contains(strings.ToLower(entry.Category), strings.ToLower(request.Category)) {
if request.HTTPS == "" {
return true
}
if value, err := strconv.ParseBool(request.HTTPS); err == nil {
if entry.HTTPS == value {
return true
}
}
}
return false
}