DateOnly and TimeOnly implementation
Walkthrough - Implement DateOnly/TimeOnly Support
I have implemented support for generating System.DateOnly and System.TimeOnly types for xs:date and xs:time XML schema types.
Changes
1. GeneratorConfiguration.cs and Generator.cs
Added a new configuration property UseDateOnly.
// GeneratorConfiguration.cs
public bool UseDateOnly { get; set; } = false;
// Generator.cs
public bool UseDateOnly
{
get { return _configuration.UseDateOnly; }
set { _configuration.UseDateOnly = value; }
}
2. CodeUtilities.cs
- Added internal
DateOnlyandTimeOnlystructs to allow the generator to identify these types even when running on older .NET frameworks (netstandard2.0). - Updated
GetEffectiveTypeto mapXmlTypeCode.DateandXmlTypeCode.Timeto these internal types whenUseDateOnlyis true. - Updated
CreateTypeReferenceto map the internal types toSystem.DateOnlyandSystem.TimeOnlystring references in the generated code.
3. TypeModel.cs
Updated GetDefaultValueFor to handle standard default value parsers for DateOnly and TimeOnly (using .Parse()).
4. Program.cs
Added a new command line option --dateOnly (or -do) to the CLI.
xscgen -do schema.xsd
Verification
I added a new test file XmlSchemaClassGenerator.Tests/DateOnlyTimeOnlyTests.cs and confirmed that:
- When
-dois enabled,xs:dategeneratespublic System.DateOnly PropertyName. - When
-dois enabled,xs:timegeneratespublic System.TimeOnly PropertyName. - When disabled, it falls back to
DateTime. - Verified that default values for
xs:dateandxs:timeare correctly parsed usingSystem.DateOnly.ParseandSystem.TimeOnly.Parse. - Verified that
xs:timeandxs:datecorrectly map toDateTimeOffsetwhenUseDateOnlyis false andDateTimeWithTimeZoneis true.
Usage
To use the new feature via CLI:
xscgen -do input.xsd
To use via code:
var generator = new Generator
{
UseDateOnly = true
};
generator.Generate(schemaSet);
(implemented by Gemini Pro 3)
This should fix #524 and #310
Codecov Report
:x: Patch coverage is 95.74468% with 2 lines in your changes missing coverage. Please review.
:white_check_mark: Project coverage is 94.27%. Comparing base (6f928ee) to head (78ff980).
:warning: Report is 8 commits behind head on master.
| Files with missing lines | Patch % | Lines |
|---|---|---|
| XmlSchemaClassGenerator/Generator.cs | 85.71% | 1 Missing :warning: |
| XmlSchemaClassGenerator/TypeModel.cs | 96.55% | 0 Missing and 1 partial :warning: |
Additional details and impacted files
@@ Coverage Diff @@
## master #573 +/- ##
==========================================
+ Coverage 94.24% 94.27% +0.02%
==========================================
Files 21 21
Lines 3216 3232 +16
Branches 509 516 +7
==========================================
+ Hits 3031 3047 +16
Misses 123 123
Partials 62 62
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
@copilot open a new pull request to apply changes based on the comments in this thread
@Lanayx Thanks!