Mercurial > pub > ModelGenerator
changeset 6:1f4009d4afb6
working on model to csharp transform
author | cin |
---|---|
date | Mon, 05 Mar 2018 10:37:47 +0300 |
parents | 6f67d3a6b950 |
children | 3fe157be5141 |
files | data/sample.xml xslt/generator.csharp.xsl xslt/model.xsl |
diffstat | 3 files changed, 75 insertions(+), 109 deletions(-) [+] |
line wrap: on
line diff
--- a/data/sample.xml Fri Mar 02 18:34:19 2018 +0300 +++ b/data/sample.xml Mon Mar 05 10:37:47 2018 +0300 @@ -8,12 +8,31 @@ <entity name="Gallery"> <primaryKey name="Id" type="uuid"/> - + <hasMany name="ItemLinks" type="GallertItemLink"> + <otherKey name="Item"/> + <clr:association/> + </hasMany> </entity> <entity name="GalleryItemLink"> - <primaryKey> + <primaryKey name="Id"> + <member name="Item"/> + <member name="Gallery"/> </primaryKey> - <hasA /> + <hasA name="Item" type="Item"> + <thisKey name="ItemId" /> + <clr:association /> + <clr:lazy /> + </hasA> + <hasA name="Gallery" type="Gallery"> + <thisKey name="GalleryId" /> + <clr:association /> + <clr:lazy/> + </hasA> + </entity> + + <entity name="Item"> + <primaryKey name="Id" type="uuid"/> + <property name="href" type="text"/> </entity> </package> \ No newline at end of file
--- a/xslt/generator.csharp.xsl Fri Mar 02 18:34:19 2018 +0300 +++ b/xslt/generator.csharp.xsl Mon Mar 05 10:37:47 2018 +0300 @@ -28,7 +28,7 @@ <xsl:template match="m:entity" mode="entity"> <!-- select all members --> <xsl:variable name="members"> - <xsl:apply-templates mode="fiter-members"/> + <xsl:apply-templates mode="filter-members"/> </xsl:variable> <cs:class modifiers="partial"> @@ -138,51 +138,43 @@ <xsl:template match="*" mode="property"> <cs:property modifiers="public"> - <xsl:attribute name="name"><xsl:apply-templates select="." mode="property-name"/></xsl:attribute> - <xsl:attribute name="modifiers"><xsl:apply-templates select="." mode="property-modifiers"/></xsl:attribute> - <xsl:apply-templates select="." mode="property-type"/> - <xsl:apply-templates select="." mode="property-attributes"/> - <xsl:apply-templates select="." mode="property-extension" /> + <xsl:attribute name="name"><xsl:apply-templates select="." mode="member-name"/></xsl:attribute> + <xsl:attribute name="modifiers"><xsl:apply-templates select="." mode="member-modifiers"/></xsl:attribute> + <xsl:apply-templates select="." mode="member-type"/> + <xsl:apply-templates select="." mode="member-attributes"/> + <xsl:apply-templates select="." mode="member-extension" /> <xsl:apply-templates select="." mode="property-accessors"/> </cs:property> </xsl:template> - <!-- property-name --> - <xsl:template match="m:hasA/clr:association[not(@name|@clr:name)]" mode="property-name"> + <!-- member-name --> + <xsl:template match="m:hasA/clr:association[not(@name|@clr:name)]" mode="member-name"> <!-- if the association doesn't define a name, use it from the parent node --> - <xsl:apply-templates select=".." mode="property-name"/> + <xsl:apply-templates select=".." mode="member-name"/> </xsl:template> - <xsl:template match="m:hasMany/clr:association[not(@name|@clr:name)]" mode="property-name"> + <xsl:template match="m:hasMany/clr:association[not(@name|@clr:name)]" mode="member-name"> <!-- if the association doesn't define a name, use it from the parent node --> - <xsl:apply-templates select=".." mode="property-name"/> + <xsl:apply-templates select=".." mode="member-name"/> </xsl:template> - <xsl:template match="*" mode="property-name"> - <xsl:apply-templates select="." mode="member-name"/> - </xsl:template> - - <!-- property-modifiers --> - <xsl:template match="*" mode="property-modifiers"> + <!-- member-modifiers --> + <xsl:template match="*" mode="member-modifiers"> <xsl:text>public</xsl:text> </xsl:template> - <!-- property-type --> - <xsl:template match="*" mode="property-type"> - <xsl:apply-templates select="." mode="member-type"/> + <!-- member-type --> + <xsl:template match="m:hasA[@type]/clr:association[not(clr:type)]" mode="member-type"> + <xsl:apply-templates select=".." mode="member-type"/> </xsl:template> - <xsl:template match="m:hasA[@type]/clr:association[not(clr:type)]" mode="property-type"> - <xsl:apply-templates select=".." mode="property-type"/> - </xsl:template> - - <xsl:template match="m:hasMany[@type]/clr:association[not(clr:type)]" mode="property-type"> + <xsl:template match="m:hasMany[@type]/clr:association[not(clr:type)]" mode="member-type"> <cs:array> - <xsl:apply-templates select=".." mode="property-type"/> + <xsl:apply-templates select=".." mode="member-type"/> </cs:array> </xsl:template> - <xsl:template match="m:hasA[@type]/m:thisKey" mode="property-type"> + <xsl:template match="m:hasA[@type]/m:thisKey" mode="member-type"> <xsl:variable name="otherKey"> <xsl:call-template name="getPrimaryKey"> <xsl:with-param name="type" select="../@type"/> @@ -190,40 +182,37 @@ </xsl:variable> <xsl:choose> <xsl:when test="@optional"> + <cs:nullable> + <xsl:apply-templates select="exsl:node-set($otherKey)" mode="member-type"/> + </cs:nullable> </xsl:when> - <cs:nullable> - <xsl:apply-templates select="exsl:node-set($otherKey)" mode="property-type"/> - </cs:nullable> <xsl:otherwise> - <xsl:apply-templates select="exsl:node-set($otherKey)" mode="property-type"/> + <xsl:apply-templates select="exsl:node-set($otherKey)" mode="member-type"/> </xsl:otherwise> </xsl:choose> </xsl:template> - <!-- property-attributes --> - <xsl:template match="m:primaryKey" mode="property-attributes"> + <!-- member-attributes --> + <xsl:template match="m:primaryKey" mode="member-attributes"> <cs:attribute> <cs:type name="PrimaryKey" namespace="Linq2Db" /> </cs:attribute> </xsl:template> - <xsl:template match="m:hasA/clr:association" mode="property-attributes"> + <xsl:template match="m:hasA/clr:association" mode="member-attributes"> <cs:attribute> <cs:type name="Association" namespace="Linq2Db"/> <cs:parameter name="thisKey"> <cs:nameOf> - <xsl:call-template name="getMember"> + <xsl:call-template name="getClrMemberReference"> <xsl:with-param name="memberName" select="../@name"/> - <xsl:with-param name="explicitMemberName" select="@thisKey"/> - <xsl:with-param name="type" select="../../@name"/> - <xsl:with-param name="short" select="true()"/> + <xsl:with-param name="type" select="../@declaringType"/> </xsl:call-template> </cs:nameOf> </cs:parameter> <cs:parameter name="otherKey"> <cs:nameOf> - <xsl:call-template name="getKeyMember"> - <xsl:with-param name="explicitMemberName" select="@thisKey"/> + <xsl:call-template name="getClrKeyReference"> <xsl:with-param name="type" select="../@type"/> </xsl:call-template> </cs:nameOf> @@ -231,40 +220,28 @@ </cs:attribute> </xsl:template> - <xsl:template match="m:hasMany/clr:association" mode="property-attributes"> + <xsl:template match="m:hasMany/clr:association" mode="member-attributes"> <cs:attribute> <cs:type name="Association" namespace="Linq2Db"/> <!-- thisKey points to own primaryKey which may be inherited, using getKeyName to address such cases --> <cs:parameter name="thisKey"> <cs:nameOf> - <xsl:call-template name="getKeyMember"> + <xsl:call-template name="getClrKeyReference"> <xsl:with-param name="type" select="../../@name"/> - <xsl:with-param name="explicitMemberName" select="@thisKey"/> - <xsl:with-param name="short" select="true()"/> </xsl:call-template> </cs:nameOf> </cs:parameter> <cs:parameter name="otherKey"> <cs:nameOf> - <xsl:call-template name="getMember"> + <xsl:call-template name="getClrMemberReference"> <xsl:with-param name="type" select="../@type"/> <xsl:with-param name="memberName" select="../m:otherKey/@name"/> - <xsl:with-param name="explicitMemberName" select="@otherKey"/> </xsl:call-template> </cs:nameOf> </cs:parameter> </cs:attribute> </xsl:template> - <xsl:template match="*" mode="property-attributes"> - <xsl:apply-templates select="." mode="member-attributes"/> - </xsl:template> - - <!-- property-extension --> - <xsl:template match="*" mode="property-extension"> - <xsl:apply-templates select="." mode="member-extension"/> - </xsl:template> - <!-- property-accessors --> <xsl:template match="*" mode="property-accessors"> <cs:get/> @@ -298,45 +275,41 @@ <!-- fields --> <xsl:template match="*" mode="field"> <cs:field> - <xsl:attribute name="name"><xsl:apply-templates select="." mode="field-name"/></xsl:attribute> - <xsl:apply-templates select="." mode="field-type"/> + <xsl:attribute name="name"><xsl:apply-templates select="." mode="member-name"/></xsl:attribute> + <xsl:apply-templates select="." mode="member-type"/> <xsl:apply-templates select="." mode="field-initializer"/> </cs:field> </xsl:template> <!-- field-name --> - <xsl:template match="m:hasA/clr:lazy" mode="field-name"> + <xsl:template match="m:hasA/clr:lazy" mode="member-name"> <xsl:text>m_lazy</xsl:text> - <xsl:apply-templates select=".." mode="property-name"/> + <xsl:apply-templates select=".." mode="member-name"/> </xsl:template> - <xsl:template match="clr:lazy[@field]" mode="field-name"> + <xsl:template match="clr:lazy[@field]" mode="member-name"> <xsl:value-of select="@field"/> </xsl:template> - <xsl:template match="*" mode="field-name"> - <xsl:apply-templates select="." mode="member-name"/> - </xsl:template> - - <!-- field-type --> - <xsl:template match="m:hasA[@optional='true']/clr:lazy" mode="field-type"> - <cs:type name="NullableReference" namespace="Pallada.Data"> - <xsl:call-template name="getKeyType"> + <!-- member-type --> + <xsl:template match="m:hasA[@optional='true']/clr:lazy" mode="member-type"> + <xsl:variable name="otherKey"> + <xsl:call-template name="getPrimaryKey"> <xsl:with-param name="type" select="../@type"/> </xsl:call-template> - <xsl:apply-templates select=".." mode="property-type"/> + </xsl:variable> + <cs:type name="NullableReference" namespace="Pallada.Data"> + <xsl:apply-templates select="exsl:node-set($otherKey)" mode="member-type"/> + <xsl:apply-templates select=".." mode="member-type"/> </cs:type> </xsl:template> - <xsl:template match="m:hasA[@optional!='true']/clr:lazy" mode="field-type"> + <xsl:template match="m:hasA[@optional!='true']/clr:lazy" mode="member-type"> <cs:type name="Reference" namespace="Pallada.Data"> - <xsl:apply-templates select="../m:thisKey" mode="property-type"/> - <xsl:apply-templates select=".." mode="property-type"/> + <xsl:apply-templates select="../m:thisKey" mode="member-type"/> + <xsl:apply-templates select=".." mode="member-type"/> </cs:type> </xsl:template> - <xsl:template match="*" mode="field-type"> - <xsl:apply-templates select="." mode="member-type"/> - </xsl:template> <!-- field-initializer --> <xsl:template match="*|text()" mode="field-initializer"/> @@ -367,7 +340,7 @@ </xsl:template> <xsl:template match="*" mode="clr-member-reference"> - <xsl:param name="$type" select="@declaringType"/> + <xsl:param name="type" select="@declaringType"/> <cs:member> <xsl:attribute name="name"> <xsl:apply-templates select="." mode="member-name"/> @@ -378,7 +351,6 @@ </cs:member> </xsl:template> - <!-- resolves CLR type for the given type --> <xsl:template name="getClrType"> <xsl:param name="type" />
--- a/xslt/model.xsl Fri Mar 02 18:34:19 2018 +0300 +++ b/xslt/model.xsl Mon Mar 05 10:37:47 2018 +0300 @@ -75,7 +75,7 @@ </xsl:call-template> </xsl:variable> - <xsl:variable name="members" select="exsl:node-set($scopeMembers)/*[not($memberName) or @name='$memberName']"/> + <xsl:variable name="members" select="exsl:node-set($scopeMembers)/*[not($memberName) or @name=$memberName]"/> <xsl:choose> <xsl:when test="$members"> @@ -92,7 +92,7 @@ <xsl:otherwise> <xsl:call-template name="warn"> <xsl:with-param name="msg"> - <t:trace msg="failed to resolve {$member}"/> + <t:trace msg="failed to resolve {$memberName}, scope={$scope}"/> <t:trace msg="inspected classes"> <xsl:for-each select="$seen | $self"> <t:trace msg="{name()} {@name}"/> @@ -113,7 +113,7 @@ <xsl:apply-templates mode="filter-members"/> </xsl:when> <xsl:when test="$scope='keys'"> - <xsl:apply-template mode="filter-keys"/> + <xsl:apply-templates mode="filter-keys"/> </xsl:when> <xsl:when test="$scope='properties'"> <xsl:apply-templates mode="fiflter-properties"/> @@ -163,33 +163,8 @@ <xsl:param name="type" /> <xsl:call-template name="getMembers"> <xsl:with-param name="type" select="$type"/> - <xsl:with-param name="scope" select="keys"/> + <xsl:with-param name="scope" select="'keys'"/> </xsl:call-template> </xsl:template> - <xsl:template match="*|text()" mode="process-member"/> - - <xsl:template match="m:hasA" mode="process-member"> - <xsl:apply-templates mode="process-member"/> - </xsl:template> - - <xsl:template match="m:hasA[@type]/m:thisKey" mode="foreign-key"> - <xsl:variable name="foreignPrimaryKey"> - <xsl:call-template name="getPrimaryKey"> - <xsl:with-param name="type" select="../@type"/> - </xsl:call-template> - </xsl:variable> - <m:property type="{exsl:node-set($foreignPrimaryKey)[1]/@type}"> - <xsl:copy-of select="@*"/> - </m:property> - </xsl:template> - - <xsl:template match="m:primaryKey" mode="process-member"> - <m:property> - <xsl:copy-of select="@*"/> - <xsl:copy-of select="*"/> - </m:property> - </xsl:template> - - </xsl:stylesheet>