Carleslc / Simple-YAML

This Java API provides an easy-to-use way to store data and provide configurations using the YAML format.
https://carleslc.me/Simple-YAML
GNU General Public License v3.0
132 stars 38 forks source link

There is a bug when i set the list #61

Closed OMAR-3OOV closed 2 years ago

OMAR-3OOV commented 2 years ago

so basically I used the library for a few days, and I found out that when I have a list already and try to add an element to it, something weird happens, so I create the List first by using config.set("key", ArrayList<String>("")) so first I want to create empty list. but in the YML file it's not empty, so the bug is when I try to add an element like this:

List<String> list = ArrayList<String>(config.getStringList("key"), ("newElement"));

config.set("key", list);
config.save()

it displayed in the file like this

-  - 'newElement'
- 'oldElements'

it's so weird bug...

EverNife commented 2 years ago

This is not a bug.

You are creating an ArrayList that is not of \, but of \!

List myWrongList = new ArrayList();
myWrongList.add(Arrays.asList("element1", "element2", "element3");
myWrongList.add("simpleString1");
myWrongList.add("simpleString2");
myWrongList.add("simpleString3");

Now, the myWrongList contains 4 elements, and not 6 elements. myWrongList contains 1 List of Strings and 3 other strings

image

To fix your problem, rather than calling add(anotherList) you should be calling addAll

Code showing how to reproduce your problem. ``` @Test void contextLoads() throws Exception { YamlFile firstYamlFile = new YamlFile(new File("teste.yml")); List firstList = Arrays.asList( "OldElement1", "OldElement2", "OldElement3" ); firstYamlFile.set("MyList", firstList); firstYamlFile.save(); YamlFile secondYamlFile = new YamlFile(new File("teste.yml")); //Load from the previous value secondYamlFile.loadWithComments(); List retrievedList = secondYamlFile.getStringList("MyList"); List mixedList = new ArrayList(); mixedList.add(retrievedList); mixedList.add("newElement1"); mixedList.add("newElement2"); mixedList.add("newElement3"); secondYamlFile.set("MyList2", mixedList); secondYamlFile.save(); System.out.println(mixedList); } ```
Carleslc commented 2 years ago

As stated, that is the intended behaviour. Thank you @EverNife for the answer.

With config.set("key", Arrays.asList(config.getStringList("key"), "newElement")) you're creating a list of lists instead of appending the previous elements. To append multiple elements you must use addAll.

Example to add elements to a list:

// Create the YamlFile
YamlFile config = new YamlFile("examples/test61.yml");

// Create a new config file if it does not exist or load its contents otherwise
config.createOrLoad();

// Set the list
config.set("key", new ArrayList<>(Arrays.asList("oldElement1", "oldElement2")));

// Get the previously set list (copy)
List<String> list = config.getStringList("key");

// Add several elements to the list
list.addAll(Arrays.asList("newElement1", "newElement2"));

// Add single element to the list
list.add("newElement3");

// Update the list elements
config.set("key", list);

// You can also add elements with list indexing!
config.set("key[-1]", "lastElement");

// Save the configuration file
config.save();
key:
  - oldElement1
  - oldElement2
  - newElement1
  - newElement2
  - newElement3
  - lastElement