This repository has been archived by the owner on Jul 19, 2021. It is now read-only.
forked from paulmach/orb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
properties.go
112 lines (90 loc) · 2.81 KB
/
properties.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package geojson
import "fmt"
// Properties defines the feature properties with some helper methods.
type Properties map[string]interface{}
// MustBool guarantees the return of a `bool` (with optional default).
// This function useful when you explicitly want a `bool` in a single
// value return context, for example:
// myFunc(f.Properties.MustBool("param1"), f.Properties.MustBool("optional_param", true))
// This function will panic if the value is present but not a bool.
func (p Properties) MustBool(key string, def ...bool) bool {
v := p[key]
if b, ok := v.(bool); ok {
return b
}
if v != nil {
panic(fmt.Sprintf("not a bool, but a %T: %v", v, v))
}
if len(def) > 0 {
return def[0]
}
panic("property not found")
}
// MustInt guarantees the return of an `int` (with optional default).
// This function useful when you explicitly want a `int` in a single
// value return context, for example:
// myFunc(f.Properties.MustInt("param1"), f.Properties.MustInt("optional_param", 123))
// This function will panic if the value is present but not a number.
func (p Properties) MustInt(key string, def ...int) int {
v := p[key]
if i, ok := v.(int); ok {
return i
}
if f, ok := v.(float64); ok {
return int(f)
}
if v != nil {
panic(fmt.Sprintf("not a number, but a %T: %v", v, v))
}
if len(def) > 0 {
return def[0]
}
panic("property not found")
}
// MustFloat64 guarantees the return of a `float64` (with optional default)
// This function useful when you explicitly want a `float64` in a single
// value return context, for example:
// myFunc(f.Properties.MustFloat64("param1"), f.Properties.MustFloat64("optional_param", 10.1))
// This function will panic if the value is present but not a number.
func (p Properties) MustFloat64(key string, def ...float64) float64 {
v := p[key]
if f, ok := v.(float64); ok {
return f
}
if i, ok := v.(int); ok {
return float64(i)
}
if v != nil {
panic(fmt.Sprintf("not a number, but a %T: %v", v, v))
}
if len(def) > 0 {
return def[0]
}
panic("property not found")
}
// MustString guarantees the return of a `string` (with optional default)
// This function useful when you explicitly want a `string` in a single
// value return context, for example:
// myFunc(f.Properties.MustString("param1"), f.Properties.MustString("optional_param", "default"))
// This function will panic if the value is present but not a string.
func (p Properties) MustString(key string, def ...string) string {
v := p[key]
if s, ok := v.(string); ok {
return s
}
if v != nil {
panic(fmt.Sprintf("not a string, but a %T: %v", v, v))
}
if len(def) > 0 {
return def[0]
}
panic("property not found")
}
// Clone returns a shallow copy of the properties.
func (p Properties) Clone() Properties {
n := make(Properties, len(p))
for k, v := range p {
n[k] = v
}
return n
}