Details
-
Improvement
-
Status: Closed
-
Minor
-
Resolution: Fixed
-
0.12.0
-
None
-
Patch Available
Description
`union` option for netcore generator was fixed in 0.12, but what it generates doesn't seem very user-friendly.
Following thrift:
```thrift
struct PlayMsg
union RequestMsg
{ 1: PlayMsg Play, }```
Generates:
public abstract partial class RequestMsg : TAbstractBase { public abstract void Write(TProtocol protocol); public readonly bool Isset; public abstract object Data { get; } protected RequestMsg(bool isset) { Isset = isset; } public class ___undefined : RequestMsg { public override object Data { get { return null; } } public ___undefined() : base(false) {} public override void Write(TProtocol protocol) { throw new TProtocolException( TProtocolException.INVALID_DATA, "Cannot persist an union type which is not set."); } } public class Play : RequestMsg { private PlayMsg _data; public override object Data { get { return _data; } } public Play(PlayMsg data) : base(true) { this._data = data; } //SNIP
Usage:
// RequestMsg message = ... switch (message) { case RequestMsg.Play msg: // Need a cast here T_T PlayMsg play = (PlayMsg)msg.Data;
Is there a reason for the `public abstract object Data`?
If we get rid of that and instead generate a strongly-type getter we don't need to cast `Data`:
public class Play : RequestMsg{ public PlayMsg Data { get; private set; } public Play(PlayMsg data) : base(true) { this.Data = data; } //SNIP
I could have sworn it worked like that with the "csharp" generator in 0.11.0 but it generates the same now.
Is the intended usage different from what I'm doing?
Attachments
Issue Links
- Dependency
-
THRIFT-1786 C# Union Typing
- Closed
- is related to
-
THRIFT-4722 Netcore union "data" should be strongly-typed
- Closed
- links to