forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket_helper.go
88 lines (76 loc) · 1.63 KB
/
socket_helper.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package server
import (
"encoding/json"
"fmt"
"math"
"reflect"
"strings"
"time"
"github.com/evcc-io/evcc/util"
)
func encode(v interface{}) (string, error) {
var s string
switch val := v.(type) {
case time.Time:
if val.IsZero() {
s = "null"
} else {
s = fmt.Sprintf(`"%s"`, val.Format(time.RFC3339))
}
case time.Duration:
// must be before stringer to convert to seconds instead of string
s = fmt.Sprintf("%d", int64(val.Seconds()))
case float64:
if math.IsNaN(val) {
s = "null"
} else {
s = fmt.Sprintf("%.5g", val)
}
default:
if b, err := json.Marshal(v); err == nil {
s = string(b)
} else {
return "", err
}
}
return s, nil
}
func encodeSlice(v interface{}) (string, error) {
rv := reflect.ValueOf(v)
res := make([]string, rv.Len())
for i := 0; i < rv.Len(); i++ {
var err error
if res[i], err = encode(rv.Index(i).Interface()); err != nil {
return "", err
}
}
return fmt.Sprintf("[%s]", strings.Join(res, ",")), nil
}
func kv(p util.Param) string {
var (
val string
err error
)
// unwrap slices
if p.Val != nil && reflect.TypeOf(p.Val).Kind() == reflect.Slice {
val, err = encodeSlice(p.Val)
} else {
val, err = encode(p.Val)
}
if err != nil {
panic(err)
}
if p.Key == "" && val == "" {
log.ERROR.Printf("invalid key/val for %+v, please report to https://github.com/evcc-io/evcc/issues/6439", p)
return "\"foo\":\"bar\""
}
var msg strings.Builder
msg.WriteString("\"")
if p.Loadpoint != nil {
msg.WriteString(fmt.Sprintf("loadpoints.%d.", *p.Loadpoint))
}
msg.WriteString(p.Key)
msg.WriteString("\":")
msg.WriteString(val)
return msg.String()
}