Uploaded image for project: 'Thrift'
  1. Thrift
  2. THRIFT-4722

Netcore union "data" should be strongly-typed

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 0.12.0
    • 0.13.0
    • netcore - Compiler
    • None
    • Patch Available

    Description

      Following thrift:
      ```thrift
      struct PlayMsg

      { 1: string url, }

      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;
      }
      
      // SNIP
      
      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;
      

      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
      

       

      Attachments

        Issue Links

          Activity

            People

              jensg Jens Geyer
              jeikabu J W
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 20m
                  20m