Skip to content

Commit b299517

Browse files
committed
fix: dont return match labels if there are only matchExpressions (#878)
Signed-off-by: Andre Dietisheim <[email protected]>
1 parent cb4c0c5 commit b299517

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ import com.intellij.openapi.util.text.StringUtil
1515
import com.intellij.psi.PsiElement
1616
import org.jetbrains.yaml.psi.YAMLKeyValue
1717
import org.jetbrains.yaml.psi.YAMLMapping
18+
import org.jetbrains.yaml.psi.YAMLScalar
1819
import org.jetbrains.yaml.psi.YAMLSequence
1920
import org.jetbrains.yaml.psi.YAMLSequenceItem
21+
import org.jetbrains.yaml.psi.YAMLValue
2022

2123
private const val KEY_MATCH_LABELS = "matchLabels"
2224
private const val KEY_MATCH_EXPRESSIONS = "matchExpressions"
@@ -142,12 +144,20 @@ fun YAMLMapping.getMatchLabels(): YAMLMapping? {
142144
val matchLabels = selector.getKeyValueByKey(KEY_MATCH_LABELS)
143145
return if (matchLabels != null) {
144146
matchLabels.value as? YAMLMapping?
145-
} else {
147+
} else if (selector.getKeyValueByKey(KEY_MATCH_EXPRESSIONS) == null) {
146148
// Service can have matchLabels as direct children without the 'matchLabels' key.
149+
// check if selector has matchExpressions which are not matchLabels
147150
selector
151+
} else {
152+
null
148153
}
149154
}
150155

156+
private fun YAMLValue.isYamlStringValue(): Boolean {
157+
return this is YAMLScalar && this.textValue != null
158+
}
159+
160+
151161
private fun JsonObject.getMatchLabels(): JsonObject? {
152162
val selector = this.getSelector() ?: return null
153163
val matchLabels = selector.findProperty(KEY_MATCH_LABELS)

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,24 @@ class PsiElementExtensionsTest {
6767
assertThat(isEmpty!!.keyValues).isEmpty()
6868
}
6969

70+
@Test
71+
fun `#getMatchLabels should return null if selector only has matchExpressions`() {
72+
// given
73+
val hasMatchExpressions = createYAMLMapping(listOf(
74+
createYAMLKeyValue("kind", "Deployment")
75+
))
76+
hasMatchExpressions.createMatchExpressions(
77+
createYAMLSequence(listOf(
78+
createYAMLSequenceItem("jedi", "In", listOf("yoda", "obiwan", "luke"))
79+
))
80+
)
81+
// when
82+
val isNull = yamlElement.getMatchLabels()
83+
84+
// then
85+
assertThat(isNull).isNull()
86+
}
87+
7088
@Test
7189
fun `#hasMatchLabels should return true when labels exist`() {
7290
// given

0 commit comments

Comments
 (0)