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

Fix squeezed Toml Files #154

Merged
merged 9 commits into from
Jan 27, 2024
Merged

Fix squeezed Toml Files #154

merged 9 commits into from
Jan 27, 2024

Conversation

Stein-N
Copy link
Contributor

@Stein-N Stein-N commented Jan 23, 2024

As described in discussion #153, ​​the TableEntries were squeezed together by the new check in the TableWriter.

The check separates the TablesEntries from the normal/simple written values inside the Toml File.

This simple Solution separates the TableEntries and TableArrayEntries from each other without effecting the Entry or List by itself.
Means the ArrayList gets further squeezed together but when a new ArrayList is written a blank line is written in between.

@TheElectronWill
Copy link
Owner

TheElectronWill commented Jan 26, 2024

Looks good to me, nice little contribution :)

@TheElectronWill
Copy link
Owner

Do you want to try to add unit tests? Otherwise I can write them.

@Stein-N
Copy link
Contributor Author

Stein-N commented Jan 26, 2024

Do you want to try to add unit tests? Otherwise I can write them.

Never wrote testing to this point, would try tomorrow and will commit them. Hope they will be good 😁

@Stein-N
Copy link
Contributor Author

Stein-N commented Jan 27, 2024

The second empty line at the end of the file can be "fixed" by modifying the writeNormal Function, by adding an counter for the Amount of TableEntries and TableArrayEntries and decreasing it everytime one Entrie/Array was written.
But this seems unnecessary because this additional empty line only appears at the end of the toml file.
So i would say it is better only to add this empty line to the tests rather than adding this check.
We can also use an Iterator and while instead of for loops to check if the entrySet has a next value, but this also seems a bit to much.

private static void writeNormal(UnmodifiableCommentedConfig config, List<String> configPath,
									CharacterOutput output, TomlWriter writer) {
		List<UnmodifiableCommentedConfig.Entry> tablesEntries = new ArrayList<>();
		List<UnmodifiableCommentedConfig.Entry> tableArraysEntries = new ArrayList<>();

		int tableEntriesAmount = 0; // Fix for second empty line at the end of the File
		int tableArrayEntriesAmount = 0; // Fix for second empty line at the end of the File

		// Writes the "simple" values:
		writer.increaseIndentLevel();// Indent++
		for (UnmodifiableCommentedConfig.Entry entry : config.entrySet()) {
			final String key = entry.getKey();
			final Object value = entry.getValue();
			final String comment = entry.getComment();
			if (value instanceof UnmodifiableConfig &&
				!writer.writesInline((UnmodifiableConfig)value)) {
				tablesEntries.add(entry);
				tableEntriesAmount++; // Fix for second empty line at the end of the File
				continue;
			} else if (value instanceof List) {
				List<?> list = (List<?>)value;
				if (!list.isEmpty() && list.stream().allMatch(UnmodifiableConfig.class::isInstance)) {
					tableArraysEntries.add(entry);
					tableArrayEntriesAmount++; // Fix for second empty line at the end of the File
					continue;
				}
			}
			writer.writeComment(comment, output);// Writes the comment above the key
			writer.writeIndent(output);// Indents the line.
			writer.writeKey(key, output);
			output.write(KEY_VALUE_SEPARATOR);
			ValueWriter.write(value, output, writer);
			writer.writeNewline(output);
		}

		int nonSimpleValuesCount = tablesEntries.size() + tableArraysEntries.size();
		int simpleValuesCount = config.size() - nonSimpleValuesCount;
		if (simpleValuesCount > 0 && nonSimpleValuesCount > 0) {
			writer.writeNewline(output);
		}

		// Writes the tables:
		for (UnmodifiableCommentedConfig.Entry entry : tablesEntries) {
			// Writes the comment, if there is one
			writer.writeComment(entry.getComment(), output);

			// Writes the table declaration
			configPath.add(entry.getKey());// path level ++
			writeTableName(configPath, output, writer);
			writer.writeNewline(output);

			// Writes the table's content
			writeNormal(entry.<UnmodifiableConfig>getValue(), configPath, output, writer);
			configPath.remove(configPath.size() - 1);// path level --
			tableEntriesAmount--;// Fix for second empty line at the end of the File
                        if (tableEntriesAmount > 0) writer.writeNewline(output); // Fix for second empty line at the end of the File
		}

		// Writes the arrays of tables:
		for (UnmodifiableCommentedConfig.Entry entry : tableArraysEntries) {
			// Writes the comment, if there is one
			writer.writeComment(entry.getComment(), output);

			// Writes the tables
			configPath.add(entry.getKey());// path level ++
			List<Config> tableArray = entry.getValue();
			for (UnmodifiableConfig table : tableArray) {
				writeTableArrayName(configPath, output, writer);
				writer.writeNewline(output);
				writeNormal(table, configPath, output, writer);
			}
			configPath.remove(configPath.size() - 1);// path level --
			tableArrayEntriesAmount--;// Fix for second empty line at the end of the File
			if (tableArrayEntriesAmount > 0) writer.writeNewline(output); // Fix for second empty line at the end of the File
		}
		writer.decreaseIndentLevel();// Indent--
	}

