-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyntax.go
More file actions
162 lines (146 loc) · 3.17 KB
/
Copy pathsyntax.go
File metadata and controls
162 lines (146 loc) · 3.17 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package eutil
import (
"encoding/json"
"unsafe"
)
func If[T any](cond bool, t T, f T) T {
if cond {
return t
}
return f
}
// Or iterates over the given functions and compares the return value to a zero
// value, returning it as soon as it gets a nonzero value. It is a parody of the
// OR operator in Python.
//
// You can get the basic usage on example_test file.
// In additional, there is another solution:
//
// type oer struct {
// or *oer
// val string
// }
//
// func cmdFlag() *oer {
// var configPath string
// flag.StringVar(&configPath, "c", "", "")
// flag.Parse()
// if configPath != "" {
// // yes
// }
// return &oer{val: configPath}
// }
//
// func (o *oer) env() *oer {
// if o.val != "" {
// return o
// }
// configEnv := os.Getenv("MY_CONFIG")
// if configEnv != "" {
// // yes
// }
// o.val = configEnv
// return o
// }
//
// func (o *oer) _default() string {
// if o.val != "" {
// return o.val
// }
// return "default value"
// }
//
// func foo() {
// configPath := cmdFlag().or.env().or._default()
// // use configPath
// }
//
// Deprecated: Use cmp.Or instead.
func Or[T comparable](args ...T) (res T) {
var zero = res
for _, v := range args {
if v != zero {
return v
}
}
return zero
}
// OrUnwished like the function Or but can indicate the unwished value. It will
// traverses through the given function and compares to the unwished value,
// returning it as soon as it gets the value is not equal non-unwished value
// and not equal zero value. However, it will return a zero value when every
// function return the unwished value.
func OrUnwish[T comparable](unwish T, args ...T) (res T) {
var zero = res
for _, v := range args {
if v != unwish && v != zero {
return v
}
}
return zero
}
// In checks if the first argument is in the rest of the arguments.
// It returns true if the first argument is in the rest of the arguments.
func In[T comparable](ori T, args ...T) bool {
for _, arg := range args {
if arg == ori {
return true
}
}
return false
}
// B2S means slice of byte to string
func B2S(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
// S2B means string to byte slice
func S2B(s string) []byte {
return unsafe.Slice(unsafe.StringData(s), len(s))
}
func DeepCopy[T any](src T, dst *T) error {
b, err := json.Marshal(src)
if err != nil {
return err
}
if err := json.Unmarshal(b, dst); err != nil {
return err
}
return nil
}
type applyFunc[T any] func(T) T
func Apply[T any](fn applyFunc[T], args ...T) []T {
for i, v := range args {
args[i] = fn(v)
}
return args
}
type mapFunc[T, V any] func(T) V
func Map[T, V any](fn mapFunc[T, V], args ...T) []V {
res := make([]V, 0, len(args))
for _, v := range args {
res = append(res, fn(v))
}
return res
}
type reduceFunc[T any] func(T, T) T
func Reduce[T any](fn reduceFunc[T], args ...T) (res T) {
if len(args) == 0 {
return
}
res = args[0]
l := len(args)
for i := 1; i < l; i++ {
res = fn(res, args[i])
}
return res
}
type filterFunc[T any] func(T) bool
func Filter[T any](fn filterFunc[T], args ...T) []T {
res := make([]T, 0, len(args))
for _, v := range args {
if ok := fn(v); ok {
res = append(res, v)
}
}
return res
}