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

Implement reading from null safe dereferences #21239

Merged
merged 1 commit into from
Nov 9, 2016

Commits on Nov 9, 2016

  1. Implement reading from null safe dereferences

    Null safe dereferences make handling null or missing values shorter.
    Compare without:
    ```
    if (ctx._source.missing != null && ctx._source.missing.foo != null) {
      ctx._source.foo_length = ctx.source.missing.foo.length()
    }
    ```
    
    To with:
    ```
    Integer length = ctx._source.missing?.foo?.length();
    if (length != null) {
      ctx._source.foo_length = length
    }
    ```
    
    Combining this with the as of yet unimplemented elvis operator allows
    for very concise defaults for nulls:
    ```
    ctx._source.foo_length = ctx._source.missing?.foo?.length() ?: 0;
    ```
    
    Since you have to start somewhere, we started with null safe dereferenes.
    
    Anyway, this is a feature borrowed from groovy. Groovy allows writing to
    null values like:
    ```
    def v = null
    v?.field = 'cat'
    ```
    And the writes are simply ignored. Painless doesn't support this at this
    point because it'd be complex to implement and maybe not all that useful.
    
    There is no runtime cost for this feature if it is not used. When it is
    used we implement it fairly efficiently, adding a jump rather than a
    temporary variable.
    
    This should also work fairly well with doc values.
    nik9000 committed Nov 9, 2016
    Configuration menu
    Copy the full SHA
    d03b8e4 View commit details
    Browse the repository at this point in the history