33
33
import org .yaml .snakeyaml .nodes .ScalarNode ;
34
34
35
35
public class Yaml {
36
- private static org .yaml .snakeyaml .Yaml yaml =
37
- new org .yaml .snakeyaml .Yaml (new CustomConstructor ());
38
36
private static Map <String , Class <?>> classes = new HashMap <>();
39
37
private static Map <String , String > apiGroups = new HashMap <>();
40
38
private static List <String > apiVersions = new ArrayList <>();
@@ -167,33 +165,8 @@ public static Object load(File f) throws IOException {
167
165
* @throws IOException If an error occurs while reading the YAML.
168
166
*/
169
167
public static Object load (Reader reader ) throws IOException {
170
- Map <String , Object > data = yaml .load (reader );
171
- String kind = (String ) data .get ("kind" );
172
- if (kind == null ) {
173
- throw new IOException ("Missing kind in YAML file!" );
174
- }
175
- String apiVersion = (String ) data .get ("apiVersion" );
176
- if (apiVersion == null ) {
177
- throw new IOException ("Missing apiVersion in YAML file!" );
178
- }
179
-
180
- Class <?> clazz = (Class <?>) classes .get (apiVersion + "/" + kind );
181
- if (clazz == null ) {
182
- // Attempt to detect class from version and kind alone
183
- if (apiVersion .contains ("/" )) {
184
- clazz = (Class <?>) classes .get (apiVersion .split ("/" )[1 ] + "/" + kind );
185
- }
186
- }
187
- if (clazz == null ) {
188
- throw new IOException (
189
- "Unknown apiVersionKind: "
190
- + apiVersion
191
- + "/"
192
- + kind
193
- + " known kinds are: "
194
- + classes .toString ());
195
- }
196
- return loadAs (new StringReader (yaml .dump (data )), clazz );
168
+ Map <String , Object > data = getSnakeYaml ().load (reader );
169
+ return modelMapper (data );
197
170
}
198
171
199
172
/**
@@ -206,7 +179,7 @@ public static Object load(Reader reader) throws IOException {
206
179
* @throws IOException If an error occurs while reading the YAML.
207
180
*/
208
181
public static <T > T loadAs (String content , Class <T > clazz ) {
209
- return yaml .loadAs (new StringReader (content ), clazz );
182
+ return getSnakeYaml () .loadAs (new StringReader (content ), clazz );
210
183
}
211
184
212
185
/**
@@ -218,7 +191,7 @@ public static <T> T loadAs(String content, Class<T> clazz) {
218
191
* @throws IOException If an error occurs while reading the YAML.
219
192
*/
220
193
public static <T > T loadAs (File f , Class <T > clazz ) throws IOException {
221
- return yaml .loadAs (new FileReader (f ), clazz );
194
+ return getSnakeYaml () .loadAs (new FileReader (f ), clazz );
222
195
}
223
196
224
197
/**
@@ -231,7 +204,62 @@ public static <T> T loadAs(File f, Class<T> clazz) throws IOException {
231
204
* @throws IOException If an error occurs while reading the YAML.
232
205
*/
233
206
public static <T > T loadAs (Reader reader , Class <T > clazz ) {
234
- return yaml .loadAs (reader , clazz );
207
+ return getSnakeYaml ().loadAs (reader , clazz );
208
+ }
209
+
210
+ /**
211
+ * Load list of instantiated API objects from a YAML string representation. Returns list of
212
+ * concrete typed objects (e.g. { V1Pod, V1SERVICE })
213
+ *
214
+ * <p>Order of API objects in list will be preserved according to order of objects in YAML string.
215
+ *
216
+ * @param content The YAML content
217
+ * @return List of instantiated objects.
218
+ * @throws IOException If an error occurs while reading the YAML.
219
+ */
220
+ public static List <Object > loadAll (String content ) throws IOException {
221
+ return loadAll (new StringReader (content ));
222
+ }
223
+
224
+ /**
225
+ * Load list of instantiated API objects from a YAML file. Returns list of concrete typed objects
226
+ * (e.g. { V1Pod, V1SERVICE })
227
+ *
228
+ * <p>Order of API objects in list will be preserved according to order of objects in YAML file.
229
+ *
230
+ * @param f The file to load.
231
+ * @return List of instantiated of the objects.
232
+ * @throws IOException If an error occurs while reading the YAML.
233
+ */
234
+ public static List <Object > loadAll (File f ) throws IOException {
235
+ return loadAll (new FileReader (f ));
236
+ }
237
+
238
+ /**
239
+ * Load list of instantiated API objects from a stream of data. Returns list of concrete typed
240
+ * objects (e.g. { V1Pod, V1SERVICE })
241
+ *
242
+ * <p>Order of API objects in list will be preserved according to order of objects in stream of
243
+ * data.
244
+ *
245
+ * @param reader The stream to load.
246
+ * @return List of instantiated of the objects.
247
+ * @throws IOException If an error occurs while reading the YAML.
248
+ */
249
+ public static List <Object > loadAll (Reader reader ) throws IOException {
250
+ Iterable <Object > iterable = getSnakeYaml ().loadAll (reader );
251
+ List <Object > list = new ArrayList <Object >();
252
+ for (Object object : iterable ) {
253
+ if (object != null ) {
254
+ try {
255
+ list .add (modelMapper ((Map <String , Object >) object ));
256
+ } catch (ClassCastException ex ) {
257
+ logger .error ("Unexpected exception while casting: " + ex );
258
+ }
259
+ }
260
+ }
261
+
262
+ return list ;
235
263
}
236
264
237
265
/** Defines constructor logic for custom types in this library. */
@@ -252,4 +280,43 @@ private IntOrString constructIntOrString(ScalarNode node) {
252
280
}
253
281
}
254
282
}
283
+
284
+ /** @return An instantiated SnakeYaml Object. */
285
+ public static org .yaml .snakeyaml .Yaml getSnakeYaml () {
286
+ return new org .yaml .snakeyaml .Yaml (new CustomConstructor ());
287
+ }
288
+
289
+ /**
290
+ * @param data Map that will be converted to concrete API object.
291
+ * @return An instantiation of the object.
292
+ * @throws IOException
293
+ */
294
+ private static Object modelMapper (Map <String , Object > data ) throws IOException {
295
+ String kind = (String ) data .get ("kind" );
296
+ if (kind == null ) {
297
+ throw new IOException ("Missing kind in YAML file!" );
298
+ }
299
+ String apiVersion = (String ) data .get ("apiVersion" );
300
+ if (apiVersion == null ) {
301
+ throw new IOException ("Missing apiVersion in YAML file!" );
302
+ }
303
+
304
+ Class <?> clazz = (Class <?>) classes .get (apiVersion + "/" + kind );
305
+ if (clazz == null ) {
306
+ // Attempt to detect class from version and kind alone
307
+ if (apiVersion .contains ("/" )) {
308
+ clazz = (Class <?>) classes .get (apiVersion .split ("/" )[1 ] + "/" + kind );
309
+ }
310
+ }
311
+ if (clazz == null ) {
312
+ throw new IOException (
313
+ "Unknown apiVersionKind: "
314
+ + apiVersion
315
+ + "/"
316
+ + kind
317
+ + " known kinds are: "
318
+ + classes .toString ());
319
+ }
320
+ return loadAs (new StringReader (getSnakeYaml ().dump (data )), clazz );
321
+ }
255
322
}
0 commit comments