UcasRichard / snakeyaml

Automatically exported from code.google.com/p/snakeyaml
Apache License 2.0
0 stars 0 forks source link

Add loadAllAs() methods to Yaml class, or overload loadAll() to use generics and avoid typecasting #162

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
When you need to load multiple Yaml objects from a file you have to load them 
using the loadAll() method, but this does not support the typed version like 
the loadAs() does. 

So you have to pass the Constructor object to the Yaml constructor first, and 
then iterate through all objects returned through loadAll(), type casting each 
object.

It would be more convenient and typesafe if Yaml supported a loadAllAs() method 
which took the Class<T> parameter of the object, and returned Iterable<T>. 
Alternatively one could also simply overload the loadAll() methods, and have a 
second Class<T> parameter.

Applies to SnakeYaml 1.11, Java 1.7. 

Original issue reported on code.google.com by josef.ba...@gmail.com on 22 Nov 2012 at 12:27

GoogleCodeExporter commented 9 years ago
Apologies for the issue type, it should be Enhancement, but I couldn't find 
where to set it.

Original comment by josef.ba...@gmail.com on 22 Nov 2012 at 12:29

GoogleCodeExporter commented 9 years ago
Feel free to deliver the implementation.

Original comment by py4fun@gmail.com on 22 Nov 2012 at 4:12

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Attached including junit test cases. :)

Original comment by josef.ba...@gmail.com on 22 Nov 2012 at 5:35

Attachments:

GoogleCodeExporter commented 9 years ago
1) please implement loadAllAs() because loadAll() methods do not cast
2) the whole idea behind loadAs() methods that you do not need to create your 
own Constructor at all. Look at the examples:
        Yaml beanLoader = new Yaml();
        ListFieldBean parsed = beanLoader.loadAs(output, ListFieldBean.class);

What should happen if users give different Classes for the Constructor and for 
the loadAllAs() ?

Original comment by py4fun@gmail.com on 22 Nov 2012 at 7:35

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Well the overloaded loadAll() is essentially a different method, just 
overloaded with the same name. We can change it to loadAllAs() no problem. I 
just followed the same pattern used by JPA (EntityManager.createQuery() 
http://docs.oracle.com/javaee/6/api/javax/persistence/EntityManager.html#createQ
uery(java.lang.String, java.lang.Class))

The new loadAll(..., Class<T> type) methods do work without the Constructor. 
(Its included in the test case but its redundant, because the code does exactly 
the same as the loadAs(..., Class<T> type).

> What should happen if users give different Classes for the Constructor and 
for the loadAllAs() ?

This is just a convenience typesafe method for when you have a list of YAML 
objects all of the same type in a file. The same thing that happens now to 
loadAs() if different constructor classes are given should happen.

Original comment by josef.ba...@gmail.com on 22 Nov 2012 at 7:45

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Indeed, I have removed the custom Constructor, but the tests are still green.
The tests may have 2 purposes:
1) they prove that the contact works
2) they show how to use the API (without redundant code)
Can you please provide a patch (or a clone) which delivers both ? 
Please do not forget to check the coverage ('mvn site', and look at the 
generated coverage report - 
file:///.../snakeyaml/target/site/cobertura/index.html). At the moment, the 
introduction of a new method in BaseConstructor reduces the coverage for the 
existing method.

Original comment by py4fun@gmail.com on 23 Nov 2012 at 10:12

GoogleCodeExporter commented 9 years ago
OK, will try to refine it a little more as you're suggesting when I get some 
time.

Original comment by josef.ba...@gmail.com on 27 Nov 2012 at 11:05