-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeta.go
More file actions
154 lines (148 loc) · 3.2 KB
/
Copy pathmeta.go
File metadata and controls
154 lines (148 loc) · 3.2 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
package errors
import (
"fmt"
"strconv"
"time"
"unsafe"
)
type Meta []struct {
Key string
Value string
}
func (meta Meta) Len() int {
return len(meta)
}
func (meta Meta) Less(i, j int) bool {
return meta[i].Key < meta[j].Key
}
func (meta Meta) Swap(i, j int) {
meta[i], meta[j] = meta[j], meta[i]
}
// WithMeta
// 设置元数据。
func WithMeta(key string, val any) Option {
return func(o *Options) {
if key == "" {
return
}
switch v := val.(type) {
case string:
o.Meta = append(o.Meta, struct {
Key string
Value string
}{Key: key, Value: v})
break
case int:
o.Meta = append(o.Meta, struct {
Key string
Value string
}{Key: key, Value: strconv.FormatInt(int64(v), 10)})
break
case int8:
o.Meta = append(o.Meta, struct {
Key string
Value string
}{Key: key, Value: strconv.FormatInt(int64(v), 10)})
break
case int16:
o.Meta = append(o.Meta, struct {
Key string
Value string
}{Key: key, Value: strconv.FormatInt(int64(v), 10)})
break
case int32:
o.Meta = append(o.Meta, struct {
Key string
Value string
}{Key: key, Value: strconv.FormatInt(int64(v), 10)})
break
case int64:
o.Meta = append(o.Meta, struct {
Key string
Value string
}{Key: key, Value: strconv.FormatInt(v, 10)})
break
case uint:
o.Meta = append(o.Meta, struct {
Key string
Value string
}{Key: key, Value: strconv.FormatUint(uint64(v), 10)})
break
case uint16:
o.Meta = append(o.Meta, struct {
Key string
Value string
}{Key: key, Value: strconv.FormatUint(uint64(v), 10)})
break
case uint32:
o.Meta = append(o.Meta, struct {
Key string
Value string
}{Key: key, Value: strconv.FormatUint(uint64(v), 10)})
break
case uint64:
o.Meta = append(o.Meta, struct {
Key string
Value string
}{Key: key, Value: strconv.FormatUint(v, 10)})
break
case float32:
o.Meta = append(o.Meta, struct {
Key string
Value string
}{Key: key, Value: strconv.FormatFloat(float64(v), 'f', -1, 32)})
break
case float64:
o.Meta = append(o.Meta, struct {
Key string
Value string
}{Key: key, Value: strconv.FormatFloat(v, 'f', -1, 64)})
break
case bool:
o.Meta = append(o.Meta, struct {
Key string
Value string
}{Key: key, Value: strconv.FormatBool(v)})
break
case byte:
o.Meta = append(o.Meta, struct {
Key string
Value string
}{Key: key, Value: string(v)})
break
case []byte:
if len(v) == 0 {
o.Meta = append(o.Meta, struct {
Key string
Value string
}{Key: key, Value: ""})
break
}
s := unsafe.String(&v[0], len(v))
o.Meta = append(o.Meta, struct {
Key string
Value string
}{Key: key, Value: s})
break
case time.Time:
o.Meta = append(o.Meta, struct {
Key string
Value string
}{Key: key, Value: v.Format(time.RFC3339)})
break
default:
if s, ok := v.(interface{ String() string }); ok {
o.Meta = append(o.Meta, struct {
Key string
Value string
}{Key: key, Value: s.String()})
break
}
o.Meta = append(o.Meta, struct {
Key string
Value string
}{Key: key, Value: fmt.Sprintf("%v", v)})
break
}
}
}