-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
180 lines (155 loc) · 3.69 KB
/
model.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
* snowmark - HTML templates for Go.
*
* MIT License.
* Copyright (c) 2022, Sandeep Gupta.
* https://github.com/sangupta/snowmark
*
* Use of this source code is governed by a MIT style license
* that can be found in LICENSE file in the code repository:
*/
package snowmark
import "github.com/sangupta/berry"
//
// Represents a model of values
// that are used to merge with a page or a fragment.
//
type Model struct {
_map map[string]interface{}
}
//
// Create new model instance.
//
func NewModel() *Model {
return &Model{
_map: make(map[string]interface{}),
}
}
//
// Return the size of the model.
//
func (model *Model) Size() int {
return len(model._map)
}
//
// Check if model is empty or not. Returns `true` if
// there are no keys in model, `false` otherwise.
//
func (model *Model) IsEmpty() bool {
return model.Size() == 0
}
//
// Clear model and remove all keys
//
func (model *Model) Clear() {
model._map = make(map[string]interface{})
}
//
// Return the map associated with this model.
//
func (model *Model) GetMap() map[string]interface{} {
return model._map
}
//
// Get the value from the model.
//
func (model *Model) Get(key string) (interface{}, bool) {
value, exists := model._map[key]
return value, exists
}
//
// Get a `string` value for a key from the model, or the default value if
// the key does not exist, or is not a `string`.
//
func (model *Model) GetString(key string, defaultValue string) string {
value, exists := model._map[key]
if !exists {
return defaultValue
}
return berry.ConvertToString(value)
}
//
// Get a `bool` value for a key from the model, or the default value if
// the key does not exist, or is not a `bool`.
//
func (model *Model) GetBool(key string, defaultValue bool) bool {
value, exists := model._map[key]
if !exists {
return defaultValue
}
b, _ := berry.ConvertToBool(value)
return b
}
//
// Get a `uint64` value for a key from the model, or the default value if
// the key does not exist, or is not a `uint64`.
//
func (model *Model) GetUInt64(key string, defaultValue uint64) uint64 {
value, exists := model._map[key]
if !exists {
return defaultValue
}
b, _ := berry.ConvertToUint64(value, defaultValue)
return b
}
//
// Get a `int64` value for a key from the model, or the default value if
// the key does not exist, or is not a `int64`.
//
func (model *Model) GetInt64(key string, defaultValue int64) int64 {
value, exists := model._map[key]
if !exists {
return defaultValue
}
b, _ := berry.ConvertToInt64(value, defaultValue)
return b
}
//
// Get a `float64` value for a key from the model, or the default value if
// the key does not exist, or is not a `float64`.
//
func (model *Model) GetFloat64(key string, defaultValue float64) float64 {
value, exists := model._map[key]
if !exists {
return defaultValue
}
b, _ := berry.ConvertToFloat64(value, defaultValue)
return b
}
//
// Put the value in model against given key.
//
func (model *Model) Put(key string, value interface{}) {
model._map[key] = value
}
//
// Put the value in model if the key does not exist. Returns
// `true` if value was added to model, `false` otherwise.
//
func (model *Model) PutIfNotExists(key string, value interface{}) bool {
_, exists := model._map[key]
if exists {
return false
}
model._map[key] = value
return true
}
//
// Replace the current key with new value. If no key exists
// the function returns `false`. Returns `true` if value was
// replaced.
//
func (model *Model) Replace(key string, value interface{}) bool {
_, exists := model._map[key]
if !exists {
return false
}
model._map[key] = value
return true
}
//
// Remove the key from the model, if it exists.
//
func (model *Model) Remove(key string) {
delete(model._map, key)
}