-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfmpxmlresult2SwiftyJSONModel.xslt
103 lines (98 loc) · 3.85 KB
/
fmpxmlresult2SwiftyJSONModel.xslt
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
<?xml version="1.0" encoding="UTF-8"?>
<!--
Written by Gjermund G Thorsen 2017, all rights deserved
for the purpose of generating SwiftJSONModel from FMPXMLRESULT
This script was inspired by a talk given by https://github.com/alickbass for CocoaHeadNL 2017, Jan 18
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fmp="http://www.filemaker.com/fmpxmlresult" version="1.0">
<xsl:output method="text" version="1.0" encoding="UTF-8" indent="no"/>
<xsl:template match="fmp:FMPXMLRESULT">
<xsl:text>import SwiftyJSONModel
struct </xsl:text><xsl:value-of select="$tableName"/><xsl:text> {
</xsl:text>
<xsl:for-each select="fmp:METADATA/fmp:FIELD">
<xsl:text> let </xsl:text><xsl:value-of select="@NAME"/><xsl:text>: </xsl:text>
<xsl:value-of select="concat(
substring( 'String', 1 div boolean( @TYPE = 'TEXT' ) ),
substring( 'Binary', 1 div boolean( @TYPE = 'CONTAINER' ) ),
substring( 'NSTimeInterval', 1 div boolean( @TYPE = 'DATE' ) ),
substring( 'NSTimeInterval', 1 div boolean( @TYPE = 'TIMESTAMP' ) ),
substring( 'NSTimeInterval', 1 div boolean( @TYPE = 'TIME' ) ),
substring( 'Int', 1 div boolean( @TYPE = 'NUMBER' ) )
)" />
<xsl:text>
</xsl:text>
</xsl:for-each>
<xsl:text>}
extension </xsl:text><xsl:value-of select="$tableName"/><xsl:text>: JSONModelType {
enum PropertyKey: String {
case </xsl:text>
<xsl:for-each select="fmp:METADATA/fmp:FIELD">
<xsl:value-of select="@NAME"/>
<xsl:choose>
<xsl:when test="position()=last()">
<xsl:text/>
</xsl:when>
<xsl:otherwise>
<xsl:text>, </xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<xsl:text>
}
init( object: JSONObject<PropertyKey> ) throws {
</xsl:text>
<xsl:for-each select="fmp:METADATA/fmp:FIELD">
<xsl:text> </xsl:text>
<xsl:value-of select="@NAME"/>
<xsl:text> = try object.value( for: .</xsl:text>
<xsl:value-of select="@NAME"/>
<xsl:text> )
</xsl:text>
</xsl:for-each>
<xsl:text>
}
var dictValue: [PropertyKey : JSONRepresentable?] {
return [ </xsl:text>
<xsl:for-each select="fmp:METADATA/fmp:FIELD">
<xsl:text>.</xsl:text>
<xsl:value-of select="@NAME"/>
<xsl:text>: </xsl:text>
<xsl:value-of select="@NAME"/>
<xsl:choose>
<xsl:when test="position()=last()">
<xsl:text/>
</xsl:when>
<xsl:otherwise>
<xsl:text>, </xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<xsl:text> ]
}
}
let </xsl:text><xsl:value-of select="$tableName"/><xsl:text>JSON = //JSON that we'll use for our model
do {
let </xsl:text><xsl:value-of select="$tableName"/><xsl:text> = try </xsl:text><xsl:value-of select="$tableName"/><xsl:text>( json: </xsl:text><xsl:value-of select="$tableName"/><xsl:text>JSON )
print( </xsl:text><xsl:value-of select="$tableName"/><xsl:text>.jsonValue )
} catch let error {
print( error )
}
</xsl:text>
</xsl:template>
<xsl:variable name="databaseName">
<xsl:value-of select="fmp:FMPXMLRESULT/fmp:DATABASE/@NAME"/>
</xsl:variable>
<xsl:variable name="tableName">
<xsl:value-of select="fmp:FMPXMLRESULT/fmp:DATABASE/@LAYOUT"/>
</xsl:variable>
<xsl:variable name="timeformat">
<xsl:value-of select="fmp:FMPXMLRESULT/fmp:DATABASE/@TIMEFORMAT"/>
</xsl:variable>
</xsl:stylesheet><!--
========================================================================================
Copyright (c) 2008 Gjermund Gusland Thorsen, released under the MIT License.
All rights deserved.
This piece comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law.
========================================================================================
-->