forked from redhat-developer/intellij-kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: impl'd describe for resources (redhat-developer#553)
Signed-off-by: Andre Dietisheim <[email protected]>
- Loading branch information
Showing
4 changed files
with
154 additions
and
1 deletion.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/describe/DescribeDocument.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...in/kotlin/com/redhat/devtools/intellij/kubernetes/editor/describe/YAMLDescribeDocument.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...otlin/com/redhat/devtools/intellij/kubernetes/editor/describe/YAMLDescribeDocumentTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) | ||
} | ||
} |