https://kotlinlang.org logo
#vertx
Title
f

fstn

10/19/2017, 7:05 AM
@dave08 this is what I’m doing :
Copy code
object JsonObjectUtils{
     inline operator fun JsonObject.set(key: String, value: Any): Unit {
        this.put(key,value)
    }
    inline operator fun <T> JsonObject.get(key: String): T {
        return this.getValue(key) as T
    }
    fun  JsonObject.getFlow(): JsonObject {
        return this.getJsonObject("flow")
    }
    fun  JsonObject.getFlowNodeId(): String {
        return this.getFlow().get("nodeId")
    }
    fun  JsonObject.setFlowNodeId(nodeId:String) {
        this.getFlow().put("nodeId",nodeId)
    }
    fun  JsonObject.getFlowName(): String {
        return this.getFlow().get("name")
    }
    fun  JsonObject.setFlowName(nodeId:String) {
        this.getFlow().put("name",nodeId)
    }
}
d

dave08

10/19/2017, 7:10 AM
@fstn How does this simplify your eventbus code?
f

fstn

10/19/2017, 7:12 AM
instead of doing
Copy code
message.body().getJsonObject("flow").getValue("nodeId")
I can use
Copy code
message.body().getFlowNodeId()
That I want If to use a dynamic object for the event bus message
but with the ability to get known value like property
d

dave08

10/19/2017, 7:18 AM
@fstn Personally, I use the JsonObject itself to provide the parameters for my services that are in the Business Logic Layer of my app. The code retreiving and setting values to/from the bus should only be in one place, and then be delegated to other layers of the app using data classes or function parameters. If you only use this Utils class in one place it might be a waste to maintain it, you might be better off converting incoming messages to data classes to be used in other layers of the app. I hope I understand you properly... 😉
f

fstn

10/19/2017, 7:24 AM
mmm I’m not sure to understand, sorry 😞 my body can be :
Copy code
{ 
    “user” : 
    {   
        “firstName” : “MyFirstName”
    }
    “flow”:
    {
        “id”:“myId”,
        “name”:“myName”
    }
}
or
Copy code
{
    "user" :
    {
        "firstName" : "MyFirstName",
        "dynamicProperty": "myDynamicProperty"
    }
    "flow":
    {
    }
}
so to do that you declare a data object context with 2 properties user and flow that are JsonObject?
and you get it with for example context.user
d

dave08

10/19/2017, 7:26 AM
No, it depends on what input your logic needs. You need to convert it in a form you can easily process
Json is too flexible and might introduce errors of unexpected non existant values or formats
f

fstn

10/19/2017, 7:28 AM
so How do you do a class this a dynamic part?
d

dave08

10/19/2017, 7:28 AM
Whereas if you box it into a data class, it'll give you an error as to exactly what is missing from what the logic is expecting
Dynamic is always converted to something to process -- code is not dynamic
It's always expecting something specific...
f

fstn

10/19/2017, 7:29 AM
so the solution can Be to have class User (val firstName:String,val lastName:String,val dynamicPart:JsonObject{
d

dave08

10/19/2017, 7:30 AM
Try to rethink if you really need the dynamic and if it needs processing by your code - if it is just passed along you could even keep it as a json string...
f

fstn

10/19/2017, 7:30 AM
It’s really dynamic, the dynamic part is save inside a jsonB column
and it can be consume by others app
d

dave08

10/19/2017, 7:31 AM
But does it need processing?
f

fstn

10/19/2017, 7:31 AM
yes because it’s for a workflow, user can modify the json inside the workflow
sorry 😞
d

dave08

10/19/2017, 7:32 AM
User using the UI, but does your code touch it or just save as is in the db? and retrieve for the user to see
If so, you could even use a
val flow: String
, if not, you might think to make it into some State Machine
Good luck!
f

fstn

10/19/2017, 7:34 AM
thanks @dave08 💪
4 Views