CONSUMER

A problem the app found in its own settings for a node, kept in the same file as the tree, so that one list holds everything wrong with the file. Heddle never reports this kind: the app builds the problem with the node's TopologyProblem.path and puts it in the list itself.

Samples

val text = """
    {"root": "pool", "children": [
      {"type": "group", "name": "acme", "weight": "3", "children": [
        {"type": "lane", "name": "batch", "rules": ["nightly"]},
        {"type": "lane", "name": "batch", "rules": ["hourly"]}
      ]}
    ]}
"""
// One decode, under a default `Json`: a key neither heddle nor the app knows is an error.
val file = Json.decodeFromString<PoolFile>(text)

fun PoolNode.document(): TopologyDocument.Node = when (this) {
    is PoolGroup -> TopologyDocument.Group(name, weight, children.map { it.document() }, template)
    is PoolLane -> TopologyDocument.Lane(name, weight)
}
val document = TopologyDocument(file.root, file.children.map { it.document() })

// The app's own check, addressed the same way: child indices from the root, in written order.
fun MutableList<TopologyProblem>.checkRules(nodes: List<PoolNode>, path: List<Int>, names: List<String>) {
    nodes.forEachIndexed { index, node ->
        node.rules.filter { it !in listOf("nightly", "weekly") }.forEach { rule ->
            add(TopologyProblem(path + index, names + node.name, TopologyProblem.Kind.CONSUMER, "no rule is called \"$rule\""))
        }
        if (node is PoolGroup) checkRules(node.children, path + index, names + node.name)
    }
}
val ours = buildList { checkRules(file.children, emptyList(), listOf(file.root)) }
val heddles = (Topology.from(document) as? TopologyParse.Invalid)?.problems.orEmpty()
val problems = heddles + ours
// One list, one addressing: the second `batch` is both a duplicate and has an unknown rule.
check(problems.map { it.path to it.kind } == listOf(listOf(0, 1) to TopologyProblem.Kind.DUPLICATE_NAME, listOf(0, 1) to TopologyProblem.Kind.CONSUMER))

// Why one strict decode and not two lenient ones: to decode the same file into heddle's type,
// the app's `rules` key must be ignored, and so is a misspelt `weigth`, which then reads as "1".
val typo = """{"root": "pool", "children": [{"type": "lane", "name": "batch", "weigth": "3"}]}"""
check(Json { ignoreUnknownKeys = true }.decodeFromString<TopologyDocument>(typo).children.single().weight == "1")
val refused = try {
    Json.decodeFromString<PoolFile>(typo)
    null
} catch (e: SerializationException) {
    e
}
check(refused != null)

Properties

Link copied to clipboard
expect val name: String
Link copied to clipboard
expect val ordinal: Int