-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.go
67 lines (57 loc) · 1.22 KB
/
utils.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
package dsmock
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"google.golang.org/appengine/datastore"
)
func ToString(value interface{}) string {
switch t := value.(type) {
case string:
return t
default:
return fmt.Sprint(value)
}
}
func ToFloat64(val interface{}) (float64, error) {
switch v := val.(type) {
case float64:
return v, nil
case float32, int, int32, int64:
if v, ok := v.(float64); ok {
return v, nil
} else {
return 0, fmt.Errorf("can not convert %v to float64", val)
}
case string:
return strconv.ParseFloat(v, 64)
}
return 0, fmt.Errorf("can not convert %v to float64", val)
}
func KeyToString(k *datastore.Key) string {
if k.Parent() == nil {
if k.IntID() != 0 {
return strconv.FormatInt(k.IntID(), 10)
} else {
return strconv.Quote(k.StringID())
}
}
keys := make([]string, 0)
for {
var v string
if k.IntID() != 0 {
v = strconv.FormatInt(k.IntID(), 10)
} else {
v = strconv.Quote(k.StringID())
}
keys = append([]string{strconv.Quote(k.Kind()), v}, keys...)
if k.Parent() == nil {
return "[" + strings.Join(keys, ",") + "]"
}
k = k.Parent()
}
}
func DecodeJSON(str string, value interface{}) error {
return json.Unmarshal([]byte(str), value)
}