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,58 @@ 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 > loadAllAs (String content ) throws IOException {
221
+ return loadAllAs (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 > loadAllAs (File f ) throws IOException {
235
+ return loadAllAs (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 > loadAllAs (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
+ list .add (ModelMapper ((Map <String , Object >) object ));
255
+ }
256
+ }
257
+
258
+ return list ;
235
259
}
236
260
237
261
/** Defines constructor logic for custom types in this library. */
@@ -252,4 +276,43 @@ private IntOrString constructIntOrString(ScalarNode node) {
252
276
}
253
277
}
254
278
}
279
+
280
+ /** @return An instantiated SnakeYaml Object. */
281
+ public static org .yaml .snakeyaml .Yaml getSnakeYaml () {
282
+ return new org .yaml .snakeyaml .Yaml (new CustomConstructor ());
283
+ }
284
+
285
+ /**
286
+ * @param data Map that will be converted to concrete API object.
287
+ * @return An instantiation of the object.
288
+ * @throws IOException
289
+ */
290
+ private static Object ModelMapper (Map <String , Object > data ) throws IOException {
291
+ String kind = (String ) data .get ("kind" );
292
+ if (kind == null ) {
293
+ throw new IOException ("Missing kind in YAML file!" );
294
+ }
295
+ String apiVersion = (String ) data .get ("apiVersion" );
296
+ if (apiVersion == null ) {
297
+ throw new IOException ("Missing apiVersion in YAML file!" );
298
+ }
299
+
300
+ Class <?> clazz = (Class <?>) classes .get (apiVersion + "/" + kind );
301
+ if (clazz == null ) {
302
+ // Attempt to detect class from version and kind alone
303
+ if (apiVersion .contains ("/" )) {
304
+ clazz = (Class <?>) classes .get (apiVersion .split ("/" )[1 ] + "/" + kind );
305
+ }
306
+ }
307
+ if (clazz == null ) {
308
+ throw new IOException (
309
+ "Unknown apiVersionKind: "
310
+ + apiVersion
311
+ + "/"
312
+ + kind
313
+ + " known kinds are: "
314
+ + classes .toString ());
315
+ }
316
+ return loadAs (new StringReader (getSnakeYaml ().dump (data )), clazz );
317
+ }
255
318
}
0 commit comments