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

Custom default values for fields #48

Open
bagrusss opened this issue Jul 5, 2017 · 3 comments
Open

Custom default values for fields #48

bagrusss opened this issue Jul 5, 2017 · 3 comments

Comments

@bagrusss
Copy link

bagrusss commented Jul 5, 2017

I have this message:

message SomeMessage {
    message SomeInnerMessage {
        optional string someField = 1 [default = "some_value"];
        optional string someOtherField = 2 [default = "some_other_value"];
        required string parameter1 = 3;
        required string parameter2 = 4;
    }
    ...
}

Compiler generates this code:

...
    public static final class SomeInnerMessage
            implements io.protostuff.Message<Request> {

        private static final Request DEFAULT_INSTANCE = newBuilder().build();

        private String someField;

        private String someOtherField;

        private String parameter1;

        private String parameter2;

        private boolean __merge_lock = false;
        private int __bitField0;

        private SomeInnerMessage() {
            this.someField = "";
            this.someOtherField = "";
            this.parameter1 = "";
            this.parameter2 = "";
        }
        ...
     }
...

There are neither default initializers for fields someField, someOtherField for private constructor nor for SomeInnerMessage.Builder private constructor. How to generate classes with default fields values?

@bagrusss bagrusss changed the title Compiler not generates default values for fields. CLI compiler not generates default values for fields. Jul 5, 2017
@bagrusss bagrusss changed the title CLI compiler not generates default values for fields. Compiler not generates default values for fields. Jul 5, 2017
@kshchepanovskyi
Copy link
Member

Actually it does. However, fields are not initialized in constructor directly.

There are 3 ways to create a message:

  1. Use Message.getDefaultInstance() - this one will be initialized with private constructor without parameters.
  2. Use Message.newBuilder().build() - this one will be initialized with builder's build() method.
  3. Use ProtobufIOUtil.mergeFrom(...) - this one will be initialized by schema, when it will finish reading all the data from input.

Please check full source of the generated class. It is similar to classes generated by google's protoc, but adjusted to work with protostuff library.

If you are not sure - I recommend you to write a unit test to double-check that there is default value, something like https://github.com/protostuff/protostuff-compiler/blob/master/protostuff-it/src/test/java/io/protostuff/it/java/ScalarTest.java or https://github.com/protostuff/protostuff-compiler/blob/master/protostuff-it/src/test/java/io/protostuff/it/java/MessageTest.java.

@bagrusss
Copy link
Author

bagrusss commented Jul 17, 2017

The default values ​​are (zero values, -1 for enums, and empty strings), but these are not the ones that one would like to have.
The Message.getDefaultInstance() and Message.newBuilder().build() ways not generates default values from .proto file. ProtobufIOUtil.mergeFrom(...) methods requires source with initialized fields. In the above tests, you initialize each field manually. I have this example message:

message LogoutClient {
    message Request {
        optional string path = 1 [default = "/logout"];
        optional string method = 2 [default = "delete"];
    }
    message Response {
    }
}

And generated code for this message. There are no default values ("/logout" and "delete" for fields path and method respectively) for fields in the Builder's and Message's constructors.

Can I create an object instance without having other initialized messages objects with default fields values from field's extensions default in the .proto file as from code that protoc generates?
Or CLI has limited functionality?

@bagrusss bagrusss changed the title Compiler not generates default values for fields. Compiler not generates default values for fields from fields extension. Jul 17, 2017
@bagrusss bagrusss changed the title Compiler not generates default values for fields from fields extension. Compiler not generates default values for fields from fields extension default. Jul 17, 2017
@bagrusss bagrusss changed the title Compiler not generates default values for fields from fields extension default. Compiler not generates default values for fields from field's extension default. Jul 17, 2017
@kshchepanovskyi
Copy link
Member

Sorry, I misinterpreted issue description.

You are right, custom default values are not supported. protostuff-generator ignores values for default option.

I cannot say when I can implement support for custom default values, as currently I'm quite busy with https://github.com/protostuff/protobuf-jetbrains-plugin.

If you are willing to help, I will gladly accept pull request.

@kshchepanovskyi kshchepanovskyi changed the title Compiler not generates default values for fields from field's extension default. Compiler does not use custom default values for fields Jul 17, 2017
@kshchepanovskyi kshchepanovskyi changed the title Compiler does not use custom default values for fields Custom default values for fields Jul 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants