-
Notifications
You must be signed in to change notification settings - Fork 2
/
encoding.go
64 lines (53 loc) · 1.45 KB
/
encoding.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
package hash
import (
"encoding/binary"
"fmt"
"math"
"reflect"
"sigs.k8s.io/yaml"
)
type encoder func(v any) ([]byte, error)
func boolEncoder(v any) ([]byte, error) {
if reflect.ValueOf(v).Bool() {
return []byte{1}, nil
}
return []byte{0}, nil
}
func intEncoder(v any) ([]byte, error) {
vt := reflect.ValueOf(v)
return binary.AppendVarint([]byte{}, vt.Int()), nil
}
func uintEncoder(v any) ([]byte, error) {
vt := reflect.ValueOf(v)
return binary.AppendUvarint([]byte{}, vt.Uint()), nil
}
func floatEncoder(v any) ([]byte, error) {
vt := reflect.ValueOf(v)
return uintEncoder(math.Float64bits(vt.Float()))
}
func stringEncoder(v any) ([]byte, error) {
vt := reflect.ValueOf(v)
return []byte(vt.String()), nil
}
func structEncoder(v any) ([]byte, error) {
b, err := yaml.Marshal(v)
return b, err
}
func getEncoder(v any) (encoder, error) {
t := reflect.Indirect(reflect.ValueOf(v))
switch t.Kind() {
case reflect.Bool:
return boolEncoder, nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return intEncoder, nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return uintEncoder, nil
case reflect.Float32, reflect.Float64:
return floatEncoder, nil
case reflect.String:
return stringEncoder, nil
case reflect.Struct, reflect.Map, reflect.Slice, reflect.Array:
return structEncoder, nil
}
return nil, fmt.Errorf("encoder for type %s not supported", t.Kind().String())
}