-
Notifications
You must be signed in to change notification settings - Fork 2k
add Yaml.loadAll() method #256
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add Yaml.loadAll() method #256
Conversation
renamed the methods loadAllAs() to loadAll(). |
@@ -33,8 +33,6 @@ | |||
import org.yaml.snakeyaml.nodes.ScalarNode; | |||
|
|||
public class Yaml { | |||
private static org.yaml.snakeyaml.Yaml yaml = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are you moving away from a single YAML?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(ah, nevermind, saw your top-level comment)
* @return An instantiation of the object. | ||
* @throws IOException | ||
*/ | ||
private static Object ModelMapper(Map<String, Object> data) throws IOException { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: lower case the m
in ModelMapper
Iterable<Object> iterable = getSnakeYaml().loadAll(reader); | ||
List<Object> list = new ArrayList<Object>(); | ||
for (Object object : iterable) { | ||
if (object != null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In what situations is object
null
? Should we throw an exception in this case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when loading yaml via string reader and if string ends with --- and new line loadAll adds null at the end of array returned
example following string returns list with null at end
Input String:
------------------------------YAML-START----------------
kind: Scale
apiVersion: extensions/v1beta1
metadata:
name: foo
---
kind: Deployment
apiVersion: apps/v1beta1
metadata:
name: foo
---
------------------------------YAML-END---------------------
OutPut List :
[{kind=Scale, apiVersion=extensions/v1beta1, metadata={name=foo}}, {kind=Deployment, apiVersion=apps/v1beta1, metadata={name=foo}}, null]
List<Object> list = new ArrayList<Object>(); | ||
for (Object object : iterable) { | ||
if (object != null) { | ||
list.add(ModelMapper((Map<String, Object>) object)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps try { ... } catch (ClassCastException ex) { ... }
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so with in modelMapper shall i change all: throw new IOException's to throw new ClassCastException's
i.e.
throw new IOException("Missing kind in YAML file!");
to
throw new ClassCastException("Missing kind in YAML file!");
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, sorry I was unclear. The ((Map<String, Object) object)
part of this line could throw ClassCastException (if for example the YAML contains a string) I would catch that and throw a more intelligible error, rather than just letting the class cast percolate up to the caller.
We should keep the IOException
the same.
A few small fixes... |
@brendandburns |
LGTM, thanks! |
adding Yaml.loadAllAs() method to support multi yaml documents from single file. issue Not support to load multi yaml documents from a single file. #254
Moved static org.yaml.snakeyaml.Yaml(new CustomConstructor()) to getSnakeYaml() as, loading(loadAll) multi yaml file using static snake yaml object and again using same static snake yaml object to instantiate the individual objects (org.yaml.snakeyaml.Yaml.loadAs()) is causing the Iterable to loose next element and causes null pointer exceptions.
methods added: