Skip to content

Commit a8e288d

Browse files
committed
fix: support base64 encoded values in json/yaml (redhat-developer#854)
* fixed: base64 encoded values in json * fixed: Secret & ConfigMap are recognized if kind value is quoted ("Service", "ConfigMap")
1 parent 6022a25 commit a8e288d

File tree

6 files changed

+56
-21
lines changed

6 files changed

+56
-21
lines changed

src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/inlay/ResourceEditorInlayHintsProvider.kt

+14-7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import com.intellij.codeInsight.hints.InlayHintsSink
2121
import com.intellij.codeInsight.hints.NoSettings
2222
import com.intellij.codeInsight.hints.SettingsKey
2323
import com.intellij.json.psi.JsonFile
24+
import com.intellij.openapi.application.ApplicationManager
25+
import com.intellij.openapi.application.ReadAction
2426
import com.intellij.openapi.editor.Editor
2527
import com.intellij.psi.PsiElement
2628
import com.intellij.psi.PsiFile
@@ -51,7 +53,7 @@ internal class ResourceEditorInlayHintsProvider : InlayHintsProvider<NoSettings>
5153
}
5254
}
5355

54-
override fun getCollectorFor(file: PsiFile, editor: Editor, settings: NoSettings, sink: InlayHintsSink): InlayHintsCollector? {
56+
override fun getCollectorFor(file: PsiFile, editor: Editor, settings: NoSettings, sink: InlayHintsSink): InlayHintsCollector {
5557
return Collector(editor)
5658
}
5759

@@ -75,16 +77,21 @@ internal class ResourceEditorInlayHintsProvider : InlayHintsProvider<NoSettings>
7577
}
7678

7779
private fun create(file: YAMLFile, sink: InlayHintsSink, editor: Editor) {
78-
file.documents.forEach { document ->
79-
val info = KubernetesTypeInfo.extractMeta(document) ?: return
80-
val element = document.topLevelValue ?: return
81-
Base64Presentations.create(element, info, sink, editor)?.create()
80+
return ReadAction.run<Exception> {
81+
file.documents.forEach { document ->
82+
val info = KubernetesTypeInfo.extractMeta(document) ?: return@forEach
83+
val element = document.topLevelValue ?: return@forEach
84+
Base64Presentations.create(element, info, sink, editor)?.create()
85+
}
8286
}
8387
}
8488

8589
private fun create(file: JsonFile, sink: InlayHintsSink, editor: Editor) {
86-
val info = KubernetesTypeInfo.extractMeta(file) ?: return
87-
Base64Presentations.create(file, info, sink, editor)?.create()
90+
return ReadAction.run<Exception> {
91+
val info = KubernetesTypeInfo.extractMeta(file) ?: return@run
92+
val element = file.topLevelValue ?: return@run
93+
Base64Presentations.create(element, info, sink, editor)?.create()
94+
}
8895
}
8996

9097
}

src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/inlay/base64/Base64Presentations.kt

