Skip to content

Commit

Permalink
Minor refactoring in encodeValue function
Browse files Browse the repository at this point in the history
  • Loading branch information
ermolenkodev committed Aug 15, 2024
1 parent d4aa42f commit 2974f4d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,11 @@ internal fun encodeRowWithMetadata(
return JsonObject(values.toMap())
}

internal fun encodeValue(col: AnyCol, index: Int, customEncoders: List<CustomEncoder> = emptyList()): JsonElement =
when {
customEncoders.any { it.canEncode(col[index]) } -> {
customEncoders.first { it.canEncode(col[index]) }.encode(col[index])
}
internal fun encodeValue(col: AnyCol, index: Int, customEncoders: List<CustomEncoder> = emptyList()): JsonElement {
val matchingEncoder = customEncoders.firstOrNull { it.canEncode(col[index]) }

return when {
matchingEncoder != null -> matchingEncoder.encode(col[index])

col.isList() -> col[index]?.let { list ->
val values = (list as List<*>).map { convert(it) }
Expand All @@ -181,6 +181,7 @@ internal fun encodeValue(col: AnyCol, index: Int, customEncoders: List<CustomEnc

else -> JsonPrimitive(col[index]?.toString())
}
}

internal class DataframeConvertableEncoder(
private val encoders: List<CustomEncoder>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,11 @@ internal fun encodeRowWithMetadata(
return JsonObject(values.toMap())
}

internal fun encodeValue(col: AnyCol, index: Int, customEncoders: List<CustomEncoder> = emptyList()): JsonElement =
when {
customEncoders.any { it.canEncode(col[index]) } -> {
customEncoders.first { it.canEncode(col[index]) }.encode(col[index])
}
internal fun encodeValue(col: AnyCol, index: Int, customEncoders: List<CustomEncoder> = emptyList()): JsonElement {
val matchingEncoder = customEncoders.firstOrNull { it.canEncode(col[index]) }

return when {
matchingEncoder != null -> matchingEncoder.encode(col[index])

col.isList() -> col[index]?.let { list ->
val values = (list as List<*>).map { convert(it) }
Expand All @@ -181,6 +181,7 @@ internal fun encodeValue(col: AnyCol, index: Int, customEncoders: List<CustomEnc

else -> JsonPrimitive(col[index]?.toString())
}
}

internal class DataframeConvertableEncoder(
private val encoders: List<CustomEncoder>,
Expand Down
10 changes: 5 additions & 5 deletions docs/StardustDocs/snippets/manual/extensionPropertiesApi1.html
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,9 @@
</style>
</head>
<body>
<table class="dataframe" id="df_1291845901"></table>
<table class="dataframe" id="df_-1627389683"></table>

<table class="dataframe" id="static_df_1291845902"><thead><tr><th class="bottomBorder" style="text-align:left">example</th></tr></thead><tbody><tr><td style="vertical-align:top">123</td></tr></tbody></table>
<table class="dataframe" id="static_df_-1627389682"><thead><tr><th class="bottomBorder" style="text-align:left">example</th></tr></thead><tbody><tr><td style="vertical-align:top">123</td></tr></tbody></table>
<p class="dataframe_description"></p>
</body>
<script>
Expand Down Expand Up @@ -609,11 +609,11 @@

/*<!--*/
call_DataFrame(function() { DataFrame.addTable({ cols: [{ name: "<span title=\"example: Int\">example</span>", children: [], rightAlign: true, values: ["<span class=\"formatted\" title=\"\"><span class=\"numbers\">123</span></span>"] },
], id: 1291845901, rootId: 1291845901, totalRows: 1 } ) });
], id: -1627389683, rootId: -1627389683, totalRows: 1 } ) });
/*-->*/

call_DataFrame(function() { DataFrame.renderTable(1291845901) });
call_DataFrame(function() { DataFrame.renderTable(-1627389683) });

document.getElementById("static_df_1291845902").style.display = "none";
document.getElementById("static_df_-1627389682").style.display = "none";
</script>
</html>

0 comments on commit 2974f4d

Please sign in to comment.