Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ import com.fasterxml.jackson.databind.BeanProperty
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.JavaType
import com.fasterxml.jackson.databind.JsonDeserializer
import com.fasterxml.jackson.databind.JsonMappingException
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.deser.ContextualDeserializer
import com.fasterxml.jackson.databind.deser.std.StdDeserializer
import com.openlayer.api.errors.OpenlayerInvalidDataException
import kotlin.reflect.KClass

abstract class BaseDeserializer<T : Any>(type: KClass<T>) :
Expand All @@ -30,38 +28,17 @@ abstract class BaseDeserializer<T : Any>(type: KClass<T>) :

protected abstract fun ObjectCodec.deserialize(node: JsonNode): T

protected fun <T> ObjectCodec.deserialize(node: JsonNode, type: TypeReference<T>): T =
protected fun <T> ObjectCodec.tryDeserialize(node: JsonNode, type: TypeReference<T>): T? =
try {
readValue(treeAsTokens(node), type)
} catch (e: Exception) {
throw OpenlayerInvalidDataException("Error deserializing", e)
}

protected fun <T> ObjectCodec.tryDeserialize(
node: JsonNode,
type: TypeReference<T>,
validate: (T) -> Unit = {},
): T? {
return try {
readValue(treeAsTokens(node), type).apply(validate)
} catch (e: JsonMappingException) {
null
} catch (e: RuntimeException) {
null
}
}

