-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathslice.go
71 lines (61 loc) · 1.83 KB
/
slice.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
package gq
import (
"encoding/json"
"sort"
"github.com/pquerna/ffjson/ffjson"
"github.com/sirupsen/logrus"
)
// KV 타입은 Key-Value 데이터 형태의 struct 이며, K 속성은 Key(string), V속성은 Value(interface{}) 를 의미 합니다.
type KV struct {
K string `json:"k"`
V interface{} `json:"v"`
}
// KVSlice 타입은 "KV" 타입의 Slice 타입이며, "Map" 타입과 상호 전환하여 사용할 수 있습니다.
type KVSlice []*KV
// Clone 는 `KVSlice 타입` 을 복제하여 리턴합니다.
func (t KVSlice) Clone() KVSlice {
tmplist := make([]*KV, len(t))
copy(tmplist, t)
return KVSlice(tmplist)
}
// SortByValue 는 KVSlice 를 V값 기준으로 역정렬하여 리턴합니다.
func (t KVSlice) RSortByValue() KVSlice {
sort.Slice(t, func(i, j int) bool {
res := true
switch x, y := t[i].V, t[j].V; x.(type) {
case float64:
res = x.(float64) > y.(float64)
default:
res = x.(int) > y.(int)
}
return res
})
return t
}
// GetJSONPretty 는 `KVSlice 타입`에 정의된 KV 데이터셋을 JSON string 으로 보기좋게 리턴합니다
func (t KVSlice) GetJSONPretty() string {
res, e := json.MarshalIndent(t, "", "\t")
if e != nil {
logrus.Errorf("GetPretty Err : %s", e)
return ""
}
return string(res)
}
// GetJSONString 는 `KVSlice 타입`에 정의된 KV 데이터셋을 JSON 포멧(string) 으로 리턴합니다
func (t KVSlice) GetJSONString() string {
res := t.GetJSONByte()
if res != nil {
return string(res)
}
return ""
}
// TODO: {"k":"apple","v":38} 와 같이 처리 할지 {"apple":38} 와 같이 처리 할지
// GetJSONByte 는 `KVSlice 타입`에 정의된 KV 데이터셋을 JSON 포멧(byte) 으로 리턴합니다
func (t KVSlice) GetJSONByte() []byte {
res, e := ffjson.Marshal(t)
if e != nil {
logrus.Errorf("GetByte Err : %s", e)
return nil
}
return res
}