/
set.go
61 lines (43 loc) · 1.07 KB
/
set.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
package gotk
import (
"fmt"
"strconv"
"strings"
"cyberpull.com/gotk/v2/errors"
"golang.org/x/exp/constraints"
)
type SetConstraint interface {
constraints.Integer | constraints.Float | ~string
}
func Join[T SetConstraint](entries []T, delim string) (value string, err error) {
for _, entry := range entries {
data := fmt.Sprint(entry)
value += delim + data
}
value = strings.TrimPrefix(value, delim)
return
}
func Split[T SetConstraint](data string, delim string) (value []T, err error) {
var t T
value = make([]T, 0)
for _, entry := range strings.Split(data, delim) {
var newValue any
switch any(t).(type) {
case string:
newValue = entry
case int, int8, int16, int32, int64:
newValue, err = strconv.ParseInt(entry, 0, 64)
case uint, uint8, uint16, uint32, uint64:
newValue, err = strconv.ParseUint(entry, 0, 64)
case float32, float64:
newValue, err = strconv.ParseFloat(entry, 64)
default:
err = errors.Newf("Type '%T' not allowed", t)
}
if err != nil {
return
}
value = append(value, any(newValue).(T))
}
return
}