Skip to content

Commit

Permalink
Add union configuration option (see #397)
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Ganss committed Jul 18, 2023
1 parent 2a4d8c1 commit c60cc6e
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 3 deletions.
3 changes: 3 additions & 0 deletions XmlSchemaClassGenerator.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ static void Main(string[] args)
var enumAsString = false;
var namespaceFiles = new List<string>();
var nameSubstituteFiles = new List<string>();
var unionCommonType = false;

var options = new OptionSet {
{ "h|help", "show this message and exit", v => showHelp = v != null },
Expand Down Expand Up @@ -148,6 +149,7 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l
{ "ar|useArrayItemAttribute", "use ArrayItemAttribute for sequences with single elements (default is true)", v => useArrayItemAttribute = v != null },
{ "es|enumAsString", "Use string instead of enum for enumeration", v => enumAsString = v != null },
{ "ca|commandArgs", "generate a comment with the exact command line arguments that were used to generate the source code (default is true)", v => generateCommandLineArgs = v != null },
{ "uc|unionCommonType", "generate a common type for unions if possible (default is false)", v => unionCommonType = v != null },
};

var globsAndUris = options.Parse(args);
Expand Down Expand Up @@ -232,6 +234,7 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l
GenerateCommandLineArgumentsComment = generateCommandLineArgs,
UseArrayItemAttribute = useArrayItemAttribute,
EnumAsString = enumAsString,
MapUnionToWidestCommonType = unionCommonType
};

if (nameSubstituteMap.Any())
Expand Down
1 change: 1 addition & 0 deletions XmlSchemaClassGenerator.Tests/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public static Assembly GenerateFiles(string name, IEnumerable<string> files, Gen
NamingScheme = generatorPrototype.NamingScheme,
UseArrayItemAttribute = generatorPrototype.UseArrayItemAttribute,
EnumAsString = generatorPrototype.EnumAsString,
MapUnionToWidestCommonType = generatorPrototype.MapUnionToWidestCommonType
};

gen.CommentLanguages.Clear();
Expand Down
8 changes: 7 additions & 1 deletion XmlSchemaClassGenerator.Tests/XmlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,13 @@ public void TestClient()
[UseCulture("en-US")]
public void TestUnion()
{
var assembly = Compiler.Generate("Union", UnionPattern);
var assembly = Compiler.Generate("Union", UnionPattern, new Generator
{
NamespacePrefix = "Union",
IntegerDataType = typeof(int),
MapUnionToWidestCommonType = true
});

Assert.NotNull(assembly);

SharedTestFunctions.TestSamples(Output, "Union", UnionPattern);
Expand Down
2 changes: 1 addition & 1 deletion XmlSchemaClassGenerator/CodeUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public static Type GetEffectiveType(this XmlSchemaDatatype type, GeneratorConfig
{
var resultType = type.TypeCode switch
{
XmlTypeCode.AnyAtomicType => GetUnionType(configuration, schemaType, attribute), // union
XmlTypeCode.AnyAtomicType => configuration.MapUnionToWidestCommonType ? GetUnionType(configuration, schemaType, attribute) : typeof(string), // union
XmlTypeCode.AnyUri or XmlTypeCode.GDay or XmlTypeCode.GMonth or XmlTypeCode.GMonthDay or XmlTypeCode.GYear or XmlTypeCode.GYearMonth => typeof(string),
XmlTypeCode.Duration => configuration.NetCoreSpecificCode ? type.ValueType : typeof(string),
XmlTypeCode.Time => typeof(DateTime),
Expand Down
6 changes: 6 additions & 0 deletions XmlSchemaClassGenerator/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,12 @@ public CommandLineArgumentsProvider CommandLineArgumentsProvider
set { _configuration.CommandLineArgumentsProvider = value; }
}

public bool MapUnionToWidestCommonType
{
get { return _configuration.MapUnionToWidestCommonType; }
set { _configuration.MapUnionToWidestCommonType = value; }
}

static Generator()
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
Expand Down
8 changes: 7 additions & 1 deletion XmlSchemaClassGenerator/GeneratorConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public void WriteLog(string message)
public Action<CodeTypeDeclaration, TypeModel> TypeVisitor { get; set; }

/// <summary>
/// Provides options to customize Elementnamens with own logik
/// Provides options to customize Elementnamens with own logic
/// </summary>
public INamingProvider NamingProvider { get; set; }

Expand Down Expand Up @@ -328,5 +328,11 @@ public void WriteLog(string message)
/// for sequences with single elements. Default is true.
/// </summary>
public bool UseArrayItemAttribute { get; set; } = true;

/// <summary>
/// Tries to determine a common specific type for union member types, e.g. if a union has member types that are all integers
/// a numeric C# type is generated. If this is disabled, a union's type will default to string. Default is false.
/// </summary>
public bool MapUnionToWidestCommonType { get; set; }
}
}

0 comments on commit c60cc6e

Please sign in to comment.