Skip to content

Commit

Permalink
feat: impl'd describe for resources (redhat-developer#553)
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Dietisheim <[email protected]>
  • Loading branch information
adietish committed Jul 10, 2024
1 parent d8fe032 commit bdb305a
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*******************************************************************************
* Copyright (c) 2024 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
******************************************************************************/
package com.redhat.devtools.intellij.kubernetes.editor.describe

abstract class DescribeDocument {

protected val paragraphs = mutableListOf<Paragraph>()

fun add(paragraph: Paragraph): DescribeDocument {
paragraphs.add(paragraph)
return this
}

fun add(title: String, value: String): DescribeDocument {
paragraphs.add(Paragraph(title, value));
return this
}

abstract fun toText(): String

class Paragraph(val title: String, val value: String? = null) {

val children = mutableListOf<Paragraph>()

fun add(paragraph: Paragraph): Paragraph {
children.add(paragraph)
return this
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*******************************************************************************
* Copyright (c) 2024 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
******************************************************************************/
package com.redhat.devtools.intellij.kubernetes.editor.describe

import org.yaml.snakeyaml.DumperOptions
import org.yaml.snakeyaml.Yaml


class YAMLDescribeDocument: DescribeDocument() {

private val yaml = let {
val options = DumperOptions().apply {
defaultFlowStyle = DumperOptions.FlowStyle.BLOCK
}
Yaml(options)
}

override fun toText(): String {
return yaml.dump(toMap(paragraphs))
}

/*
* Turning objects into map because I couldn't convince snakeyaml to format like I wanted by using native configurations.
*/
private fun toMap(paragraphs: List<Paragraph>): Map<String, Any?> {
return paragraphs.associate { paragraph ->
if (paragraph.children.isEmpty()) {
paragraph.title to paragraph.value
} else {
paragraph.title to toMap(paragraph.children)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,12 @@ open class AllContexts(
get() {
lock.write {
if (_all.isEmpty()) {
val all = createContexts(client.get(), client.get()?.config)
try {
val all = createContexts(client.get(), client.get()?.config)
_all.addAll(all)
} catch (e: Exception) {
//
}
}
return _all
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.redhat.devtools.intellij.kubernetes.editor.describe

import com.redhat.devtools.intellij.kubernetes.editor.describe.DescribeDocument.*
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test

class YAMLDescribeDocumentTest {

@Test
fun `toText should return empty String if document has no paragraphs`() {
// given
val document = YAMLDescribeDocument()
// when
val text = document.toText()
// then
assertThat(text).isEqualTo("")
}

@Test
fun `toText should return title and value if document has 1 paragraph`() {
// given
val document = YAMLDescribeDocument()
document.add("jedi", "luke")
// when
val text = document.toText()
// then
assertThat(text).isEqualTo("jedi: luke\n")
}

@Test
fun `toText should return title, subtitle and values if document has 1 paragraph with a nested paragraph`() {
// given
val document = YAMLDescribeDocument()
val jedis = Paragraph("jedis")
.add(Paragraph("padawan", "luke"))
.add(Paragraph("master", "yoda"))
document.add(jedis)
// when
val text = document.toText()
// then
assertThat(text).isEqualTo(
"jedis:\n" +
" padawan: luke\n" +
" master: yoda\n"
)
}

@Test
fun `toText should return title, subtitle and a title if document has a paragraph, a nested one and a paragraph again`() {
// given
val document = YAMLDescribeDocument()
val jedis = Paragraph("jedis")
.add(Paragraph("padawan", "luke"))
val siths = Paragraph("sith")
.add(Paragraph("master", "darth vader"))
document
.add(jedis)
.add(siths)
// when
val text = document.toText()
// then
assertThat(text).isEqualTo(
"jedis:\n" +
" padawan: luke\n" +
"sith:\n" +
" master: darth vader\n"
)
}
}

0 comments on commit bdb305a

Please sign in to comment.