[th2-2469] builders for parsed/raw messages and events#130
[th2-2469] builders for parsed/raw messages and events#130andrew-drobynin wants to merge 37 commits intomasterfrom
Conversation
b1a1566 to
354d0f3
Compare
354d0f3 to
3eb34a0
Compare
src/main/java/com/exactpro/th2/common/schema/factory/CommonFactory.java
Outdated
Show resolved
Hide resolved
src/main/kotlin/com/exactpro/th2/common/message/MessageBuilder.kt
Outdated
Show resolved
Hide resolved
src/main/kotlin/com/exactpro/th2/common/message/MessageBuilder.kt
Outdated
Show resolved
Hide resolved
src/main/kotlin/com/exactpro/th2/common/message/MessageBuilder.kt
Outdated
Show resolved
Hide resolved
src/main/kotlin/com/exactpro/th2/common/message/MessageBuilder.kt
Outdated
Show resolved
Hide resolved
| import com.google.protobuf.Timestamp | ||
| import java.time.Instant | ||
|
|
||
| abstract class MessageBuilder<T> { |
There was a problem hiding this comment.
Please try to implement type-safe-builders
https://kotlinlang.org/docs/type-safe-builders.html#full-definition-of-the-com-example-html-package
| fun addNullField(field: String): ParsedMessageBuilder { | ||
| this.fields[field] = Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build() | ||
| return this | ||
| } | ||
|
|
||
| fun addSimpleField(field: String, value: String): ParsedMessageBuilder { | ||
| this.fields[field] = Value.newBuilder().setSimpleValue(value).build() | ||
| return this | ||
| } |
There was a problem hiding this comment.
We should cover four types of values:
simple, message
list of simple, list of message
There was a problem hiding this comment.
By common.proto in th2-grpc-common it can be:
message Value {
oneof kind {
NullValue null_value = 1;
string simple_value = 2;
Message message_value = 3;
ListValue list_value = 4;
}
}
I added methods for Message and ListValue.
There was a problem hiding this comment.
factory.createMessage("session alias") {
metadata {
sequence = 3425
direction = FIRST
}
body {
"a" to "value",
"B" to message() {
body {
"c" to "value"
}
}
"D" to list [
message() { ... },
message() { ... }
],
"E" to list ["", "", ""]
}
}
There was a problem hiding this comment.
list[...] doesn't seem to be much different from built-in listOf(...), I would've prefer to use built-in collection builders where possible
There was a problem hiding this comment.
Also, I think we should not forget that the builder should work and be convenient in Java as well. The version above will be hard to use in Java. Also, it can be used only if we need to build a message right here. We can't imagine how we pass this builder as a parameter to another method.
We can achieve what you described above with Kotlin extension functions. I think we need to focus on the builder's API for Java
There was a problem hiding this comment.
Do we combine both approaches in one builder?
For example
java:
msg.metadata() // getter
.sequence(3425) // setter
.direction(FIRST) // setter
msg.body() // getter
.put("a", "value")
.put("B", factory.createMessage()
.body()
.put("c", "value"))
...
4d1be62 to
4edafbb
Compare
| public static Event start(IEventFactory eventFactory) { | ||
| return eventFactory.start(); | ||
| } |
There was a problem hiding this comment.
I don't think we need this method here at all. We can simply call IEventFactory.start() instead
There was a problem hiding this comment.
@andreydrobynin @Nikita-Smirnov-Exactpro what do you think about that?
There was a problem hiding this comment.
@OptimumOpium as I remember @Nikita-Smirnov-Exactpro asked to save back compatibility.
There was a problem hiding this comment.
It is a new method. How do we save backward compatibility here?
There was a problem hiding this comment.
Sorry, I meant we want to have a new method with same name as an old. I think @Nikita-Smirnov-Exactpro can clarify it correctly than me.
There was a problem hiding this comment.
From my point of view, it looks a bit strange. Why do I have to write Event.start(eventFactory) instead of eventFactory.start() in my code?
| public static Event from(Instant startTimestamp, IEventFactory eventFactory) { | ||
| return eventFactory.from(startTimestamp); | ||
| } |
There was a problem hiding this comment.
The same as for start method
There was a problem hiding this comment.
Here also back compatibility
src/main/java/com/exactpro/th2/common/schema/message/RabbitMqTest.java
Outdated
Show resolved
Hide resolved
|
|
||
| override fun from(startTimestamp: Instant, endTimestamp: Instant?) = from(startTimestamp, endTimestamp, this) | ||
|
|
||
| override fun from(startTimestamp: Instant, endTimestamp: Instant?, eventFactory: IEventFactory) = |
There was a problem hiding this comment.
Why do we need this method?
There was a problem hiding this comment.
@Nikita-Smirnov-Exactpro asked to add it. As I remember he wanted to have builder interface with old implementation (com.exactpro.th2.common.event.Event)
There was a problem hiding this comment.
I still don't understand why do we need a method in the factory that accepts the factory as one of the parameters. How is it connected to an old implementation?
src/main/kotlin/com/exactpro/th2/common/schema/configuration/ConfigurationManager.kt
Outdated
Show resolved
Hide resolved
a1b0faf to
b4c4eec
Compare
| protected static final ThreadLocal<ObjectMapper> OBJECT_MAPPER = ThreadLocal.withInitial(() -> new ObjectMapper().setSerializationInclusion(NON_NULL)); | ||
|
|
||
| protected final String id = generateUUID(); | ||
| protected String bookName; |
There was a problem hiding this comment.
I think we should make it final and set in the constructor
There was a problem hiding this comment.
@Nikita-Smirnov-Exactpro asked to have builder methods (e.g. bookName(), endTimestamp())
There was a problem hiding this comment.
I understand why we need the endTimestamp setter. But why do we need a setter for bookName? Why shouldn't we set it once in the constructor?
| public static Event start(IEventFactory eventFactory) { | ||
| return eventFactory.start(); | ||
| } |
There was a problem hiding this comment.
@andreydrobynin @Nikita-Smirnov-Exactpro what do you think about that?
No description provided.