Skip to content
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

There is a bug when i set the list #61

Closed
OMAR-3OOV opened this issue May 15, 2022 · 2 comments
Closed

There is a bug when i set the list #61

OMAR-3OOV opened this issue May 15, 2022 · 2 comments
Labels

Comments

@OMAR-3OOV
Copy link

OMAR-3OOV commented May 15, 2022

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

EverNife commented Jun 2, 2022

This is not a bug.

You are creating an ArrayList that is not of <Strings>, but of <Object>!

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<String> 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<String> 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
Copy link
Owner

Carleslc commented Aug 1, 2022

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

@Carleslc Carleslc closed this as completed Aug 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants