-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit.go
60 lines (52 loc) · 1.04 KB
/
unit.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
package dbunit
import (
"io/ioutil"
"os"
"reflect"
"gopkg.in/yaml.v2"
"github.com/goapt/dbunit/fixtures"
)
func PluckWithFixture(filePath string, key string) []interface{} {
var data = make([]map[string]interface{}, 0)
if !isExists(filePath) {
panic("file not exists:" + filePath)
}
d, _ := ioutil.ReadFile(filePath)
tpl := fixtures.NewTemplate()
d, err := tpl.Parse(d)
if err != nil {
panic(err)
}
err = yaml.Unmarshal(d, &data)
if err != nil {
panic(err)
}
return Pluck(data, key)
}
func Pluck(data []map[string]interface{}, key string) []interface{} {
s := make([]interface{}, len(data))
for k, v := range data {
s[k] = v[key]
}
return unique(s)
}
func unique(s []interface{}) []interface{} {
ns := make([]interface{}, 0)
for _, v := range s {
isDuplicate := false
for _, nv := range ns {
if reflect.DeepEqual(nv, v) {
isDuplicate = true
break
}
}
if !isDuplicate {
ns = append(ns, v)
}
}
return ns
}
func isExists(path string) bool {
_, err := os.Stat(path)
return !os.IsNotExist(err)
}