-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDataSet.go
354 lines (282 loc) · 10.1 KB
/
DataSet.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
//******************************************************************************************************
// DataSet.go - Gbtc
//
// Copyright © 2021, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may not use this
// file except in compliance with the License. You may obtain a copy of the License at:
//
// http://opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 09/23/2021 - J. Ritchie Carroll
// Generated original version of source code.
//
//******************************************************************************************************
package data
import (
"errors"
"strconv"
"strings"
"time"
"github.com/shopspring/decimal"
"github.com/sttp/goapi/sttp/guid"
"github.com/sttp/goapi/sttp/xml"
)
const (
// XmlSchemaNamespace defines the schema namespace for the W3C XML Schema Definition Language (XSD)
// used by STTP metadata tables.
XmlSchemaNamespace = "http://www.w3.org/2001/XMLSchema"
// ExtXmlSchemaDataNamespace is used to define extended types for XSD elements, e.g., Guid and expression data types.
ExtXmlSchemaDataNamespace = "urn:schemas-microsoft-com:xml-msdata"
// DateTimeFormat defines the format of date/time values in an XSD formatted XML schema.
DateTimeFormat = "2006-01-02T15:04:05.99-07:00"
)
// DataSet represents an in-memory cache of records that is structured similarly to information
// defined in a database. The data set object consists of a collection of data table objects.
// See https://sttp.github.io/documentation/data-sets/ for more information.
// Note that this implementation uses a case-insensitive map for DataTable name lookups.
// Internally, case-insensitive lookups are accomplished using `strings.ToUpper`.
type DataSet struct {
tables map[string]*DataTable
// Name defines the name of the DataSet.
Name string
}
// NewDataSet creates a new DataSet.
func NewDataSet() *DataSet {
return &DataSet{
tables: make(map[string]*DataTable),
Name: "DataSet",
}
}
// AddTable adds the specified table to the DataSet.
func (ds *DataSet) AddTable(table *DataTable) {
ds.tables[strings.ToUpper(table.Name())] = table
}
// Table gets the DataTable for the specified tableName if the name exists;
// otherwise, nil is returned. Lookup is case-insensitive.
func (ds *DataSet) Table(tableName string) *DataTable {
if table, ok := ds.tables[strings.ToUpper(tableName)]; ok {
return table
}
return nil
}
// TableNames gets the table names defined in the DataSet.
func (ds *DataSet) TableNames() []string {
tableNames := make([]string, 0, len(ds.tables))
for _, table := range ds.tables {
tableNames = append(tableNames, table.Name())
}
return tableNames
}
// Tables gets the DataTable instances defined in the DataSet.
func (ds *DataSet) Tables() []*DataTable {
tables := make([]*DataTable, 0, len(ds.tables))
for _, table := range ds.tables {
tables = append(tables, table)
}
return tables
}
// CreateTable creates a new DataTable associated with the DataSet.
// Use AddTable to add the new table to the DataSet.
func (ds *DataSet) CreateTable(name string) *DataTable {
return newDataTable(ds, name)
}
// TableCount gets the total number of tables defined in the DataSet.
func (ds *DataSet) TableCount() int {
return len(ds.tables)
}
// RemoveTable removes the specified tableName from the DataSet. Returns
// true if table was removed; otherwise, false if it did not exist.
// Lookup is case-insensitive.
func (ds *DataSet) RemoveTable(tableName string) bool {
tableName = strings.ToUpper(tableName)
if _, ok := ds.tables[tableName]; ok {
delete(ds.tables, tableName)
return true
}
return false
}
// String get a representation of the DataSet as a string.
func (ds *DataSet) String() string {
var image strings.Builder
image.WriteString(ds.Name)
image.WriteString(" [")
i := 0
for _, table := range ds.tables {
if i > 0 {
image.WriteString(", ")
}
image.WriteString(table.Name())
i++
}
image.WriteRune(']')
return image.String()
}
// ParseXml loads the DataSet from the XML in the specified buffer.
func (ds *DataSet) ParseXml(data []byte) error {
var doc xml.XmlDocument
if err := doc.LoadXml(data); err != nil {
return err
}
return ds.ParseXmlDocument(&doc)
}
// ParseXmlDocument loads the DataSet from an existing XmlDocument.
func (ds *DataSet) ParseXmlDocument(doc *xml.XmlDocument) error {
root := doc.Root
// Find schema node
schema, found := root.Item["schema"]
if !found {
return errors.New("failed to parse DataSet XML: Cannot find schema node")
}
id, found := schema.Attributes["id"]
if !found || id != root.Name {
return errors.New("failed to parse DataSet XML: Cannot find schema node matching \"" + root.Name + "\"")
}
// Validate schema namespace
if schema.Namespace != XmlSchemaNamespace {
return errors.New("failed to parse DataSet XML: cannot find schema namespace \"" + XmlSchemaNamespace + "\"")
}
// Populate DataSet schema
ds.loadSchema(schema)
// Populate DataSet records
ds.loadRecords(&root)
return nil
}
//gocyclo:ignore
func (ds *DataSet) loadSchema(schema *xml.XmlNode) {
schemaPrefix := schema.Prefix()
if len(schemaPrefix) > 0 {
schemaPrefix += ":"
}
// Find choice elements representing schema table definitions
tableNodes := schema.SelectNodes("element/complexType/choice/element")
for _, tableNode := range tableNodes {
tableName, found := tableNode.Attributes["name"]
if !found || len(tableName) == 0 {
continue
}
dataTable := ds.CreateTable(tableName)
// Find sequence elements representing schema table field definitions
fieldNodes := tableNode.SelectNodes("complexType/sequence/element")
dataTable.InitColumns(len(fieldNodes))
for _, fieldNode := range fieldNodes {
fieldName, found := fieldNode.Attributes["name"]
if !found || len(fieldName) == 0 {
continue
}
typeName, found := fieldNode.Attributes["type"]
if !found || len(typeName) == 0 {
continue
}
typeName = strings.TrimPrefix(typeName, schemaPrefix)
// Check for extended data type (allows XSD Guid field definitions)
extDataType, found := fieldNode.Attributes["DataType"]
if found && len(extDataType) > 0 {
// Ignore DataType attributes that do not target desired namespace
if fieldNode.AttributeNamespaces["DataType"] != ExtXmlSchemaDataNamespace {
extDataType = ""
}
}
dataType, found := ParseXsdDataType(typeName, extDataType)
// Columns with unsupported XSD data types are skipped
if !found {
continue
}
// Check for computed expression
expression, found := fieldNode.Attributes["Expression"]
if found && len(expression) > 0 {
// Ignore Expression attributes that do not target desired namespace
if fieldNode.AttributeNamespaces["Expression"] != ExtXmlSchemaDataNamespace {
expression = ""
}
}
dataColumn := dataTable.CreateColumn(fieldName, dataType, expression)
dataTable.AddColumn(dataColumn)
}
ds.AddTable(dataTable)
}
}
//gocyclo:ignore
func (ds *DataSet) loadRecords(root *xml.XmlNode) {
// Each root node child that matches a table name represents a record
for _, table := range ds.Tables() {
records := root.Items[table.Name()]
table.InitRows(len(records))
for _, record := range records {
dataRow := table.CreateRow()
// Each child node of a record represents a field value
for _, field := range record.ChildNodes {
column := table.ColumnByName(field.Name)
if column == nil {
continue
}
columnIndex := column.Index()
value := field.Value()
switch column.Type() {
case DataType.String:
dataRow.SetValue(columnIndex, value)
case DataType.Boolean:
dataRow.SetValue(columnIndex, value == "true")
case DataType.DateTime:
dt, _ := time.Parse(DateTimeFormat, value)
dataRow.SetValue(columnIndex, dt)
case DataType.Single:
f32, _ := strconv.ParseFloat(value, 32)
dataRow.SetValue(columnIndex, float32(f32))
case DataType.Double:
f64, _ := strconv.ParseFloat(value, 64)
dataRow.SetValue(columnIndex, f64)
case DataType.Decimal:
d, _ := decimal.NewFromString(value)
dataRow.SetValue(columnIndex, d)
case DataType.Guid:
g, _ := guid.Parse(value)
dataRow.SetValue(columnIndex, g)
case DataType.Int8:
i8, _ := strconv.ParseInt(value, 0, 8)
dataRow.SetValue(columnIndex, int8(i8))
case DataType.Int16:
i16, _ := strconv.ParseInt(value, 0, 16)
dataRow.SetValue(columnIndex, int16(i16))
case DataType.Int32:
i32, _ := strconv.ParseInt(value, 0, 32)
dataRow.SetValue(columnIndex, int32(i32))
case DataType.Int64:
i64, _ := strconv.ParseInt(value, 0, 64)
dataRow.SetValue(columnIndex, i64)
case DataType.UInt8:
ui8, _ := strconv.ParseUint(value, 0, 8)
dataRow.SetValue(columnIndex, uint8(ui8))
case DataType.UInt16:
ui16, _ := strconv.ParseUint(value, 0, 16)
dataRow.SetValue(columnIndex, uint16(ui16))
case DataType.UInt32:
ui32, _ := strconv.ParseUint(value, 0, 32)
dataRow.SetValue(columnIndex, uint32(ui32))
case DataType.UInt64:
ui64, _ := strconv.ParseUint(value, 0, 64)
dataRow.SetValue(columnIndex, ui64)
}
}
table.AddRow(dataRow)
}
}
}
// // WriteXML saves the DataSet information as XML into the specified buffer.
// func (ds *DataSet) WriteXml(data *[]byte, dataSetName string) {
// // TODO: Will be needed by DataPublisher
// }
// FromXml creates a new DataSet as read from the XML in the specified buffer.
func FromXml(buffer []byte) *DataSet {
dataSet := NewDataSet()
dataSet.ParseXml(buffer)
return dataSet
}