protected fun <T> ObjectCodec.tryDeserialize(
node: JsonNode,
type: JavaType,
validate: (T) -> Unit = {},
): T? {
return try {
readValue<T>(treeAsTokens(node), type).apply(validate)
} catch (e: JsonMappingException) {
null
} catch (e: RuntimeException) {
protected fun <T> ObjectCodec.tryDeserialize(node: JsonNode, type: JavaType): T? =
try {
readValue(treeAsTokens(node), type)
} catch (e: Exception) {
null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,34 @@ internal fun <K : Comparable<K>, V> SortedMap<K, V>.toImmutable(): SortedMap<K,
if (isEmpty()) Collections.emptySortedMap()
else Collections.unmodifiableSortedMap(toSortedMap(comparator()))

/**
* Returns all elements that yield the largest value for the given function, or an empty list if
* there are zero elements.
*
* This is similar to [Sequence.maxByOrNull] except it returns _all_ elements that yield the largest
* value; not just the first one.
*/
@JvmSynthetic
internal fun <T, R : Comparable<R>> Sequence<T>.allMaxBy(selector: (T) -> R): List<T> {
var maxValue: R? = null
val maxElements = mutableListOf<T>()

val iterator = iterator()
while (iterator.hasNext()) {
val element = iterator.next()
val value = selector(element)
if (maxValue == null || value > maxValue) {
maxValue = value
maxElements.clear()
maxElements.add(element)
} else if (value == maxValue) {
maxElements.add(element)
}
}

return maxElements
}

/**
* Returns whether [this] is equal to [other].
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,7 @@ private constructor(
mlModelId()
passingGoalCount()
projectId()
status()
status().validate()
statusMessage()
storageUri()
totalGoalCount()
Expand All @@ -817,6 +817,39 @@ private constructor(
validated = true
}

fun isValid(): Boolean =
try {
validate()
true
} catch (e: OpenlayerInvalidDataException) {
false
}

/**
* Returns a score indicating how many valid values are contained in this object recursively.
*
* Used for best match union deserialization.
*/
@JvmSynthetic
internal fun validity(): Int =
(if (id.asKnown().isPresent) 1 else 0) +
(commit.asKnown().getOrNull()?.validity() ?: 0) +
(if (dateArchived.asKnown().isPresent) 1 else 0) +
(if (dateCreated.asKnown().isPresent) 1 else 0) +
(if (failingGoalCount.asKnown().isPresent) 1 else 0) +
(if (mlModelId.asKnown().isPresent) 1 else 0) +
(if (passingGoalCount.asKnown().isPresent) 1 else 0) +
(if (projectId.asKnown().isPresent) 1 else 0) +
(status.asKnown().getOrNull()?.validity() ?: 0) +
(if (statusMessage.asKnown().isPresent) 1 else 0) +
(if (storageUri.asKnown().isPresent) 1 else 0) +
(if (totalGoalCount.asKnown().isPresent) 1 else 0) +
(if (trainingDatasetId.asKnown().isPresent) 1 else 0) +
(if (validationDatasetId.asKnown().isPresent) 1 else 0) +
(if (archived.asKnown().isPresent) 1 else 0) +
(if (deploymentStatus.asKnown().isPresent) 1 else 0) +
(links.asKnown().getOrNull()?.validity() ?: 0)

/** The details of a commit (project version). */
class Commit
private constructor(
Expand Down Expand Up @@ -1416,6 +1449,35 @@ private constructor(
validated = true
}

fun isValid(): Boolean =
try {
validate()
true
} catch (e: OpenlayerInvalidDataException) {
false
}

/**
* Returns a score indicating how many valid values are contained in this object
* recursively.
*
* Used for best match union deserialization.
*/
@JvmSynthetic
internal fun validity(): Int =
(if (id.asKnown().isPresent) 1 else 0) +
(if (authorId.asKnown().isPresent) 1 else 0) +
(if (fileSize.asKnown().isPresent) 1 else 0) +
(if (message.asKnown().isPresent) 1 else 0) +
(if (mlModelId.asKnown().isPresent) 1 else 0) +
(if (storageUri.asKnown().isPresent) 1 else 0) +
(if (trainingDatasetId.asKnown().isPresent) 1 else 0) +
(if (validationDatasetId.asKnown().isPresent) 1 else 0) +
(if (dateCreated.asKnown().isPresent) 1 else 0) +
(if (gitCommitRef.asKnown().isPresent) 1 else 0) +
(if (gitCommitSha.asKnown().isPresent) 1 else 0) +
(if (gitCommitUrl.asKnown().isPresent) 1 else 0)

override fun equals(other: Any?): Boolean {
if (this === other) {
return true
Expand Down Expand Up @@ -1549,6 +1611,33 @@ private constructor(
OpenlayerInvalidDataException("Value is not a String")
}

private var validated: Boolean = false

fun validate(): Status = apply {
if (validated) {
return@apply
}

known()
validated = true
}

fun isValid(): Boolean =
try {
validate()
true
} catch (e: OpenlayerInvalidDataException) {
false
}

/**
* Returns a score indicating how many valid values are contained in this object
* recursively.
*
* Used for best match union deserialization.
*/
@JvmSynthetic internal fun validity(): Int = if (value() == Value._UNKNOWN) 0 else 1

override fun equals(other: Any?): Boolean {
if (this === other) {
return true
Expand Down Expand Up @@ -1680,6 +1769,22 @@ private constructor(
validated = true
}

fun isValid(): Boolean =
try {
validate()
true
} catch (e: OpenlayerInvalidDataException) {
false
}

/**
* Returns a score indicating how many valid values are contained in this object
* recursively.
*
* Used for best match union deserialization.
*/
@JvmSynthetic internal fun validity(): Int = (if (app.asKnown().isPresent) 1 else 0)

override fun equals(other: Any?): Boolean {
if (this === other) {
return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,33 @@ private constructor(
OpenlayerInvalidDataException("Value is not a String")
}

private var validated: Boolean = false

fun validate(): Status = apply {
if (validated) {
return@apply
}

known()
validated = true
}

fun isValid(): Boolean =
try {
validate()
true
} catch (e: OpenlayerInvalidDataException) {
false
}

/**
* Returns a score indicating how many valid values are contained in this object
* recursively.
*
* Used for best match union deserialization.
*/
@JvmSynthetic internal fun validity(): Int = if (value() == Value._UNKNOWN) 0 else 1

override fun equals(other: Any?): Boolean {
if (this === other) {
return true
Expand Down Expand Up @@ -531,6 +558,33 @@ private constructor(
OpenlayerInvalidDataException("Value is not a String")
}

private var validated: Boolean = false

fun validate(): Type = apply {
if (validated) {
return@apply
}

known()
validated = true
}

fun isValid(): Boolean =
try {
validate()
true
} catch (e: OpenlayerInvalidDataException) {
false
}

/**
* Returns a score indicating how many valid values are contained in this object
* recursively.
*
* Used for best match union deserialization.
*/
@JvmSynthetic internal fun validity(): Int = if (value() == Value._UNKNOWN) 0 else 1

override fun equals(other: Any?): Boolean {
if (this === other) {
return true
Expand Down
Loading