forked from dasfaha/sky
/
property_file.go
309 lines (261 loc) · 7.32 KB
/
property_file.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package skyd
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"sort"
)
//------------------------------------------------------------------------------
//
// Typedefs
//
//------------------------------------------------------------------------------
// A PropertyFile manages the serialization of Property objects for a table.
type PropertyFile struct {
opened bool
path string
properties map[int64]*Property
propertiesByName map[string]*Property
}
//------------------------------------------------------------------------------
//
// Constructors
//
//------------------------------------------------------------------------------
// NewProperty returns a new PropertyFile.
func NewPropertyFile(path string) *PropertyFile {
p := &PropertyFile{
path: path,
}
p.Reset()
return p
}
//------------------------------------------------------------------------------
//
// Accessors
//
//------------------------------------------------------------------------------
// The path to the property file on disk.
func (p *PropertyFile) Path() string {
return p.path
}
// The path to the factors database.
func (p *PropertyFile) DbPath() string {
if p.path != "" {
return fmt.Sprintf("%v.factors", p.path)
}
return ""
}
//------------------------------------------------------------------------------
//
// Methods
//
//------------------------------------------------------------------------------
//--------------------------------------
// Property Management
//--------------------------------------
// Adds a new property to the property file and generate an identifier for it.
func (p *PropertyFile) CreateProperty(name string, transient bool, dataType string) (*Property, error) {
// Don't allow duplicate names.
if p.propertiesByName[name] != nil {
return nil, errors.New("Property already exists.")
}
property, err := NewProperty(0, name, transient, dataType)
if err != nil {
return nil, err
}
// Find the next object/action identifier.
if property.Transient {
_, property.Id = p.NextIdentifiers()
} else {
property.Id, _ = p.NextIdentifiers()
}
// Add to the list.
p.properties[property.Id] = property
p.propertiesByName[property.Name] = property
return property, nil
}
// Retrieves a list of undeleted properties sorted by id.
func (p *PropertyFile) GetProperties() []*Property {
list := make([]*Property, 0)
for _, property := range p.propertiesByName {
list = append(list, property)
}
sort.Sort(PropertyList(list))
return list
}
// Retrieves a list of all properties sorted by id.
func (p *PropertyFile) GetAllProperties() []*Property {
list := make([]*Property, 0)
for _, property := range p.properties {
list = append(list, property)
}
sort.Sort(PropertyList(list))
return list
}
// Retrieves a single property by id.
func (p *PropertyFile) GetProperty(id int64) *Property {
return p.properties[id]
}
// Retrieves a single property by name.
func (p *PropertyFile) GetPropertyByName(name string) *Property {
return p.propertiesByName[name]
}
// Deletes a property.
func (p *PropertyFile) DeleteProperty(property *Property) {
if property != nil && property.Name != "" {
delete(p.properties, property.Id)
delete(p.propertiesByName, property.Name)
}
}
// Clears out the property file.
func (p *PropertyFile) Reset() {
p.properties = make(map[int64]*Property)
p.propertiesByName = make(map[string]*Property)
}
//--------------------------------------
// Encoding
//--------------------------------------
// Encodes a property file.
func (p *PropertyFile) Encode(writer io.Writer) error {
// Convert the lookup into a sorted slice.
list := p.GetAllProperties()
// Encode the slice.
encoder := json.NewEncoder(writer)
err := encoder.Encode(list)
return err
}
// Decodes a property file.
func (p *PropertyFile) Decode(reader io.Reader) error {
list := make([]*Property, 0)
decoder := json.NewDecoder(reader)
err := decoder.Decode(&list)
if err != nil {
return err
}
// Create lookups for the properties.
p.Reset()
for _, property := range list {
p.properties[property.Id] = property
if property.Name != "" {
p.propertiesByName[property.Name] = property
}
}
return nil
}
//--------------------------------------
// State
//--------------------------------------
// Opens the property file.
func (p *PropertyFile) Open() error {
if p.IsOpen() {
return errors.New("skyd.PropertyFile: Property file is already open.")
}
// Ignore if there is no file.
if _, err := os.Stat(p.path); os.IsNotExist(err) {
return nil
}
// Otherwise open it.
file, err := os.Open(p.path)
if err != nil {
return err
}
defer file.Close()
// Then decode it.
err = p.Decode(bufio.NewReader(file))
if err != nil {
return err
}
p.opened = true
return nil
}
// Closes the property file.
func (p *PropertyFile) Close() {
p.Reset()
p.opened = false
}
// Returns whether the property file is currently open.
func (p *PropertyFile) IsOpen() bool {
return p.opened
}
//--------------------------------------
// Persistence
//--------------------------------------
// Saves the property file to disk.
func (p *PropertyFile) Save() error {
// Open the file for writing.
file, err := os.Create(p.path)
if err != nil {
return err
}
defer file.Close()
// Then decode it.
w := bufio.NewWriter(file)
err = p.Encode(w)
if err != nil {
return err
}
if err = w.Flush(); err != nil {
return err
}
return nil
}
//--------------------------------------
// Normalization
//--------------------------------------
// Converts a map with string keys to use property identifier keys.
func (p *PropertyFile) NormalizeMap(m map[string]interface{}) (map[int64]interface{}, error) {
clone := make(map[int64]interface{})
for k, v := range m {
// Look up the property by name and convert it to the ID.
property := p.GetPropertyByName(string(k))
if property != nil {
clone[property.Id] = v
} else {
return nil, fmt.Errorf("Property not found: %v", k)
}
}
return clone, nil
}
// Converts a map with property identifier keys to use string keys.
func (p *PropertyFile) DenormalizeMap(m map[int64]interface{}) (map[string]interface{}, error) {
clone := make(map[string]interface{})
for k, v := range m {
// Look up the property by ID and convert it to the name.
property := p.GetProperty(k)
if property != nil {
clone[property.Name] = v
} else {
return nil, fmt.Errorf("skyd.PropertyFile: Property not found: %v", k)
}
}
return clone, nil
}
//--------------------------------------
// Factorization
//--------------------------------------
// Converts the value for a given property to its internal representation.
// This only changes the value for 'factor' data types.
func (p *PropertyFile) Factorize(property *Property, value interface{}) (interface{}, error) {
if property.DataType == FactorDataType {
}
return value, nil
}
//--------------------------------------
// Utilities
//--------------------------------------
// Finds the next available action and object property identifiers.
func (p *PropertyFile) NextIdentifiers() (int64, int64) {
var nextPermanentId, nextTransientId int64 = 1, -1
for _, property := range p.properties {
if property.Transient && property.Id <= nextTransientId {
nextTransientId = property.Id - 1
} else if !property.Transient && property.Id >= nextPermanentId {
nextPermanentId = property.Id + 1
}
}
return nextPermanentId, nextTransientId
}