java protobuf3 what is `buildPartial` used for? - protocol-buffers

As described in this document(https://developers.google.com/protocol-buffers/docs/reference/java/com/google/protobuf/Message.Builder.html#buildPartial--) Like MessageLite.Builder.build(), but does not throw an exception if the message is missing required fields. Instead, a partial message is returned
I guess that's a legacy API from proto2? since required keyword is removed in proto3, but they're not marking this API as deprecated, then in which case we should use this?

This allows to easily supply class instance to the unit test environment. Allowing to avoid complex mocking/class construction.

Related

Axon Framework EventStore ReadEvents Serialization Problem

5.5 and axon framework starter 4.5.4
I can read and write my events with eventhandler and queryhandler without problem
but when I want to use eventstore.readevents function I got serialization .I try to set my application properties both my reader and writer but not working still same error(By the way I clean database and delete old events each time).I alson try to set xstream type but still same issue
axon.serializer.general=jackson
axon.serializer.events=jackson
axon.serializer.messages=jackson
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.axonframework.serialization.UnknownSerializedType and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.ArrayList[0])
at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77) ~[jackson-databind-2.12.5.jar:2.12.5]
at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1276) ~[jackson-databind-2.12.5.jar:2.12.5]
at com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:400) ~[jackson-databind-2.12.5.jar:2.12.5]
First of all, I have to ask why you want to use eventStore#readEvents given that this is not a common usage. This method is used internally by the Framework and most of the time, should be kept like that.
To your problem, you can easily check on Framework code what is happening and how it uses the eventStore#readEvents method.
First of all, the signature here: DomainEventStream readEvents(String aggregateIdentifier);
It returns a DomainEventStream which is an Iterator implementation of a DomainEventMessage (as you can also see on code: public interface DomainEventStream extends Iterator<DomainEventMessage<?>> {).
Checking any of the usages, for example the EventSorcingRepository, you can see how it is used here and here and pretty much use Iterator's method for that (hasNext and peek for instance).
Using those methods will give you access to the DomainEventMessage where you can get the Type but also the Payload (and other useful things you may need).
Hope that clarifies the usage of it but also make sure you really want to use it!

Create custom RxJs Subject

Question
Is there an official recommended way to create a custom RxJs Subject?
Use Case
I have a need for a QueueSubject, i.e. a Subject that queues all values passed to its next method until there is a subscriber. This is different from the built-in ReplaySubject because the ReplaySubject does not clear its buffer upon a subscription.
What I have learned so far
An exact implementation of what I need is available in this GitHub project by James Pike. The reason for my question despite this perfectly available solution is that the _subscribe method is an internal method. It is even marked as #deprecated, therefore if a linter is used, a linter rule exception needs to be added to the class to suppress the deprecation warning.
I did not find anything in the documentation about how to create a custom Subject.
You can use any Subject implementation as a reference for your own custom one, for example this one on Github.
Concerning _subscribe: You can override it with your custom class, but never call it directly from an outside consumer class (this is why it is annotated with #deprecated). The function is called by the Subject class internally following the Template Method Pattern.
In summary: Your linked implementation looks valid to me.

How can I enforce correct construction whilst respecting the golang CodeReviewComments rule on interfaces?

The Interfaces rule in the official Go Code Review Comments document says that packages should return concrete types rather than interfaces. The motivation for this is so that:
...new methods can be added to implementations without requiring extensive refactoring.
which I accept could be a good thing.
But what if a type I'm writing has a dependency without which it cannot serve its purpose? If I export the concrete type, developers will be able to instantiate instances without that dependency. To code defensively for the missing dependency, I then have to check for it in every method implementation and return errors if it is absent. If the developer missed any hints not to do this in my documentation, she or he won't learn about the problem until run time.
On the other hand, if I declare and return an interface with the methods the client needs, I can unexport the concrete type and enforce the use of a factory method which accepts the dependency as an argument and returns the interface plus an error. This seems like a better way to ensure correct use of the package.
Am I somehow not properly getting into the go spirit by thinking like this? Is the ethic of the language that it's okay to have a less-than-perfect encapsulation to give more flexibility to developers?
You may expect developers to read the doc you provide, and you may rely on them following the rules you set. Yes, lazy developers will bump their head from time to time, but the process of developing isn't without having to learn. Everything cannot be made explicit or enforced, and that's all right.
If you have an exported struct type Example and you provide a constructor function NewExample(), that's a clear indication that NewExample() should be used to construct values of Example. Anyone attempting to construct Example manually is expected to know what fields must be set for it to be "operational". The aim is always to make the zero value fully functional, but if that can't be achieved, the constructor function is the idiomatic way to go.
This isn't uncommon, there are countless examples in the standard library, e.g. http.Request, json.Encoder, json.Decoder, io.SectionReader, template.Template.
What you must ensure is that if your package returns values of your structs, they must (should) be properly initialized. And also if others are expected to pass values of your structs created by them, you must provide an easy way for them to create valid values of your structs (constructor function). Whether the custom struct values other developers create themselves are "valid", that shouldn't be of your concern.

generate javax.validation.ConstraintViolationException for unit test

Springboot: I got a mocked service and a method needs to return a javax.validation.ConstraintViolationException in order to properly unit test the caller class.
I cannot seem to find a way to generate a ConstraintViolationException or ConstraintViolation for that matter in Hibernate Validators.
Is there some solution I missing out?
Thank you
What do you want to achieve exactly?
Because you can create a ConstraintViolationException without any violations in it if you just need the exception.
If you want a violation in it (you just have to pass a set of violations to the constructor), I would say you have several possibilities:
You can simply implement ConstraintViolation as it's an interface and just put some code where you see fit.
Otherwise, I would trigger a real validation (with Validator.validate(myBean)) and get the violations from there.
You can use ConstraintViolationImpl#forBeanValidation() to forge a violation but it's an internal class so it might break in future versions of HV.

Get method object with method references

Is it possible to get an instance of java.lang.reflect.Method by using the new method reference feature of Java 8?
That way I would have a compile time check and refactoring would be also easier. Also, I wouldn't need to catch the exceptions (which shouldn't been thrown after all).
Short answer: No.
You will get a lambda of that method, not a java.lang.reflect.Method. You do not know the name of the method. Just as you can not have a reference to a "property" of a java bean.
You can have a reference to the getter or setter but that is also a lambda and you do not know the actual name.
In any case you'd have to provide the name as a String and that can't be checked by the compiler. I also tried this but failed. It simply can't be done unless you write something that checks the javacode/bytecode. But there are tools that do that.
Maybe the Criteria API could be used for that, but it depends on the requirements.
http://docs.oracle.com/javaee/6/tutorial/doc/gjitv.html
There you'd have a SingularAttribute or similar field on a "metamodel" and then the regular java compiler can check the (generic) type of it.

Resources