-
Notifications
You must be signed in to change notification settings - Fork 6
/
slice.go
60 lines (48 loc) · 1.12 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
package gox
import (
"encoding/json"
"encoding/xml"
"github.com/goexl/gox/internal/constant"
)
// Slice 切片,既可以兼容单个值也可以兼容数组
type Slice[T any] []T
// NewSlice 快速创建数组
func NewSlice[T any](items ...T) Slice[T] {
return items
}
func (s *Slice[T]) Length() int {
return len(*s)
}
func (s *Slice[T]) Capacity() int {
return cap(*s)
}
func (s *Slice[T]) Clone() (t Slice[T]) {
t = make(Slice[T], len(*s))
copy(t, *s)
return
}
func (s *Slice[T]) UnmarshalJSON(bytes []byte) (err error) {
t := new(T)
ts := make([]T, 0)
start := bytes[0]
if constant.JsonArrayStart == start {
err = json.Unmarshal(bytes, &ts)
} else if constant.JsonObjectStart == start {
err = json.Unmarshal(bytes, t)
}
if constant.JsonObjectStart == start && nil == err {
*s = []T{*t}
} else if constant.JsonArrayStart == start && nil == err {
*s = ts
}
return
}
func (s *Slice[T]) UnmarshalXML(decoder *xml.Decoder, start xml.StartElement) (err error) {
t := new(T)
if ue := decoder.DecodeElement(t, &start); nil != ue {
err = decoder.DecodeElement(s, &start)
} else {
*s = []T{*t}
}
return
}