+2-3
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,12 @@ import com.intellij.openapi.command.WriteCommandAction
1818
import com.intellij.openapi.editor.Editor
1919
import com.intellij.openapi.project.Project
2020
import com.intellij.psi.PsiElement
21-
import com.redhat.devtools.intellij.common.validation.KubernetesResourceInfo
2221
import com.redhat.devtools.intellij.common.validation.KubernetesTypeInfo
2322
import com.redhat.devtools.intellij.kubernetes.balloon.StringInputBalloon
2423
import com.redhat.devtools.intellij.kubernetes.editor.inlay.base64.Base64Presentations.InlayPresentationsFactory
2524
import com.redhat.devtools.intellij.kubernetes.editor.inlay.base64.Base64Presentations.create
2625
import com.redhat.devtools.intellij.kubernetes.editor.util.getBinaryData
27-
import com.redhat.devtools.intellij.kubernetes.editor.util.getData
26+
import com.redhat.devtools.intellij.kubernetes.editor.util.getDataValue
2827
import com.redhat.devtools.intellij.kubernetes.editor.util.isKubernetesResource
2928
import com.redhat.devtools.intellij.kubernetes.model.util.trimWithEllipsis
3029
import org.jetbrains.concurrency.runAsync
@@ -44,7 +43,7 @@ object Base64Presentations {
4443
fun create(element: PsiElement, info: KubernetesTypeInfo, sink: InlayHintsSink, editor: Editor): InlayPresentationsFactory? {
4544
return when {
4645
isKubernetesResource(SECRET_RESOURCE_KIND, info) -> {
47-
val data = getData(element) ?: return null
46+
val data = getDataValue(element) ?: return null
4847
StringPresentationsFactory(data, sink, editor)
4948
}
5049

src/main/kotlin/com/redhat/devtools/intellij/kubernetes/editor/util/ResourceEditorUtils.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import com.intellij.json.psi.JsonValue
1818
import com.intellij.openapi.application.ReadAction
1919
import com.intellij.openapi.fileEditor.FileEditor
2020
import com.intellij.openapi.project.Project
21+
import com.intellij.openapi.util.text.StringUtil
2122
import com.intellij.openapi.util.text.Strings
2223
import com.intellij.openapi.vfs.VirtualFile
2324
import com.intellij.psi.PsiElement
@@ -144,7 +145,7 @@ private fun getElement(key: String, parent: PsiElement): PsiElement? {
144145
* @param element the PsiElement whose "data" child should be found.
145146
* @return the PsiElement named "data"
146147
*/
147-
fun getData(element: PsiElement): PsiElement? {
148+
fun getDataValue(element: PsiElement): PsiElement? {
148149
val dataElement = element.children
149150
.filterIsInstance<PsiNamedElement>()
150151
.find { it.name == KEY_DATA }

src/main/resources/META-INF/plugin.xml

+4-1
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,10 @@
234234
<editorTabTitleProvider implementation="com.redhat.devtools.intellij.kubernetes.editor.KubernetesEditorsTabTitleProvider"/>
235235
<codeInsight.inlayProvider language="yaml"
236236
implementationClass="com.redhat.devtools.intellij.kubernetes.editor.inlay.ResourceEditorInlayHintsProvider"
237-
id="MarkdownTableInlayProvider"/>
237+
id="com.redhat.devtools.intellij.kubernetes.editor.yaml.ResourceEditorInlayHintsProvider"/>
238+
<codeInsight.inlayProvider language="JSON"
239+
implementationClass="com.redhat.devtools.intellij.kubernetes.editor.inlay.ResourceEditorInlayHintsProvider"
240+
id="com.redhat.devtools.intellij.kubernetes.editor.json.ResourceEditorInlayHintsProvider"/>
238241
<projectConfigurable id="tools.settings.redhat.kubernetes"
239242
parentId="tools"
240243
displayName="Red Hat Kubernetes"

src/test/kotlin/com/redhat/devtools/intellij/kubernetes/editor/mocks/PsiElementMocks.kt

+17-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import com.intellij.json.psi.JsonProperty
1515
import com.intellij.json.psi.JsonValue
1616
import com.intellij.openapi.fileTypes.FileType
1717
import com.intellij.openapi.project.Project
18+
import com.intellij.psi.PsiElement
1819
import com.intellij.psi.PsiFile
1920
import com.intellij.psi.PsiFileFactory
2021
import com.nhaarman.mockitokotlin2.any
@@ -51,20 +52,34 @@ fun createJsonProperty(
5152
name: String = Random.nextInt().toString(),
5253
value: String? = null,
5354
parent: JsonProperty? = null,
54-
project: Project
55+
project: Project = mock()
5556
): JsonProperty {
5657
val valueElement: JsonValue = mock {
5758
on { getText() } doReturn value
5859
}
5960
return mock {
60-
on { getValue() } doReturn valueElement
6161
on { getName() } doReturn name
6262
on { getValue() } doReturn valueElement
6363
on { getParent() } doReturn parent
6464
on { getProject() } doReturn project
6565
}
6666
}
6767

68+
fun createJsonObject(
69+
name: String = Random.nextInt().toString(),
70+
properties: List<JsonProperty> = emptyList(),
71+
parent: PsiElement? = null,
72+
project: Project = mock()
73+
): JsonObject {
74+
return mock {
75+
on { getChildren() } doReturn properties.toTypedArray()
76+
on { getPropertyList() } doReturn properties
77+
on { getName() } doReturn name
78+
on { getParent() } doReturn parent
79+
on { getProject() } doReturn project
80+
}
81+
}
82+
6883
fun createProjectWithServices(
6984
yamlGenerator: YAMLElementGenerator? = null,
7085
psiFileFactory: PsiFileFactory? = null

src/test/kotlin/com/redhat/devtools/intellij/kubernetes/editor/util/ResourceEditorUtilsTest.kt

+17-7
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@
1111
package com.redhat.devtools.intellij.kubernetes.editor.util
1212

1313
import com.nhaarman.mockitokotlin2.mock
14-
import com.nhaarman.mockitokotlin2.verify
1514
import com.redhat.devtools.intellij.common.validation.KubernetesResourceInfo
1615
import com.redhat.devtools.intellij.common.validation.KubernetesTypeInfo
17-
import com.redhat.devtools.intellij.kubernetes.editor.mocks.createYAMLDocument
18-
import com.redhat.devtools.intellij.kubernetes.editor.mocks.createYAMLFile
16+
import com.redhat.devtools.intellij.kubernetes.editor.mocks.createJsonObject
17+
import com.redhat.devtools.intellij.kubernetes.editor.mocks.createJsonProperty
1918
import com.redhat.devtools.intellij.kubernetes.editor.mocks.createYAMLKeyValue
2019
import com.redhat.devtools.intellij.kubernetes.editor.mocks.createYAMLValue
2120
import com.redhat.devtools.intellij.kubernetes.model.mocks.Mocks.kubernetesResourceInfo
@@ -86,27 +85,38 @@ class ResourceEditorUtilsTest {
8685
}
8786

8887
@Test
89-
fun `#getData should return YAMLKeyValue named data`() {
88+
fun `#getDataValue should return YAMLKeyValue named data`() {
9089
// given
9190
val data = createYAMLKeyValue("data")
9291
val parent = createYAMLValue(arrayOf(data))
9392
// when
94-
val found = getData(parent)
93+
val found = getDataValue(parent)
9594
// then
9695
assertThat(found).isNotNull()
9796
}
9897

9998
@Test
100-
fun `#getData should return null if there is no child named data`() {
99+
fun `#getDataValue should return null if there is no child named data`() {
101100
// given
102101
val yoda = createYAMLKeyValue("yoda")
103102
val parent = createYAMLValue(arrayOf(yoda))
104103
// when
105-
val found = getData(parent)
104+
val found = getDataValue(parent)
106105
// then
107106
assertThat(found).isNull()
108107
}
109108

109+
@Test
110+
fun `#getDataValue should return JsonProperty named data`() {
111+
// given
112+
val data = createJsonProperty("data", value = "anakin")
113+
val parent = createJsonObject(properties = listOf(data))
114+
// when
115+
val found = getDataValue(parent)
116+
// then
117+
assertThat(found?.text).isEqualTo("anakin")
118+
}
119+
110120
@Test
111121
fun `#getBinaryData should return YAMLKeyValue named binaryData`() {
112122
// given

0 commit comments

Comments
 (0)