13
13
import com .intellij .json .psi .JsonFile ;
14
14
import com .intellij .json .psi .JsonProperty ;
15
15
import com .intellij .json .psi .JsonValue ;
16
+ import com .intellij .openapi .util .text .StringUtil ;
16
17
import com .intellij .psi .PsiElement ;
17
18
import com .intellij .psi .PsiElementVisitor ;
18
19
import com .intellij .psi .PsiFile ;
19
- import java .util .Objects ;
20
20
import org .jetbrains .annotations .NotNull ;
21
+ import org .jetbrains .yaml .psi .YAMLDocument ;
21
22
import org .jetbrains .yaml .psi .YAMLFile ;
22
23
import org .jetbrains .yaml .psi .YAMLKeyValue ;
23
24
import org .jetbrains .yaml .psi .YAMLValue ;
24
25
26
+ import java .util .List ;
27
+ import java .util .Objects ;
28
+
25
29
public class KubernetesTypeInfo {
30
+
31
+ private static final String KEY_API_VERSION = "apiVersion" ;
32
+ private static final String KEY_KIND = "kind" ;
33
+
26
34
private String apiGroup = "" ;
27
- private String kind = "" ;
35
+ private String kind = "" ;
28
36
29
37
public KubernetesTypeInfo (String apiGroup , String kind ) {
30
38
this .apiGroup = apiGroup ;
@@ -33,55 +41,72 @@ public KubernetesTypeInfo(String apiGroup, String kind) {
33
41
34
42
public KubernetesTypeInfo () {}
35
43
36
- public static KubernetesTypeInfo extractMeta (PsiFile file ) {
37
- KubernetesTypeInfo info = new KubernetesTypeInfo ();
44
+ public static KubernetesTypeInfo create (PsiFile file ) {
38
45
if (file instanceof JsonFile ) {
39
- extractJsonMeta ((JsonFile ) file , info );
46
+ return create ((JsonFile ) file );
40
47
} else if (file instanceof YAMLFile ) {
41
- extractYAMLMeta ((YAMLFile ) file , info );
48
+ return create ((YAMLFile ) file );
49
+ } else {
50
+ return null ;
42
51
}
43
- return info ;
44
52
}
45
53
46
- private static void extractJsonMeta (JsonFile file , KubernetesTypeInfo info ) {
47
- JsonValue content = file .getTopLevelValue ();
54
+ private static KubernetesTypeInfo create (JsonFile file ) {
55
+ var collector = new JsonKubernetesTypeInfoVisitor ();
56
+ final JsonValue content = file .getTopLevelValue ();
48
57
if (content != null ) {
49
- content .acceptChildren (new PsiElementVisitor () {
50
- @ Override
51
- public void visitElement (@ NotNull PsiElement element ) {
52
- if (element instanceof JsonProperty ) {
53
- JsonProperty property = (JsonProperty ) element ;
54
- if (property .getName ().equals ("apiVersion" )) {
55
- info .setApiGroup (property .getValue ().getText ());
56
- } else if (property .getName ().equals ("kind" )) {
57
- info .setKind (property .getValue ().getText ());
58
- }
59
- }
60
- }
61
- });
58
+ content .acceptChildren (collector );
62
59
}
60
+ return collector .getKubernetesTypeInfo ();
63
61
}
64
62
65
- private static void extractYAMLMeta (YAMLFile file , KubernetesTypeInfo info ) {
66
- if (!file .getDocuments ().isEmpty ()) {
67
- YAMLValue content = file .getDocuments ().get (0 ).getTopLevelValue ();
68
- if (content != null ) {
69
- content .acceptChildren (new PsiElementVisitor () {
70
- @ Override
71
- public void visitElement (@ NotNull PsiElement element ) {
72
- if (element instanceof YAMLKeyValue ) {
73
- YAMLKeyValue property = (YAMLKeyValue ) element ;
74
- if (property .getKeyText ().equals ("apiVersion" )) {
75
- info .setApiGroup (property .getValueText ());
76
- } else if (property .getKeyText ().equals ("kind" )) {
77
- info .setKind (property .getValueText ());
78
- }
79
- }
80
- }
81
- });
63
+ /**
64
+ * Extracts the k8s metadata of the first document in the given YAML file.
65
+ *
66
+ * @param file the yaml file to extract the k8s metadata of
67
+ *
68
+ * @return the k8s metadata of the first document in the given file
69
+ */
70
+ private static KubernetesTypeInfo create (YAMLFile file ) {
71
+ if (file == null
72
+ || file .getDocuments ().isEmpty ()) {
73
+ return null ;
74
+ }
75
+
76
+ // only use the first document in the file
77
+ return create (file .getDocuments ().get (0 ));
78
+ }
82
79
80
+ /**
81
+ * Creates a list of {@link KubernetesTypeInfo} for the given YAML file.
82
+ * If the given file contains several documents then KubernetesTypeInfo's for each document will be created.
83
+ * If there's only a single document only a single KuberenetesTypeInfo is created.
84
+ * Returns {@code null} if the given file is {@code null} or empty.
85
+ *
86
+ * @param file the yaml file to create KubernetesTypeInfo's for
87
+ *
88
+ * @return KubernetesTypeInfos of all the documents in the given file
89
+ */
90
+ static List <KubernetesTypeInfo > createTypes (YAMLFile file ) {
91
+ if (file == null
92
+ || file .getDocuments ().isEmpty ()) {
93
+ return null ;
94
+ }
95
+
96
+ return file .getDocuments ().stream ()
97
+ .map (KubernetesTypeInfo ::create )
98
+ .toList ();
99
+ }
100
+
101
+ public static KubernetesTypeInfo create (YAMLDocument document ) {
102
+ final KubernetesTypeInfoVisitor collector = new YAMLKubernetesTypeInfoVisitor ();
103
+ if (document != null ) {
104
+ final YAMLValue content = document .getTopLevelValue ();
105
+ if (content != null ) {
106
+ content .acceptChildren (collector );
83
107
}
84
108
}
109
+ return collector .getKubernetesTypeInfo ();
85
110
}
86
111
87
112
public String getApiGroup () {
@@ -121,11 +146,65 @@ public String toString() {
121
146
122
147
public static KubernetesTypeInfo fromFileName (String filename ) {
123
148
int index = filename .indexOf ('_' );
124
- String apiGroup = (index != (-1 ))?filename .substring (0 , index ):"" ;
125
- String kind = (index != (-1 ))?filename .substring (index + 1 ):filename ;
126
- index = kind .lastIndexOf ('.' );
127
- kind = (index != (-1 ))?kind .substring (0 , index ):kind ;
128
- return new KubernetesTypeInfo (apiGroup , kind );
149
+ String apiGroup = (index != (-1 )) ? filename .substring (0 , index ) : "" ;
150
+ String kind = (index != (-1 )) ? filename .substring (index + 1 ) : filename ;
151
+ index = kind .lastIndexOf ('.' );
152
+ kind = (index != (-1 )) ? kind .substring (0 , index ) : kind ;
153
+ return new KubernetesTypeInfo (apiGroup , kind );
154
+ }
155
+
156
+ private static abstract class KubernetesTypeInfoVisitor extends PsiElementVisitor {
129
157
158
+ private KubernetesTypeInfo info = null ;
159
+
160
+ protected void setApiGroup (String apiGroup ) {
161
+ existingOrCreate ().setApiGroup (apiGroup );
130
162
}
163
+
164
+ protected void setKind (String kind ) {
165
+ existingOrCreate ().setKind (kind );
166
+ }
167
+
168
+ private KubernetesTypeInfo existingOrCreate () {
169
+ if (info == null ) {
170
+ this .info = new KubernetesTypeInfo ();
171
+ }
172
+ return info ;
173
+ }
174
+
175
+ public KubernetesTypeInfo getKubernetesTypeInfo () {
176
+ return info ;
177
+ }
178
+ }
179
+
180
+ static class YAMLKubernetesTypeInfoVisitor extends KubernetesTypeInfoVisitor {
181
+ @ Override
182
+ public void visitElement (@ NotNull PsiElement element ) {
183
+ if (element instanceof YAMLKeyValue property ) {
184
+ String value = StringUtil .unquoteString (property .getValueText ());
185
+ if (property .getKeyText ().equals (KEY_API_VERSION )) {
186
+ setApiGroup (value );
187
+ } else if (property .getKeyText ().equals (KEY_KIND )) {
188
+ setKind (value );
189
+ }
190
+ }
191
+ }
192
+ }
193
+
194
+ static class JsonKubernetesTypeInfoVisitor extends KubernetesTypeInfoVisitor {
195
+ @ Override
196
+ public void visitElement (@ NotNull PsiElement element ) {
197
+ if (element instanceof JsonProperty property
198
+ && property .getValue () != null
199
+ && property .getValue ().getText () != null ) {
200
+ String value = StringUtil .unquoteString (property .getValue ().getText ());
201
+ if (property .getName ().equals (KEY_API_VERSION )) {
202
+ setApiGroup (value );
203
+ } else if (property .getName ().equals (KEY_KIND )) {
204
+ setKind (value );
205
+ }
206
+ }
207
+ }
208
+ }
209
+
131
210
}
0 commit comments