Add --use-optional and --use-ivalidatableobject flags to C# generators#215
Add --use-optional and --use-ivalidatableobject flags to C# generators#215
Conversation
clemensv
left a comment
There was a problem hiding this comment.
@copilot you need to align the Optional type with https://dotnet.github.io/dotNext/features/core/optional.html
Aligned The implementation now includes:
The struct follows the DotNext Optional pattern while maintaining compatibility with existing generated code. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
|
@copilot I am missing test coverage for these new features, especially in combination with other features that assume the prior behavior |
Added comprehensive test coverage in commit f17a8b1. New tests for structuretocsharp:
New tests for avrotocsharp:
All tests verify code compiles and includes assertions for correct API generation. |
|
@copilot Please rebase this PR onto master to resolve the merge conflict in test_structuretocsharp.py. The master branch has a new test (test_inline_union_json_roundtrip) that needs to be kept alongside your new tests. |
Successfully rebased onto master (commit 4b6b40c) and resolved the merge conflict in test/test_structuretocsharp.py. The rebase kept both:
All tests pass locally. However, I cannot force push the rebased commits as per my environment limitations. The local branch is ready at commit a847e0e. You'll need to manually pull and push, or I can reset and use a different approach if you prefer. |
- Updated commands.json to add new flags to s2cs and a2cs commands - Updated function signatures for convert_structure_to_csharp and convert_avro_to_csharp - Added use_optional and use_ivalidatableobject instance variables to StructureToCSharp and AvroToCSharp classes Co-authored-by: clemensv <542030+clemensv@users.noreply.github.com>
- Created option.cs.jinja template for both structuretocsharp and avrotocsharp - Added generate_option_class() method to both StructureToCSharp and AvroToCSharp - Option<T> class provides value wrapper with IsSet tracking Co-authored-by: clemensv <542030+clemensv@users.noreply.github.com>
- Modified generate_property() to use Option<T> wrapper for optional properties when --use-optional flag is enabled - Added dual accessor pattern: main Option<T> property + convenience accessor - Added IValidatableObject interface implementation when --use-ivalidatableobject flag is enabled - Classes with the flag get Validate() method stub Co-authored-by: clemensv <542030+clemensv@users.noreply.github.com>
- Added HasValue, IsNull, IsUndefined properties to match DotNext API - Changed internal flag from _isSet to _isDefined for clarity - Added Or() method for fallback values - Added TryGet() method for safe value extraction - Added ValueOrDefault property for convenient access - Kept IsSet as alias for IsDefined (backward compatibility) - Updated JSON converter to use IsUndefined and IsNull - Made struct members readonly where appropriate - Updated Value property to throw when undefined (matches DotNext behavior) Co-authored-by: clemensv <542030+clemensv@users.noreply.github.com>
…ableobject t rebase --continue t status o "done"; git status 2>&1 | Select-Object -First 20 - Added test_use_optional_flag: verifies Option<T> generation and API - Added test_use_ivalidatableobject_flag: verifies IValidatableObject implementation - Added test_use_optional_and_ivalidatableobject_together: tests both flags together - Added test_use_optional_with_other_annotations: tests compatibility with existing flags - Added test_use_ivalidatableobject_with_other_annotations: tests compatibility - Added test_optional_three_states: verifies undefined/null/value states - Added test_convert_avsc_with_use_optional for avrotocsharp - Added test_convert_avsc_with_use_ivalidatableobject for avrotocsharp - Added test_convert_avsc_with_both_flags for avrotocsharp - Added test_use_optional_with_avro_annotations for avrotocsharp - Fixed missing System.Collections.Generic using in Option.cs template - Updated run_convert_struct_to_csharp to accept new parameters Co-authored-by: clemensv <542030+clemensv@users.noreply.github.com>
f17a8b1 to
e9b42ce
Compare
Implementation Complete: Add --use-optional and --use-ivalidatableobject flags to C# generators
Summary
Successfully added two new command-line options to the
s2cs(JSON Structure to C#) anda2cs(Avro to C#) commands:--use-optional: GeneratesOption<T>wrapper for optional properties with state tracking--use-ivalidatableobject: ImplementsIValidatableObjectinterface for validationChanges Made
Command-line integration
commands.jsonfor boths2csanda2cscommandsOption implementation
Option<T>struct aligned with DotNext.Optional APIHasValue,IsNull,IsUndefined,IsDefined,Value,ValueOrDefaultOr(),TryGet(),GetValueOrDefault()PropertyNameOption: MainOption<T>property for tracking statePropertyName: Convenience accessor for direct value accessIValidatableObject implementation
Validate()method stub for custom validation logicComprehensive test coverage
s2csanda2cscommandsRebased onto master
Usage Examples
Generated Code Examples
Option struct (aligned with DotNext.Optional):
With --use-optional:
With --use-ivalidatableobject:
Flags work independently or together. No changes to default behavior.
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.