Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specify name for a property #220

Open
florin141 opened this issue Aug 9, 2020 · 4 comments
Open

Specify name for a property #220

florin141 opened this issue Aug 9, 2020 · 4 comments

Comments

@florin141
Copy link

florin141 commented Aug 9, 2020

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
Copy link
Owner

mganss commented Aug 10, 2020

We have a dictionary of characters that must not occur in an identifier:

private static readonly Dictionary<char, string> InvalidChars = new Dictionary<char, string>
{
['\x00'] = "Null",
['\x01'] = "StartOfHeading",
['\x02'] = "StartOfText",
['\x03'] = "EndOfText",
['\x04'] = "EndOfTransmission",
['\x05'] = "Enquiry",
['\x06'] = "Acknowledge",
['\x07'] = "Bell",
['\x08'] = "Backspace",
['\x09'] = "HorizontalTab",
['\x0A'] = "LineFeed",
['\x0B'] = "VerticalTab",
['\x0C'] = "FormFeed",
['\x0D'] = "CarriageReturn",
['\x0E'] = "ShiftOut",
['\x0F'] = "ShiftIn",
['\x10'] = "DataLinkEscape",
['\x11'] = "DeviceControl1",
['\x12'] = "DeviceControl2",
['\x13'] = "DeviceControl3",
['\x14'] = "DeviceControl4",
['\x15'] = "NegativeAcknowledge",
['\x16'] = "SynchronousIdle",
['\x17'] = "EndOfTransmissionBlock",
['\x18'] = "Cancel",
['\x19'] = "EndOfMedium",
['\x1A'] = "Substitute",
['\x1B'] = "Escape",
['\x1C'] = "FileSeparator",
['\x1D'] = "GroupSeparator",
['\x1E'] = "RecordSeparator",
['\x1F'] = "UnitSeparator",
['\x21'] = "ExclamationMark",
['\x22'] = "Quote",
['\x23'] = "Hash",
['\x24'] = "Dollar",
['\x25'] = "Percent",
['\x26'] = "Ampersand",
['\x27'] = "SingleQuote",
['\x28'] = "LeftParenthesis",
['\x29'] = "RightParenthesis",
['\x2A'] = "Asterisk",
['\x2B'] = "Plus",
['\x2C'] = "Comma",
['\x2E'] = "Period",
['\x2F'] = "Slash",
['\x3A'] = "Colon",
['\x3B'] = "Semicolon",
['\x3C'] = "LessThan",
['\x3D'] = "Equal",
['\x3E'] = "GreaterThan",
['\x3F'] = "QuestionMark",
['\x40'] = "At",
['\x5B'] = "LeftSquareBracket",
['\x5C'] = "Backslash",
['\x5D'] = "RightSquareBracket",
['\x5E'] = "Caret",
['\x60'] = "Backquote",
['\x7B'] = "LeftCurlyBrace",
['\x7C'] = "Pipe",
['\x7D'] = "RightCurlyBrace",
['\x7E'] = "Tilde",
['\x7F'] = "Delete"
};

+ 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
Copy link

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

@florin141
Copy link
Author

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
Copy link

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" )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants