|
| 1 | +package com.redhat.devtools.intellij.kubernetes.editor.util |
| 2 | + |
| 3 | +import com.intellij.json.psi.JsonObject |
| 4 | +import com.intellij.json.psi.JsonStringLiteral |
| 5 | +import com.intellij.openapi.util.text.StringUtil |
| 6 | +import org.jetbrains.yaml.psi.YAMLKeyValue |
| 7 | +import org.jetbrains.yaml.psi.YAMLMapping |
| 8 | +import org.jetbrains.yaml.psi.YAMLSequence |
| 9 | +import org.jetbrains.yaml.psi.YAMLSequenceItem |
| 10 | +import org.jetbrains.yaml.psi.impl.YAMLMappingImpl |
| 11 | + |
| 12 | +private const val KEY_KIND = "kind" |
| 13 | +private const val KEY_METADATA = "metadata" |
| 14 | +private const val KEY_NAME = "name" |
| 15 | +private const val KEY_LABELS = "labels" |
| 16 | +private const val KEY_SPEC = "spec" |
| 17 | +private const val KEY_SELECTOR = "selector" |
| 18 | +private const val KEY_MATCH_LABELS = "matchLabels" |
| 19 | +private const val KEY_MATCH_EXPRESSIONS = "matchExpressions" |
| 20 | +// match expression |
| 21 | +private const val KEY_KEY = "matchExpressions" |
| 22 | +private const val KEY_OPERATOR = "operator" |
| 23 | +private enum class OPERATORS { In, NotIn, Exists, DoesNotExist } |
| 24 | +private const val KEY_VALUES = "values" |
| 25 | + |
| 26 | +fun YAMLMapping.isMatchingLabels(labels: YAMLMapping?): Boolean { |
| 27 | + if (labels == null) { |
| 28 | + return false |
| 29 | + } |
| 30 | + val labels = this.getLabels() ?: return false |
| 31 | + |
| 32 | + for (matchLabel in labels.keyValues) { |
| 33 | + val labelName = matchLabel.keyText |
| 34 | + val labelValue = (matchLabel.value as? YAMLKeyValue)?.valueText ?: matchLabel.valueText |
| 35 | + |
| 36 | + val resourceLabel = labels.keyValues.find { it.keyText == labelName } |
| 37 | + if (resourceLabel == null || resourceLabel.valueText != labelValue) { |
| 38 | + return false |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + return true |
| 43 | +} |
| 44 | + |
| 45 | +fun YAMLMapping.isMatchingExpressions(expressions: YAMLSequence?): Boolean { |
| 46 | + if (expressions == null) { |
| 47 | + return true |
| 48 | + } |
| 49 | + return expressions.items.all { expression -> |
| 50 | + this.isMatchingExpression(expression) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +fun YAMLMapping.isMatchingExpression(item: YAMLSequenceItem?,): Boolean { |
| 55 | + val labels = this.getLabels() ?: return false |
| 56 | + |
| 57 | + if (item !is YAMLMapping) return true // ignore invalid yaml |
| 58 | + |
| 59 | + val key = item.getKeyValueByKey(KEY_KEY)?.value ?: return true // ignore expression without key |
| 60 | + val operator = item.getKeyValueByKey(KEY_OPERATOR)?.value ?: return true // ignore expression without operator |
| 61 | + val values = item.getKeyValueByKey(KEY_VALUES)?.value as? YAMLSequence |
| 62 | + |
| 63 | + val resourceLabel = labels.keyValues.find { it.keyText == key.text } |
| 64 | + |
| 65 | + return when (operator.text) { |
| 66 | + OPERATORS.In.name -> { |
| 67 | + resourceLabel == null |
| 68 | + || values == null |
| 69 | + || values.items.all { |
| 70 | + StringUtil.unquoteString(it.text) == StringUtil.unquoteString(resourceLabel.valueText) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + OPERATORS.NotIn.name -> { |
| 75 | + resourceLabel != null |
| 76 | + && values != null |
| 77 | + && values.items.none { |
| 78 | + StringUtil.unquoteString(it.text) == StringUtil.unquoteString(resourceLabel.valueText) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + OPERATORS.Exists.name -> { |
| 83 | + resourceLabel != null |
| 84 | + } |
| 85 | + |
| 86 | + OPERATORS.DoesNotExist.name -> { |
| 87 | + resourceLabel == null |
| 88 | + } |
| 89 | + |
| 90 | + else -> { |
| 91 | + false |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +fun YAMLMapping.getMatchLabels(): YAMLMapping? { |
| 97 | + val selector = this.getSelector() ?: return null |
| 98 | + val matchLabels = selector.getKeyValueByKey(KEY_MATCH_LABELS) |
| 99 | + return matchLabels?.value as YAMLMapping? |
| 100 | + ?: selector |
| 101 | +} |
| 102 | + |
| 103 | +fun YAMLMapping.getSelector(): YAMLMapping? { |
| 104 | + return (this.getKeyValueByKey(KEY_SPEC)?.value as? YAMLMappingImpl) |
| 105 | + ?.getKeyValueByKey(KEY_SELECTOR)?.value as? YAMLMapping |
| 106 | +} |
| 107 | + |
| 108 | +fun YAMLMapping.getMatchExpressions(): YAMLSequence? { |
| 109 | + return ((this.getKeyValueByKey(KEY_SPEC) as? YAMLMapping) |
| 110 | + ?.getKeyValueByKey(KEY_SELECTOR) as? YAMLMapping) |
| 111 | + ?.getKeyValueByKey(KEY_MATCH_EXPRESSIONS) as? YAMLSequence? |
| 112 | +} |
| 113 | + |
| 114 | +fun YAMLMapping.hasKindAndName(): Boolean { |
| 115 | + val kind = this.getKeyValueByKey(KEY_KIND)?.valueText |
| 116 | + val metadata = this.getMetadata() |
| 117 | + val name = metadata?.getKeyValueByKey(KEY_NAME)?.valueText |
| 118 | + return !kind.isNullOrEmpty() && !name.isNullOrEmpty() |
| 119 | +} |
| 120 | + |
| 121 | +fun YAMLMapping.getKind(): String? { |
| 122 | + return this.getKeyValueByKey(KEY_KIND) |
| 123 | + ?.valueText |
| 124 | +} |
| 125 | + |
| 126 | +fun YAMLMapping.getMetadataName(): String? { |
| 127 | + return this.getMetadata() |
| 128 | + ?.getKeyValueByKey(KEY_NAME) |
| 129 | + ?.valueText |
| 130 | +} |
| 131 | + |
| 132 | +fun YAMLMapping.getLabels(): YAMLMapping? { |
| 133 | + return this.getMetadata() |
| 134 | + ?.getKeyValueByKey(KEY_LABELS) |
| 135 | + ?.value as? YAMLMapping |
| 136 | +} |
| 137 | + |
| 138 | +fun YAMLMapping.getMetadata(): YAMLMapping? { |
| 139 | + return this.getKeyValueByKey(KEY_METADATA) |
| 140 | + ?.value as? YAMLMapping |
| 141 | +} |
| 142 | + |
| 143 | +fun JsonObject.hasKindAndName(): Boolean { |
| 144 | + val kind = this.findProperty(KEY_KIND)?.value as? JsonStringLiteral |
| 145 | + val metadata = this.getMetadata() |
| 146 | + val name = metadata?.findProperty(KEY_NAME)?.value as? JsonStringLiteral |
| 147 | + return kind != null && name != null |
| 148 | +} |
| 149 | + |
| 150 | +fun JsonObject.getKind(): String? { |
| 151 | + val kind = this.findProperty(KEY_KIND)?.value as? JsonStringLiteral |
| 152 | + return kind?.value |
| 153 | +} |
| 154 | + |
| 155 | +fun JsonObject.getMetadataName(): String? { |
| 156 | + val metadata = this.getMetadata() |
| 157 | + val name = metadata?.findProperty(KEY_NAME)?.value as? JsonStringLiteral |
| 158 | + return name?.value |
| 159 | +} |
| 160 | + |
| 161 | +fun JsonObject.getLabels(): JsonObject? { |
| 162 | + val metadata = this.getMetadata() |
| 163 | + return metadata |
| 164 | +} |
| 165 | + |
| 166 | +fun JsonObject.getMetadata(): JsonObject? { |
| 167 | + return this.findProperty(KEY_METADATA)?.value as? JsonObject |
| 168 | +} |
| 169 | + |
| 170 | +fun JsonObject.getMatchLabels(): JsonObject? { |
| 171 | + return ((this.findProperty(KEY_SPEC) as? JsonObject) |
| 172 | + ?.findProperty(KEY_SELECTOR) as? JsonObject) |
| 173 | + ?.findProperty(KEY_MATCH_LABELS) as? JsonObject |
| 174 | +} |
| 175 | + |
0 commit comments