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

Extensive (is) option #248

Merged
merged 16 commits into from Dec 11, 2018
Merged

Extensive (is) option #248

merged 16 commits into from Dec 11, 2018

Conversation

dmdashenkov
Copy link
Contributor

This PR introduces an improvement to the message marker interface declaration. This PR fixes core-java/#911.

Summary

Previously, the only possibility to mark a message with a marker interface was to generate the interface with the help of protoc itself.

From now on, it is possible for marker interfaces to be handcrafted, instead of generated.

Handcrafted Interfaces

In order to declare that a message Java class should implement an interface, the user should annotate it with (is) option as follows:

message Student {
    option (is).java_type = "org.example.customer.FreemiumCustomer";

    string institution_name = 1;
    // ...
}

message OpenSourceCustomer {
    option (is).java_type = "org.example.customer.FreemiumCustomer";

    string company_name = 1;
    // ...
}

Now, in Java, declare org.example.customer.FreemiumCustomer interface:

package org.example.customer;

public interface FreemiumCustomer {

    default String getInstitutionOrCompanyName() {
        // Use Java or Protobuf reflection to obtain the value.
    }
}

The interface may have any methods and/or extend any other interfaces as long as it does not introduce new abstract methods to the message class. If the method is already present in the end class or has a default implementation, it is allowed.

Generated Interfaces

In order to generate the marker interface, the (is).generate option field should be set to true (the default value is false):

message Rgb {
    option (is) = {java_type: "Color", generate: true};

   // ...
}

message Cmyk {
    option (is).java_type = "Color";

   // ...
}

Note that it is sufficient to specify generate = true only once. This detail may be omitted on further message declarations. However, it is OK to specify to generate the interface on all the messages.

(every_is) Option

The (every_is) option is a "brother" of the (is) option targeting an entiere file instead of a single message type. The API of (every_is) is the same as the API of (is).

@dmdashenkov dmdashenkov self-assigned this Dec 11, 2018
@codecov
Copy link

codecov bot commented Dec 11, 2018

Codecov Report

Merging #248 into master will increase coverage by 0.03%.
The diff coverage is 91.3%.

@@             Coverage Diff              @@
##             master     #248      +/-   ##
============================================
+ Coverage     64.55%   64.59%   +0.03%     
- Complexity      575      579       +4     
============================================
  Files           309      309              
  Lines          8098     8106       +8     
  Branches        557      559       +2     
============================================
+ Hits           5228     5236       +8     
  Misses         2758     2758              
  Partials        112      112

@dmdashenkov
Copy link
Contributor Author

@armiol, PTAL.

Copy link
Collaborator

@armiol armiol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dmdashenkov LGTM. Let's revert the changes to Gradle binaries in the root. And address a comment before merging.

//
// A generated interface has no declared methods and extends `com.google.protobuf.Message`.
// The `.java` file is placed alongside with the code generated by the proto-to-java compiler.
// If specified with an FQN, the package of the generated type matches the FQN.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sentence seems to be a new paragraph, as it's about how to reference a type.

@dmdashenkov dmdashenkov merged commit 96d59c8 into master Dec 11, 2018
@dmdashenkov dmdashenkov deleted the extensive-is-option branch December 11, 2018 16:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add extensive IsOption to support opt-in generation of marker interfaces
2 participants