/
Attribute+XMLParser.swift
43 lines (38 loc) · 1.25 KB
/
Attribute+XMLParser.swift
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
import Foundation
import SWXMLHash
extension Attribute {
init(xml: XMLIndexer) throws {
self.name = try xml.value(ofAttribute: "name")
self.type = try AttributeType(stringValue: try xml.value(ofAttribute: "attributeType"))
self.isScalar = xml.value(ofAttribute: "usesScalarValueType") ?? false
self.isOptional = xml.value(ofAttribute: "optional") ?? false
self.userInfo = try xml.userInfo()
}
}
private extension AttributeType {
private static let typesMapping: [String: AttributeType] = [
"Boolean": .boolean,
"Binary": .binary,
"Date": .date,
"Decimal": .decimal,
"Double": .double,
"Float": .float,
"Integer 16": .integer16,
"Integer 32": .integer32,
"Integer 64": .integer64,
"String": .string,
"Transformable": .transformable
]
init(stringValue: String) throws {
guard let type = AttributeType.typesMapping[stringValue] else {
throw UnknownAttributeTypeError(type: stringValue)
}
self = type
}
}
private struct UnknownAttributeTypeError: Error, CustomStringConvertible {
let type: String
var description: String {
return "Unexpected attribute type: '\(type)'"
}
}