mganss / XmlSchemaClassGenerator

Generate C# classes from XML Schema files
Apache License 2.0
626 stars 180 forks source link

Specify name for a property #220

Open florin141 opened 4 years ago

florin141 commented 4 years ago

Consider this:

<simpleType name="SignType">
    <annotation>
        <documentation>gml:SignType is a convenience type with values "+" (plus) and "-" (minus).</documentation>
    </annotation>
    <restriction base="string">
        <enumeration value="-"/>
        <enumeration value="+"/>
    </restriction>
</simpleType>

The generated c# code is this:

/// <summary>
/// <para>gml:SignType is a convenience type with values "+" (plus) and "-" (minus).</para>
/// </summary>
[System.CodeDom.Compiler.GeneratedCodeAttribute("XmlSchemaClassGenerator", "1.0.0.0")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute("SignType", Namespace="http://www.opengis.net/gml/3.2")]
public enum SignType
{

    [System.Xml.Serialization.XmlEnumAttribute("-")]
    Item_,

    [System.Xml.Serialization.XmlEnumAttribute("+")]
    Plus,
}

As you can see, 'Plus' name is a perfect name for the symbol '+', but 'Item_' not so much for the symbol '-'.

Is there any way I could override that behaviour and say something like, for 'SignType' if you encounter an enumeration with value="-", change the name of the property to 'Minus'?

mganss commented 4 years ago

We have a dictionary of characters that must not occur in an identifier: https://github.com/mganss/XmlSchemaClassGenerator/blob/e0d61196b2293e7ec9b5af30833f4ecf60f0a4a5/XmlSchemaClassGenerator/NamingExtensions.cs#L12-L77 + is in that dictionary so it gets replaced by Plus but although - is also invalid it gets replaced with _ in a separate step. And because _ by itself is again invalid it gets prepended with Item. The reason we're replacing - with _ and not Minus (or Dash) is that - occurs frequently as a word separator. I think it might be best to make this a special case so that the exact string "-" is replaced by "Minus".

frankhommers commented 3 years ago

@florin141 Couldn't you do this by implementing your own NamingProvider?

florin141 commented 3 years ago

Mmm, I'm not sure how to do that. But I'll have a closer look at the code soon and I'll try that.

Any idea where I could start?

frankhommers commented 3 years ago

Make a copy of this: https://github.com/mganss/XmlSchemaClassGenerator/blob/master/XmlSchemaClassGenerator/NamingProvider.cs

Rename it and supply it as NamingProvider.

But you need to generate stuff programatically then (See https://github.com/mganss/XmlSchemaClassGenerator/#usage -> Last part: "For use from code" )