thrift-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "James E. King III (JIRA)" <j...@apache.org>
Subject [jira] [Resolved] (THRIFT-4715) C# union "data" should be strongly-typed
Date Thu, 24 Jan 2019 17:30:00 GMT

     [ https://issues.apache.org/jira/browse/THRIFT-4715?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]

James E. King III resolved THRIFT-4715.
---------------------------------------
       Resolution: Fixed
    Fix Version/s: 0.13.0

Note: lib/csharp and lib/netcore are likely to be deprecated or dropped in 0.13.0 in favor
of netstd which would incorporate this fix.

> C# union "data" should be strongly-typed
> ----------------------------------------
>
>                 Key: THRIFT-4715
>                 URL: https://issues.apache.org/jira/browse/THRIFT-4715
>             Project: Thrift
>          Issue Type: Improvement
>          Components: C# - Compiler
>    Affects Versions: 0.12.0
>            Reporter: J W
>            Assignee: Jens Geyer
>            Priority: Minor
>             Fix For: 0.13.0
>
>          Time Spent: 20m
>  Remaining Estimate: 0h
>
> `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
> { 1: string url, }
> union RequestMsg
> { 1: PlayMsg Play, }
> ```
> Generates:
> {code:java}
> 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{code}
> Usage:
> {code:java}
> // RequestMsg message = ...
> switch (message)
> {
> case RequestMsg.Play msg:
>     // Need a cast here T_T
>     PlayMsg play = (PlayMsg)msg.Data;
> {code}
> 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`:
> {code:java}
> public class Play : RequestMsg{ 
>     public PlayMsg Data { get; private set; }
>     public Play(PlayMsg data) : base(true)
>     { this.Data = data; }
> //SNIP
> {code}
>  
>  
> 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?



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Mime
View raw message