Copy link
Owner

@TheElectronWill TheElectronWill left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

java.sql.Array should not be imported, other that that it's ok.

removing second empty line Assertion
@Stein-N
Copy link
Contributor Author

Stein-N commented Jan 27, 2024

changes made

Copy link
Owner

@TheElectronWill TheElectronWill left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@TheElectronWill TheElectronWill merged commit 224cd1d into TheElectronWill:master Jan 27, 2024
0utplay referenced this pull request in CloudNetService/CloudNet May 16, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
|
[com.electronwill.night-config:json](https://github.com/TheElectronWill/night-config)
| `3.6.7` -> `3.7.1` |
[![age](https://developer.mend.io/api/mc/badges/age/maven/com.electronwill.night-config:json/3.7.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/maven/com.electronwill.night-config:json/3.7.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/maven/com.electronwill.night-config:json/3.6.7/3.7.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/maven/com.electronwill.night-config:json/3.6.7/3.7.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
|
[com.electronwill.night-config:yaml](https://github.com/TheElectronWill/night-config)
| `3.6.7` -> `3.7.1` |
[![age](https://developer.mend.io/api/mc/badges/age/maven/com.electronwill.night-config:yaml/3.7.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/maven/com.electronwill.night-config:yaml/3.7.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/maven/com.electronwill.night-config:yaml/3.6.7/3.7.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/maven/com.electronwill.night-config:yaml/3.6.7/3.7.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
|
[com.electronwill.night-config:toml](https://github.com/TheElectronWill/night-config)
| `3.6.7` -> `3.7.1` |
[![age](https://developer.mend.io/api/mc/badges/age/maven/com.electronwill.night-config:toml/3.7.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/maven/com.electronwill.night-config:toml/3.7.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/maven/com.electronwill.night-config:toml/3.6.7/3.7.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/maven/com.electronwill.night-config:toml/3.6.7/3.7.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

> [!WARNING]
> Some dependencies could not be looked up. Check the Dependency
Dashboard for more information.

---

### Release Notes

<details>
<summary>TheElectronWill/night-config
(com.electronwill.night-config:json)</summary>

###
[`v3.7.1`](https://github.com/TheElectronWill/night-config/releases/tag/v3.7.1):
3.7.1 Gradle fix

##### Fixes

- Resolve a misconfiguration of the Gradle build, see
[https://github.com/TheElectronWill/night-config/issues/173](https://github.com/TheElectronWill/night-config/issues/173)
for more details
-   Fix a minor issue with the new options of `JsonParser`

You should use v3.7.1 instead of v3.7.0.
Please [read the release notes of
v3.7.0](https://github.com/TheElectronWill/night-config/issues/173)
:sparkles: to learn what has changed since v3.6.x.

**Full Changelog**:
TheElectronWill/night-config@v3.7.0...v3.7.1

###
[`v3.7.0`](https://github.com/TheElectronWill/night-config/releases/tag/v3.7.0):
3.7.0: NightConfig rejuvenated ✨

##### Major changes

- **`FileWatcher` now comes with out-of-the-box debouncing**, which
improves the performance of autoreloading and autosaving configurations
:zap:, see
[https://github.com/TheElectronWill/night-config/pull/148](https://github.com/TheElectronWill/night-config/pull/148)
- **`FileConfig` instances are now actually thread-safe** and work
better with autoreload, which should prevent most of the corruption
issues 🛡️, see
[https://github.com/TheElectronWill/night-config/pull/152](https://github.com/TheElectronWill/night-config/pull/152)
- **A new Serialization/Deserialization framework** is available in the
`serde` package 🚀, see
[https://github.com/TheElectronWill/night-config/pull/163](https://github.com/TheElectronWill/night-config/pull/163).
Unlike the old `ObjectConverter`, the new `ObjectSerializer` and
`ObjectDeserializer` are modular and extensible: you can register your
own serializers and deserializers. Deserializers (config -> object) are
chosen based on the generic type of the field to set and the type of the
config value to deserialize. This allows for fine-grained
deserialization. The old `ObjectConverter` still works but is now
deprecated. The goal is to remove the old `conversion` package in
version 4.0.0.

##### Enhancements

- Allow multiple spec conditions to be checked by
[@&#8203;vaperion](https://github.com/vaperion) in
[https://github.com/TheElectronWill/night-config/pull/151](https://github.com/TheElectronWill/night-config/pull/151)
(Note: this fix applies to the deprecated `conversion` package; the new
`serde` package supports multiple `@SerdeAssert` conditions out of the
box)
- Support UTF-16 and UTF-8 BOM by
[@&#8203;TheElectronWill](https://github.com/TheElectronWill) in
TheElectronWill/night-config@68502b9
- In ConfigSpec and in the new `serde` Deserializer, automatically apply
some "risky" conversions (e.g. long -> int) when it is not lossy, close
[https://github.com/TheElectronWill/night-config/issues/119](https://github.com/TheElectronWill/night-config/issues/119)
- Allow custom `Config` storage in `ConfigSpec`, by
[@&#8203;PaintNinja](https://github.com/PaintNinja) in
[https://github.com/TheElectronWill/night-config/pull/168](https://github.com/TheElectronWill/night-config/pull/168)
- More unit tests, on Java 8, Java 11 and Java 17, by
[@&#8203;TheElectronWill](https://github.com/TheElectronWill)

##### Bug fixes

- toml: Fix squeezed Files by
[@&#8203;Stein-N](https://github.com/Stein-N) in
[https://github.com/TheElectronWill/night-config/pull/154](https://github.com/TheElectronWill/night-config/pull/154)
- toml: Align List ending when in a "group" by
[@&#8203;Stein-N](https://github.com/Stein-N) in
[https://github.com/TheElectronWill/night-config/pull/160](https://github.com/TheElectronWill/night-config/pull/160)
- toml: Allow header comments by
[@&#8203;Stein-N](https://github.com/Stein-N) in
[https://github.com/TheElectronWill/night-config/pull/162](https://github.com/TheElectronWill/night-config/pull/162)
- json: Disallow trailing data (configurable) by
[@&#8203;TheElectronWill](https://github.com/TheElectronWill), close
[https://github.com/TheElectronWill/night-config/issues/167](https://github.com/TheElectronWill/night-config/issues/167)
- Multiple bugs have been fixed in `ObjectConverter` by
[@&#8203;TheElectronWill](https://github.com/TheElectronWill) in
[https://github.com/TheElectronWill/night-config/pull/163](https://github.com/TheElectronWill/night-config/pull/163)

##### Compatibility Note

NightConfig 3.7.0 is backward-compatible with previous releases,
therefore I encourage you to upgrade your dependency on NightConfig in
order to benefit from the new version.

Binary-wise, the backward-compatibility has been checked with `japicmp`.
Source-wise, there is one small incompatible change:
`FileWatcher#addWatch` no longers throws an `IOException`, which can
result in a compiler error `unreachable code` on the associated
try/catch ([example in
forge](https://github.com/MinecraftForge/MinecraftForge/blob/640c206117864085e3c25889bcabeaddd555e9fb/fmlcore/src/main/java/net/minecraftforge/fml/config/ConfigFileTypeHandler.java#L50-L55)).

##### New Contributors

- [@&#8203;vaperion](https://github.com/vaperion) made their first
contribution in
[https://github.com/TheElectronWill/night-config/pull/151](https://github.com/TheElectronWill/night-config/pull/151)
- [@&#8203;Stein-N](https://github.com/Stein-N) made their first
contribution in
[https://github.com/TheElectronWill/night-config/pull/154](https://github.com/TheElectronWill/night-config/pull/154)
- [@&#8203;PaintNinja](https://github.com/PaintNinja) made their first
contribution in
[https://github.com/TheElectronWill/night-config/pull/168](https://github.com/TheElectronWill/night-config/pull/168)

Thank you everyone!

**Full Changelog**:
TheElectronWill/night-config@v3.6.7...v3.7.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "before 6:00am" in timezone
Europe/Berlin, Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about these
updates again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/CloudNetService/CloudNet).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4zNjMuNSIsInVwZGF0ZWRJblZlciI6IjM3LjM2My41IiwidGFyZ2V0QnJhbmNoIjoibmlnaHRseSIsImxhYmVscyI6WyJ0OiBkZXBlbmRlbmNpZXMiXX0=-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
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