-
Notifications
You must be signed in to change notification settings - Fork 1
/
fmresultset.go
306 lines (261 loc) · 7.73 KB
/
fmresultset.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
package gofmcon
import (
"encoding/json"
"encoding/xml"
"fmt"
"strconv"
"strings"
"time"
)
const (
// DateFormat is a format of date on a particular layout
DateFormat = "01/02/2006"
// TimeFormat is a format of time on a particular layout
TimeFormat = "15:04:05"
// TimestampFormat is a format of timestamp on a particular layout
TimestampFormat = "01/02/2006 15:04:05"
)
// FMResultset is a collection of ResultSets
type FMResultset struct {
Resultset *Resultset `xml:"resultset"`
DataSource *DataSource `xml:"datasource"`
MetaData *MetaData `xml:"metadata"`
Version string `xml:"version,attr"`
FMError FMError `xml:"error"`
}
func (rs *FMResultset) prepareRecords() {
var fd FieldsDefinitions
if rs.MetaData != nil {
fd = rs.MetaData.getAllFieldDefinitions()
}
for _, r := range rs.Resultset.Records {
r.makeFieldsMap(false, fd)
}
}
// HasError checks if FMResultset was fetched with an error
func (rs *FMResultset) HasError() bool {
return rs.FMError.Code != 0
}
// DataSource store database name, layout name and time formats
type DataSource struct {
Database string `xml:"database,attr"`
DateFormat string `xml:"date_format,attr"`
Layout string `xml:"layout,attr"`
Table string `xml:"table,attr"`
TimeFormat string `xml:"time-format,attr"`
TimestampFormat string `xml:"timestamp-format,attr"`
TotalCount int `xml:"total-count,attr"`
}
// MetaData store fields' and related sets' meta information
type MetaData struct {
FieldDefinitions []*FieldDefinition `xml:"field-definition"`
RelatedSetDefinition *RelatedSetDefinition `xml:"relatedset-definition"`
}
func (md MetaData) getAllFieldDefinitions() []FieldDefinition {
var definitions []FieldDefinition
for _, def := range md.FieldDefinitions {
definitions = append(definitions, *def)
}
if md.RelatedSetDefinition == nil {
return definitions
}
for _, def := range md.RelatedSetDefinition.FieldDefinitions {
definitions = append(definitions, *def)
}
return definitions
}
// RelatedSetDefinition is meta information of related set
type RelatedSetDefinition struct {
Table string `xml:"table,attr"`
FieldDefinitions []*FieldDefinition `xml:"field-definition"`
}
type fieldDefinition struct {
Name string `xml:"name,attr"`
AutoEnter string `xml:"auto-enter,attr"`
FourDigitYear string `xml:"four-digit-year,attr"`
Global string `xml:"global,attr"`
MaxRepeat string `xml:"max-repeat,attr"`
NotEmpty string `xml:"not-empty,attr"`
NumericOnly string `xml:"numeric-only,attr"`
Result string `xml:"result,attr"`
TimeOfDay string `xml:"time-of-day,attr"`
Type string `xml:"type,attr"`
}
// FieldType represents a type of the field
type FieldType string
const (
// TypeText is a text type of the field
TypeText FieldType = "text"
// TypeNumber is a number type of the field
TypeNumber FieldType = "number"
// TypeDate is a date type of the field
TypeDate FieldType = "date"
// TypeTime is a time type of the field
TypeTime FieldType = "time"
// TypeTimestamp is a timestamp type of the field
TypeTimestamp FieldType = "timestamp"
// TypeContainer is a container type of the field
TypeContainer FieldType = "container"
)
// FieldDefinition store information about a field in given layout
type FieldDefinition struct {
Name string `xml:"name,attr"`
AutoEnter bool
FourDigitYear bool
Global bool
MaxRepeat int
NotEmpty bool
NumericOnly bool
TimeOfDay bool
Type FieldType
}
// FieldsDefinitions is type of []FieldDefinition
type FieldsDefinitions []FieldDefinition
func (fds FieldsDefinitions) getType(name string) FieldType {
for _, fd := range fds {
if fd.Name == name {
return fd.Type
}
}
return ""
}
func (fds FieldsDefinitions) getMaxRepeat(name string) int {
for _, fd := range fds {
if fd.Name == name {
return fd.MaxRepeat
}
}
return 1
}
// UnmarshalXML serializes xml data of FieldDefinition into the object
func (f *FieldDefinition) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var fd fieldDefinition
err := d.DecodeElement(&fd, &start)
if err != nil {
return err
}
f.AutoEnter = getBoolFromString(fd.AutoEnter)
f.FourDigitYear = getBoolFromString(fd.FourDigitYear)
f.Global = getBoolFromString(fd.Global)
f.NotEmpty = getBoolFromString(fd.NotEmpty)
f.NumericOnly = getBoolFromString(fd.NumericOnly)
f.TimeOfDay = getBoolFromString(fd.TimeOfDay)
f.Name = fd.Name
f.Type = FieldType(fd.Result)
mr, _ := strconv.Atoi(fd.MaxRepeat)
f.MaxRepeat = mr
return nil
}
func getBoolFromString(str string) bool {
if str == "yes" {
return true
}
return false
}
// FMError represents a FileMaker error
type FMError struct {
Code int `xml:"code,attr"`
}
func (e *FMError) String() string {
return fmt.Sprintf("filemaker_error: %s", FileMakerErrorCodes[e.Code])
}
func (e *FMError) Error() string {
return fmt.Sprintf("filemaker_error: %s", FileMakerErrorCodes[e.Code])
}
// Resultset is a set of records with meta information
type Resultset struct {
Count int `xml:"count,attr" json:"count"`
Fetched int `xml:"fetch-size,attr" json:"fetched"`
Records []*Record `xml:"record"`
}
// Record is FileMaker record
type Record struct {
ID int `xml:"record-id,attr"`
Fields []*Field `xml:"field"`
fieldsMap map[string]interface{}
RelatedSet []*RelatedSet `xml:"relatedset"`
}
// RelatedSet is a set of records returned from FileMaker database
type RelatedSet struct {
Count int `xml:"count,attr"`
Table string `xml:"table,attr"`
Records []*Record `xml:"record"`
}
func (r *Record) makeFieldsMap(isNested bool, fieldsDefinitions FieldsDefinitions) {
if r.fieldsMap == nil {
r.fieldsMap = map[string]interface{}{}
}
for _, f := range r.Fields {
var dataArr []interface{}
for _, val := range f.Data {
switch fieldsDefinitions.getType(f.Name) {
case TypeNumber:
number, err := strconv.ParseFloat(val, 64)
if err != nil {
dataArr = append(dataArr, nil)
} else {
dataArr = append(dataArr, number)
}
case TypeDate:
t, _ := time.Parse(DateFormat, val)
dataArr = append(dataArr, t)
case TypeTime:
t, _ := time.Parse(TimeFormat, val)
dataArr = append(dataArr, t)
case TypeTimestamp:
t, _ := time.Parse(TimestampFormat, val)
dataArr = append(dataArr, t)
default:
dataArr = append(dataArr, val)
}
}
var fieldData interface{}
maxRepeat := fieldsDefinitions.getMaxRepeat(f.Name)
if maxRepeat > 1 || len(dataArr) > 1 {
fieldData = dataArr
} else if len(dataArr) == 1 {
fieldData = dataArr[0]
}
fieldName := f.Name
if isNested {
idx := strings.Index(f.Name, "::")
if idx > 0 {
fieldName = f.Name[idx+2:]
}
}
r.fieldsMap[fieldName] = fieldData
}
for _, rs := range r.RelatedSet {
var relatedRecordsFieldMaps []interface{}
for _, rr := range rs.Records {
rr.makeFieldsMap(true, fieldsDefinitions)
relatedRecordsFieldMaps = append(relatedRecordsFieldMaps, rr.fieldsMap)
}
r.fieldsMap[rs.Table] = relatedRecordsFieldMaps
}
}
// Field stands for field in a record
type Field struct {
Name string `xml:"name,attr" json:"field"`
Data []string `xml:"data" json:"data"`
Type FieldType
}
// RelatedSetFromTable returns the set of related records from given
// related table
func (r *Record) RelatedSetFromTable(t string) *RelatedSet {
rSet := &RelatedSet{}
for _, elem := range r.RelatedSet {
if elem.Table == t {
rSet = elem
}
}
return rSet
}
// Field returns fields data for given field name
func (r *Record) Field(name string) interface{} {
return r.fieldsMap[name]
}
// JSONFields return JSON representation of Record
func (r *Record) JSONFields() ([]byte, error) {
return json.MarshalIndent(r.fieldsMap, "", " ")
}