Skip to content

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

Merged
merged 3 commits into from
May 5, 2018
Merged

add Yaml.loadAll() method #256

merged 3 commits into from
May 5, 2018

Conversation

karthikkondapally
Copy link
Contributor

  • 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:

    • List loadAllAs(String content)
    • List loadAllAs(File f)
    • List loadAllAs(Reader reader)

@karthikkondapally
Copy link
Contributor Author

renamed the methods loadAllAs() to loadAll().

@karthikkondapally karthikkondapally changed the title add Yaml.loadAllAs() method add Yaml.loadAll() method Apr 29, 2018
@@ -33,8 +33,6 @@
import org.yaml.snakeyaml.nodes.ScalarNode;

public class Yaml {
private static org.yaml.snakeyaml.Yaml yaml =
Copy link
Contributor

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?

Copy link
Contributor

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 {
Copy link
Contributor

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) {
Copy link
Contributor

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?

Copy link
Contributor Author

@karthikkondapally karthikkondapally May 4, 2018

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));
Copy link
Contributor

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) { ... } ?

Copy link
Contributor Author

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!");

Copy link
Contributor

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.

@brendandburns
Copy link
Contributor

A few small fixes...

@karthikkondapally
Copy link
Contributor Author

@brendandburns
sorry, was late to do the review changes.

@brendandburns
Copy link
Contributor

LGTM, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants