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

[TOML] Order of input is not the order of output. #56

Closed
Articdive opened this issue Mar 23, 2019 · 5 comments
Closed

[TOML] Order of input is not the order of output. #56

Articdive opened this issue Mar 23, 2019 · 5 comments
Assignees

Comments

@Articdive
Copy link

Articdive commented Mar 23, 2019

I have a List that I'm outputting into my toml file.

I give it a,b,c,d in a LinkedList<> so it's definitely that order in the for loop (which just sets the path to said value), e.g "a" = "RandomNumber" and so on;
and the output I receive is:
b
c
a
d
and not
a
b
c
d

TLDR: Pretty much the set method isn't saving the order in which elements are set.

@TheElectronWill
Copy link
Owner

TheElectronWill commented Mar 23, 2019

Config is based on HashMap which is unordered, that's why the order of config entries isn't guaranteed.

It's not a goal, for now (v3), to have ordered configs by default. However, you can still have them by wrapping a LinkedHashMap:

Config ordered = Config.wrap(new LinkedHashMap(), InMemoryFormat.defaultInstance());
// use whatever other ConfigFormat you need

@Articdive
Copy link
Author

Alright, that works, thanks :)

@TheElectronWill
Copy link
Owner

@Articdive NightConfig v3.5.3 now offers a global setting to preserve the insertion order of values 😃

Config.setInsertionOrderPreserved(true);

All configurations created after this call will preserve the insertion order, except if created from a user-provided Map or Map supplier.

@TheElectronWill TheElectronWill self-assigned this Apr 14, 2019
@mikehearn
Copy link

Why not just always preserve insertion order? Are there some fundamental reasons to not delete this option and have it always be true?

FWIW in Kotlin if you say val a = mapOf(....) then what you get is always a LinkedHashMap. They considered that the very small additional memory overhead is easily worth the saving in bugs and confusion due to pseudo-random reorderings.

@TheElectronWill
Copy link
Owner

You're right, it would be better to preserve the order by default. I must admit there is no real reason other than "that's how I've done it in NightConfig v1 so it stayed like this".

Besides, LinkedHashMap's performance is comparable to HashMap's. Adding this to my TODO for NightConfig v4 😃

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

No branches or pull requests

3 participants