elect
02/15/2024, 10:01 AMval tuples = receiveText()
.drop(1).dropLast(1)
.split(',')
.map { it.substringBefore(':') to it.substringAfter(':') }
to transform something like this
{"event_type":"pre-commit","event_time":"2024-02-15T095148Z","action_name":"Dataset","hook_id":"dataset_validator","repository_id":"quickstart","branch_id":"main","source_ref":"main","commit_message":"asd","committer":"quickstart"}into a
List<Pair<String, String>>
?hho
02/15/2024, 11:13 AM,
, your code would break. Using a real parser fixes that.elect
02/15/2024, 11:23 AMShawn
02/16/2024, 2:31 AMShawn
02/16/2024, 2:31 AMelect
02/16/2024, 7:04 AMelect
02/16/2024, 11:02 AMelect
02/16/2024, 11:36 AMbranchId
only if eventType
is not a specific enumelect
02/16/2024, 1:22 PMhho
02/16/2024, 2:56 PMimport com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
val json = ObjectMapper().findAndRegisterModules()
fun parseJson(input: String): List<Pair<String, String>> {
val parsed = json.readValue<MutableMap<String,String>>(input)
if (parsed["event_type"] != "pre-commit") { // whatever your condition here
parsed.remove("branch_id")
}
return parsed.toList()
}
fun main() {
println(parseJson("""{"event_type":"pre-commit","event_time":"2024-02-15T09:51:48Z","action_name":"Dataset","hook_id":"dataset_validator","repository_id":"quickstart","branch_id":"main","source_ref":"main","commit_message":"asd","committer":"quickstart"}"""))
}
(that is with jackson-databind
and jackson-module-kotlin
on the classpath).
However, what you're describing sounds more like you have different event_types which should be deserialized into different types in your app as well. With Jackson, that could look something like this:
import com.fasterxml.jackson.annotation.JsonSubTypes
import com.fasterxml.jackson.annotation.JsonTypeInfo
import <http://com.fasterxml.jackson.annotation.JsonTypeInfo.As|com.fasterxml.jackson.annotation.JsonTypeInfo.As>
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
val json = ObjectMapper().findAndRegisterModules()
fun parseJson(input: String): BaseEvent {
return json.readValue(input)
}
fun main() {
println(parseJson("""{"event_type":"pre-commit","event_time":"2024-02-15T09:51:48Z","action_name":"Dataset","hook_id":"dataset_validator","repository_id":"quickstart","branch_id":"main","source_ref":"main","commit_message":"asd","committer":"quickstart"}"""))
println(parseJson("""{"event_type":"something-else","event_time":"2024-02-15T09:51:48Z","action_name":"Foo","hook_id":"Bar","some_other_field":"Baz"}"""))
}
@JsonTypeInfo(include = As.PROPERTY, property = "event_type", use = JsonTypeInfo.Id.NAME)
@JsonSubTypes(
JsonSubTypes.Type(value = PreCommitEvent::class, name = "pre-commit"),
JsonSubTypes.Type(value = SomeOtherEvent::class, name = "something-else")
)
interface BaseEvent {
val event_time: String
val action_name: String
val hook_id: String
}
data class PreCommitEvent(
override val event_time: String,
override val action_name: String,
override val hook_id: String,
val repository_id: String,
val branch_id: String,
val source_ref: String,
val commit_message: String,
val committer: String
) : BaseEvent
data class SomeOtherEvent(
override val event_time: String,
override val action_name: String,
override val hook_id: String,
val some_other_field: String
) : BaseEvent
elect
02/16/2024, 3:37 PM{
"event_type": "pre-merge",
"event_time": "2021-02-28T14:03:31Z",
"action_name": "test action",
"hook_id": "prevent_user_columns",
"repository_id": "repo1",
"branch_id": "feature-1",
"source_ref": "feature-1",
"commit_message": "merge commit message",
"committer": "committer",
"commit_metadata": {
"key": "value"
}
}
I don't want to deserialize branch_id
(because it won't be there and it should be null
in the corresponding class) if event_type
is a specific value(s)
https://docs.lakefs.io/howto/hooks/webhooks.html#request-body-schemaShawn
02/21/2024, 6:38 PMnull
, so you don't really need to switch on event_type
elect
02/22/2024, 9:04 AMShawn
02/22/2024, 7:02 PMevent_type
is one of those specific values?elect
02/23/2024, 9:05 AMelect
02/23/2024, 9:06 AM