forked from paulmach/go.geojson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproperties.go
127 lines (103 loc) · 3.17 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package geojson
import (
"fmt"
)
// SetProperty provides the inverse of all the property functions
// and is here for consistency.
func (f *Feature) SetProperty(key string, value interface{}) {
if f.Properties == nil {
f.Properties = make(map[string]interface{})
}
f.Properties[key] = value
}
// PropertyBool type asserts a property to `bool`.
func (f *Feature) PropertyBool(key string) (bool, error) {
if b, ok := (f.Properties[key]).(bool); ok {
return b, nil
}
return false, fmt.Errorf("type assertion of `%s` to bool failed", key)
}
// PropertyInt type asserts a property to `int`.
func (f *Feature) PropertyInt(key string) (int, error) {
if i, ok := (f.Properties[key]).(int); ok {
return i, nil
}
if i, ok := (f.Properties[key]).(float64); ok {
return int(i), nil
}
return 0, fmt.Errorf("type assertion of `%s` to int failed", key)
}
// PropertyFloat64 type asserts a property to `float64`.
func (f *Feature) PropertyFloat64(key string) (float64, error) {
if i, ok := (f.Properties[key]).(float64); ok {
return i, nil
}
return 0, fmt.Errorf("type assertion of `%s` to float64 failed", key)
}
// PropertyString type asserts a property to `string`.
func (f *Feature) PropertyString(key string) (string, error) {
if s, ok := (f.Properties[key]).(string); ok {
return s, nil
}
return "", fmt.Errorf("type assertion of `%s` to string failed", key)
}
// PropertyMustBool guarantees the return of a `bool` (with optional default)
//
// useful when you explicitly want a `bool` in a single value return context:
// myFunc(f.PropertyMustBool("param1"), f.PropertyMustBool("optional_param", true))
func (f *Feature) PropertyMustBool(key string, def ...bool) bool {
var defaul bool
b, err := f.PropertyBool(key)
if err == nil {
return b
}
if len(def) > 0 {
defaul = def[0]
}
return defaul
}
// PropertyMustInt guarantees the return of a `int` (with optional default)
//
// useful when you explicitly want a `int` in a single value return context:
// myFunc(f.PropertyMustInt("param1"), f.PropertyMustInt("optional_param", 123))
func (f *Feature) PropertyMustInt(key string, def ...int) int {
var defaul int
b, err := f.PropertyInt(key)
if err == nil {
return b
}
if len(def) > 0 {
defaul = def[0]
}
return defaul
}
// PropertyMustFloat64 guarantees the return of a `float64` (with optional default)
//
// useful when you explicitly want a `float64` in a single value return context:
// myFunc(f.PropertyMustFloat64("param1"), f.PropertyMustFloat64("optional_param", 10.1))
func (f *Feature) PropertyMustFloat64(key string, def ...float64) float64 {
var defaul float64
b, err := f.PropertyFloat64(key)
if err == nil {
return b
}
if len(def) > 0 {
defaul = def[0]
}
return defaul
}
// PropertyMustString guarantees the return of a `string` (with optional default)
//
// useful when you explicitly want a `string` in a single value return context:
// myFunc(f.PropertyMustString("param1"), f.PropertyMustString("optional_param", "default"))
func (f *Feature) PropertyMustString(key string, def ...string) string {
var defaul string
b, err := f.PropertyString(key)
if err == nil {
return b
}
if len(def) > 0 {
defaul = def[0]
}
return defaul
}