Deprecated Kotlin json builder

This commit is contained in:
pkurchatov
2023-05-31 14:56:51 +03:00
parent 5c46bc9f71
commit db9863464c
18 changed files with 78 additions and 98 deletions
View File
@@ -1,6 +1,3 @@
// Copyright (c) 2022 Yandex LLC. All rights reserved.
// Author: Anton Gulevsky <gulevsky@yandex-team.ru>.
package com.yandex.div.dsl
sealed class Property<out T>
@@ -1,6 +1,3 @@
// Copyright (c) 2022 Yandex LLC. All rights reserved.
// Author: Anton Gulevsky <gulevsky@yandex-team.ru>.
package com.yandex.div.dsl
/**
@@ -1,6 +1,3 @@
// Copyright (c) 2022 Yandex LLC. All rights reserved.
// Author: Anton Gulevsky <gulevsky@yandex-team.ru>.
package com.yandex.div.dsl.context
import com.fasterxml.jackson.annotation.JsonIgnore
@@ -11,6 +8,7 @@ import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
@Deprecated("Use divkit.dsl framework")
class CardContext {
@JvmField
@@ -27,6 +25,7 @@ class CardContext {
}
@OptIn(ExperimentalContracts::class)
@Deprecated("Use divkit.dsl framework")
fun card(
initialize: CardContext.() -> Root
): CardContext {
@@ -4,4 +4,5 @@ import com.fasterxml.jackson.annotation.JsonInclude
import com.yandex.div.dsl.model.Div
@JsonInclude(JsonInclude.Include.NON_NULL)
@Deprecated("Use divkit.dsl framework")
data class CardWithTemplates(val card: CardContext, val templates: TemplateContext<Div>? = null)
@@ -1,6 +1,3 @@
// Copyright (c) 2022 Yandex LLC. All rights reserved.
// Author: Anton Gulevsky <gulevsky@yandex-team.ru>.
package com.yandex.div.dsl.context
import com.fasterxml.jackson.annotation.JsonAnyGetter
@@ -12,6 +9,7 @@ import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
@Deprecated("Use divkit.dsl framework")
class TemplateContext<T> {
@JvmField
@@ -42,6 +40,7 @@ class TemplateContext<T> {
}
@OptIn(ExperimentalContracts::class)
@Deprecated("Use divkit.dsl framework")
fun <T> templates(initialize: TemplateContext<T>.() -> Unit): TemplateContext<T> {
contract {
callsInPlace(initialize, InvocationKind.EXACTLY_ONCE)
@@ -1,6 +1,3 @@
// Copyright (c) 2022 Yandex LLC. All rights reserved.
// Author: Anton Gulevsky <gulevsky@yandex-team.ru>.
package com.yandex.div.dsl.context
import com.yandex.div.dsl.Property
@@ -1,6 +1,3 @@
// Copyright (c) 2022 Yandex LLC. All rights reserved.
// Author: Anton Gulevsky <gulevsky@yandex-team.ru>.
package com.yandex.div.dsl.serializer
import com.fasterxml.jackson.core.JsonGenerator
@@ -1,6 +1,3 @@
// Copyright (c) 2022 Yandex LLC. All rights reserved.
// Author: Anton Gulevsky <gulevsky@yandex-team.ru>.
package com.yandex.div.dsl.serializer
import com.fasterxml.jackson.core.JsonGenerator
@@ -1,6 +1,3 @@
// Copyright (c) 2022 Yandex LLC. All rights reserved.
// Author: Anton Gulevsky <gulevsky@yandex-team.ru>.
package com.yandex.div.dsl.serializer
import com.fasterxml.jackson.annotation.JsonInclude
@@ -1,6 +1,3 @@
// Copyright (c) 2022 Yandex LLC. All rights reserved.
// Author: Anton Gulevsky <gulevsky@yandex-team.ru>.
package com.yandex.div.dsl.type
import com.fasterxml.jackson.databind.annotation.JsonSerialize
@@ -1,6 +1,3 @@
// Copyright (c) 2022 Yandex LLC. All rights reserved.
// Author: Anton Gulevsky <gulevsky@yandex-team.ru>.
package com.yandex.div.dsl.type
import com.fasterxml.jackson.databind.annotation.JsonSerialize
@@ -1,6 +1,3 @@
// Copyright (c) 2022 Yandex LLC. All rights reserved.
// Author: Anton Gulevsky <gulevsky@yandex-team.ru>.
package com.yandex.div.dsl.util
import com.yandex.div.dsl.ExpressionProperty
@@ -1,5 +1,3 @@
// Copyright (c) 2022 Yandex LLC. All rights reserved.
package com.yandex.div.dsl
import com.fasterxml.jackson.databind.node.JsonNodeFactory
@@ -9,7 +7,6 @@ import com.yandex.div.dsl.context.define
import com.yandex.div.dsl.context.override
import com.yandex.div.dsl.context.resolve
import com.yandex.div.dsl.context.templates
import com.yandex.div.dsl.model.Div
import com.yandex.div.dsl.model.DivContainer
import com.yandex.div.dsl.model.DivSizeUnit
import com.yandex.div.dsl.model.divAction
@@ -22,17 +19,18 @@ import com.yandex.div.dsl.model.divWrapContentSize
import com.yandex.div.dsl.model.state
import com.yandex.div.dsl.model.template
import com.yandex.div.dsl.serializer.toJsonNode
import divkit.dsl.readResource
import java.net.URI
import org.junit.jupiter.api.Test
import org.skyscreamer.jsonassert.JSONAssert.assertEquals
import org.skyscreamer.jsonassert.JSONCompareMode
internal class CardWithTemplatesTest {
class CardWithTemplatesTest {
@Test
internal fun renderCard() {
fun renderCard() {
val titleRef = reference<String>("title")
val templates = templates<Div> {
val templates = templates {
define ("title_text",
divText(
width = divFixedSize(
@@ -42,30 +40,6 @@ internal class CardWithTemplatesTest {
text = titleRef
)
)
define("header",
divContainer(
orientation = enum(DivContainer.Orientation.VERTICAL),
items = listOf(
divText(
width = divWrapContentSize(),
height = divFixedSize(
value = int(60),
),
text = titleRef
),
template(
type = "title_text",
override("width", divWrapContentSize())
)
)
)
)
define("subtitle_text",
template(
type = "title_text",
override("width", divWrapContentSize())
)
)
}
val card = card {
divData(
@@ -2,7 +2,6 @@ package com.yandex.div.dsl
import com.fasterxml.jackson.databind.node.JsonNodeFactory
import com.fasterxml.jackson.databind.node.ObjectNode
import com.yandex.div.dsl.context.TemplateContext
import com.yandex.div.dsl.context.card
import com.yandex.div.dsl.context.define
import com.yandex.div.dsl.context.override
@@ -37,6 +36,7 @@ import com.yandex.div.dsl.model.template
import com.yandex.div.dsl.serializer.toJsonNode
import com.yandex.div.dsl.type.BoolInt
import com.yandex.div.dsl.type.color
import divkit.dsl.readResource
import org.junit.jupiter.api.Test
import org.skyscreamer.jsonassert.JSONAssert
import org.skyscreamer.jsonassert.JSONCompareMode
@@ -59,7 +59,7 @@ class DistrictCardTest {
val galleryTailActionLinkRef = reference<DivAction>("gallery_tail_action_link")
val tailTextLinkRef = reference<String>("tail_text_link")
val templates: TemplateContext<Div> = templates<Div> {
val templates = templates {
define(
"title_text",
divText(
@@ -0,0 +1,65 @@
package divkit.dsl
import com.fasterxml.jackson.databind.json.JsonMapper
import divkit.dsl.core.bind
import divkit.dsl.core.reference
import org.junit.jupiter.api.Test
import org.skyscreamer.jsonassert.JSONAssert.assertEquals
import org.skyscreamer.jsonassert.JSONCompareMode
class DivanTest {
@Test
fun buildCardWithTemplate() {
val titleRef = reference<String>("title")
val titleTemplate = template("title_text") {
text(
width = fixedSize(
value = 120,
unit = dp
)
) + textRefs(text = titleRef)
}
val divan = divan {
data(
logId = "layout",
states = singleRoot(
div = container(
width = fixedSize(
value = 320
),
height = fixedSize(
value = 320
),
orientation = vertical,
items = listOf(
render(
titleTemplate,
titleRef bind "Good news, everyone!"
) + textProps(
width = matchParentSize()
),
text(
width = wrapContentSize(),
height = wrapContentSize(),
text = "Hello, Everyone!",
actions = listOf(
action(
logId = "tap_action",
url = url("https://yandex.ru")
)
)
)
)
)
)
)
}
val expected = readResource("/json/card_with_templates.json")
val actual = JsonMapper.builder().build().writeValueAsString(divan)
assertEquals(expected, actual, JSONCompareMode.STRICT)
}
}
@@ -1,10 +1,8 @@
// Copyright (c) 2022 Yandex LLC. All rights reserved.
package com.yandex.div.dsl
package divkit.dsl
import java.nio.file.Files
import java.nio.file.Path
internal fun readResource(path: String): String {
fun readResource(path: String): String {
return Files.readString(Path.of(object {}.javaClass.getResource(path).toURI()))
}
@@ -8,35 +8,6 @@
"unit": "dp",
"value": 120
}
},
"header": {
"type": "container",
"items": [
{
"type": "text",
"height": {
"type": "fixed",
"value": 60
},
"$text": "title",
"width": {
"type": "wrap_content"
}
},
{
"type": "title_text",
"width": {
"type": "wrap_content"
}
}
],
"orientation": "vertical"
},
"subtitle_text": {
"type": "title_text",
"width": {
"type": "wrap_content"
}
}
},
"card": {