-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
108 lines (82 loc) · 1.83 KB
/
utils.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
package inaarea
import (
"encoding/json"
"fmt"
"path"
"runtime"
"strings"
"github.com/tidwall/gjson"
)
type Area string
const (
PROVINCE Area = "PROVINCE"
DISTRICT = "DISTRICT"
SUBDISTRICT = "SUBDISTRICT"
URBANVILLAGE = "URBANVILLAGE"
)
func getPath() (dirPath, fileName string) {
_, fileName, _, ok := runtime.Caller(0)
if !ok {
panic("No caller information")
}
dirPath = path.Dir(fileName)
return
}
func getAsset(area Area) string {
dir, _ := getPath()
file := "provinces.json"
switch area {
case DISTRICT:
file = "districts.json"
case SUBDISTRICT:
file = "subDistricts.json"
case URBANVILLAGE:
file = "urbanVillages.json"
}
return path.Join(dir, "assets", file)
}
func parse(obj gjson.Result) AreaModel {
model := AreaModel{}
_ = json.Unmarshal([]byte(obj.Raw), &model)
return model
}
func buildQuery(filter AreaModel) string {
queries := []string{}
if filter.Code != "" {
queries = append(queries, fmt.Sprintf(`#(code%s"*%s*")#`, "%", filter.Code))
}
if filter.Name != "" {
queries = append(queries, fmt.Sprintf(`#(name%s"*%s*")#`, "%", filter.Name))
}
if filter.Parent != "" {
queries = append(queries, fmt.Sprintf(`#(parent%s"*%s*")#`, "%", filter.Parent))
}
query := "*"
if len(queries) > 0 {
query = "data." + strings.Join(queries, "|")
}
return query
}
func buildFilterParent(parentCode string, filters []AreaModel) []AreaModel {
if len(filters) == 0 {
filters = append(filters, AreaModel{
Parent: parentCode,
})
} else {
for i, _ := range filters {
filters[i].Parent = parentCode
}
}
return filters
}
func unique(areas []AreaModel) []AreaModel {
keys := make(map[string]bool)
list := []AreaModel{}
for _, a := range areas {
if _, value := keys[a.Code]; !value {
keys[a.Code] = true
list = append(list, a)
}
}
return list
}