10
10
******************************************************************************/
11
11
package com.redhat.devtools.intellij.kubernetes.editor
12
12
13
+ import com.fasterxml.jackson.databind.ObjectMapper
14
+ import com.fasterxml.jackson.databind.node.ArrayNode
13
15
import com.intellij.json.JsonFileType
14
16
import com.intellij.openapi.fileTypes.FileType
15
17
import com.redhat.devtools.intellij.kubernetes.model.util.ResourceException
16
18
import com.redhat.devtools.intellij.kubernetes.model.util.createResource
17
19
import io.fabric8.kubernetes.api.model.GenericKubernetesResource
18
20
import io.fabric8.kubernetes.api.model.HasMetadata
21
+ import io.fabric8.kubernetes.api.model.KubernetesListBuilder
19
22
import io.fabric8.kubernetes.client.utils.Serialization
20
23
import org.jetbrains.yaml.YAMLFileType
21
24
22
25
object EditorResourceSerialization {
23
26
24
27
const val RESOURCE_SEPARATOR_YAML = " \n ---"
25
- private const val RESOURCE_SEPARATOR_JSON = " ,\n "
26
28
27
29
/* *
28
30
* Returns a list of [HasMetadata] for a given yaml or json string.
@@ -41,60 +43,99 @@ object EditorResourceSerialization {
41
43
* @see JsonFileType.INSTANCE
42
44
*/
43
45
fun deserialize (jsonYaml : String? , fileType : FileType ? , currentNamespace : String? ): List <HasMetadata > {
44
- return if (jsonYaml == null
45
- || ! isSupported(fileType)) {
46
- emptyList()
47
- } else {
48
- val resources = jsonYaml
49
- .split(RESOURCE_SEPARATOR_YAML )
50
- .filter { jsonYaml -> jsonYaml.isNotBlank() }
51
- if (resources.size > 1
52
- && YAMLFileType .YML != fileType) {
53
- throw ResourceException (
54
- " ${fileType?.name ? : " File type" } is not supported for multi-resource documents. Only ${YAMLFileType .YML .name} is." )
46
+ return try {
47
+ when {
48
+ jsonYaml == null ->
49
+ emptyList()
50
+
51
+ YAMLFileType .YML == fileType ->
52
+ yaml2Resources(jsonYaml, currentNamespace)
53
+
54
+ JsonFileType .INSTANCE == fileType ->
55
+ json2Resources(jsonYaml, currentNamespace)
56
+
57
+ else ->
58
+ emptyList()
55
59
}
56
- try {
57
- resources
58
- .map { jsonYaml ->
59
- setMissingNamespace(currentNamespace, createResource<GenericKubernetesResource >(jsonYaml))
60
+ } catch (e: RuntimeException ) {
61
+ throw ResourceException (" Invalid kubernetes yaml/json" , e.cause ? : e)
62
+ }
63
+ }
64
+
65
+ private fun yaml2Resources (yaml : String , currentNamespace : String? ): List <HasMetadata > {
66
+ val resources = yaml
67
+ .split(RESOURCE_SEPARATOR_YAML )
68
+ .filter { yaml ->
69
+ yaml.isNotBlank()
70
+ }
71
+ return resources
72
+ .map { yaml ->
73
+ setMissingNamespace(currentNamespace, createResource<GenericKubernetesResource >(yaml))
74
+ }
75
+ .toList()
76
+ }
77
+
78
+ private fun json2Resources (json : String? , currentNamespace : String? ): List <HasMetadata > {
79
+ val mapper = ObjectMapper ()
80
+ val rootNode = mapper.readTree(json)
81
+ return when {
82
+ rootNode.isArray ->
83
+ (rootNode as ArrayNode )
84
+ .mapNotNull { node ->
85
+ setMissingNamespace(currentNamespace, mapper.treeToValue(node, GenericKubernetesResource ::class .java))
60
86
}
61
87
.toList()
62
- } catch (e: RuntimeException ) {
63
- throw ResourceException (" Invalid kubernetes yaml/json" , e.cause ? : e)
64
- }
88
+ rootNode.isObject ->
89
+ listOf (
90
+ setMissingNamespace(currentNamespace,
91
+ mapper.treeToValue(rootNode, GenericKubernetesResource ::class .java)
92
+ )
93
+ )
94
+ else ->
95
+ emptyList()
65
96
}
66
97
}
67
98
68
99
private fun setMissingNamespace (namespace : String? , resource : HasMetadata ): HasMetadata {
69
- if (resource.metadata.namespace.isNullOrEmpty()
100
+ if (resource.metadata != null
101
+ && resource.metadata.namespace.isNullOrEmpty()
70
102
&& namespace != null ) {
71
103
resource.metadata.namespace = namespace
72
104
}
73
105
return resource
74
106
}
75
107
76
108
fun serialize (resources : List <HasMetadata >, fileType : FileType ? ): String? {
77
- if (fileType == null ) {
78
- return null
79
- }
80
- if (resources.size >= 2
81
- && fileType != YAMLFileType .YML ) {
82
- throw UnsupportedOperationException (
83
- " ${fileType.name} is not supported for multi-resource documents. Only ${YAMLFileType .YML .name} is." )
109
+ return try {
110
+ when {
111
+ fileType == null ->
112
+ null
113
+
114
+ YAMLFileType .YML == fileType ->
115
+ resources2yaml(resources)
116
+
117
+ JsonFileType .INSTANCE == fileType ->
118
+ resources2json(resources)
119
+
120
+ else ->
121
+ " "
122
+ }
123
+ } catch (e: RuntimeException ) {
124
+ throw ResourceException (" Invalid kubernetes yaml/json" , e.cause ? : e)
84
125
}
85
- return resources
86
- .mapNotNull { resource -> serialize(resource, fileType) }
87
- .joinToString(" \n " )
126
+ }
88
127
128
+ private fun resources2yaml (resources : List <HasMetadata >): String {
129
+ return resources.joinToString(" \n " ) { resource ->
130
+ Serialization .asYaml(resource).trim()
131
+ }
89
132
}
90
133
91
- private fun serialize (resource : HasMetadata , fileType : FileType ): String? {
92
- return when (fileType) {
93
- YAMLFileType .YML ->
94
- Serialization .asYaml(resource).trim()
95
- JsonFileType .INSTANCE ->
96
- Serialization .asJson(resource).trim()
97
- else -> null
134
+ private fun resources2json (resources : List <HasMetadata >): String {
135
+ return if (resources.size == 1 ) {
136
+ Serialization .asJson(resources.first()).trim()
137
+ } else {
138
+ Serialization .asJson(resources).trim()
98
139
}
99
140
}
100
141
0 commit comments