Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve oneof support #40

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open

Commits on Jul 16, 2021

  1. Improve oneof support

    Given a message:
    
    ```
    message Foo {
      oneof timestamp {
        int64 created;
        int64 updated;
      }
    }
    ```
    
    Previously the generated classes by crystal-protoc-gen
    would allow to set multiple values of the oneof:
    
    ```
    foo.created = 123
    foo.updated = 345
    
    foo.created # => 123
    foo.updated # => 345
    ```
    
    With this patch the sibling oneof-values
    will be cleared when one is set:
    
    ```
    foo.created = 123
    foo.updated = 345
    
    foo.created # => nil
    foo.updated # => 345
    ```
    
    Also when deserializing a message that contains
    oneofs there was previously no way of knowing
    which oneof value is populated.
    
    With this patch we synthesize a new instance
    variable by the name of the oneof to contain
    the string-name of the member that is populated:
    
    ```
    foo.created = 123
    foo.updated = 345
    
    foo.created # => nil
    foo.updated # => 345
    foo.timestamp # => "updated"
    foo[foo.timestamp] # => 345
    ```
    m-o-e committed Jul 16, 2021
    Configuration menu
    Copy the full SHA
    82b2f40 View commit details
    Browse the repository at this point in the history
  2. Fix comment

    m-o-e committed Jul 16, 2021
    Configuration menu
    Copy the full SHA
    7587e68 View commit details
    Browse the repository at this point in the history

Commits on Jul 17, 2021

  1. Fix potential clash with reserved keywords

    Note:
    * This also changes the constructor semantics
      when multiple oneof-values are given (now the *first*
      value is kept instead of the last)
    
    * Using reserved keywords as protobuf field-names remains
      a bad idea regardless. For example a field called `delete`
      will break ts-proto / protoc-gen-js and probably others
    m-o-e committed Jul 17, 2021
    Configuration menu
    Copy the full SHA
    8035bd5 View commit details
    Browse the repository at this point in the history
  2. Avoid double-negative

    m-o-e committed Jul 17, 2021
    Configuration menu
    Copy the full SHA
    53df591 View commit details
    Browse the repository at this point in the history
  3. Configuration menu
    Copy the full SHA
    0c22b97 View commit details
    Browse the repository at this point in the history
  4. Call setters after deserializing

    Previously deserializing a message containing `oneofs`
    would not set the oneof instance var. E.g. you would get:
    
    `{ oneof_member1: 1, oneof_member_2: nil, my_oneof: nil }`
    
    Now it works as intended and you get:
    
    `{ oneof_member1: 1, oneof_member_2: nil, my_oneof: "oneof_member1" }`
    m-o-e committed Jul 17, 2021
    Configuration menu
    Copy the full SHA
    4e8c566 View commit details
    Browse the repository at this point in the history
  5. Configuration menu
    Copy the full SHA
    ca66c71 View commit details
    Browse the repository at this point in the history

Commits on Jul 23, 2021

  1. Add direct getter for oneof value

    `myobj.myoneof_value` is more convenient than `myobj[myobj.myoneof]`
    esp. when dealing with nested objects.
    m-o-e committed Jul 23, 2021
    Configuration menu
    Copy the full SHA
    83296aa View commit details
    Browse the repository at this point in the history

Commits on Jul 24, 2021

  1. Restrict return type of oneof-by-name getter

    The return-type of `myobj.myoneof_value` is now restricted
    to the types of the possible oneof-values. Previously it would
    include the types of *all* message-fields (even those that
    are not part of the queried oneof).
    m-o-e committed Jul 24, 2021
    Configuration menu
    Copy the full SHA
    93f3afc View commit details
    Browse the repository at this point in the history
  2. Fix return type restriction (String/Symbol mixup)

    Now it should actually work. ;)
    
    Note: Resist the temptation to store ONEOF as Symbol's.
    It appears to work at first and saves some String-conversions
    but wreaks havoc when JSON::Serializable comes into play.
    m-o-e committed Jul 24, 2021
    Configuration menu
    Copy the full SHA
    8ae57a8 View commit details
    Browse the repository at this point in the history

Commits on Jul 26, 2021

  1. Configuration menu
    Copy the full SHA
    2f015e4 View commit details
    Browse the repository at this point in the history