Skip to content

Commit

Permalink
yaml: Rewrite using event-based loading and saving
Browse files Browse the repository at this point in the history
This allows us to capture more information as representation
hints and to precisely control the output.

We can also read comments events to properly round-trip comments in
configuration files
  • Loading branch information
zml2008 committed Dec 19, 2022
1 parent 5695117 commit 0ece4c2
Show file tree
Hide file tree
Showing 38 changed files with 5,308 additions and 149 deletions.
10 changes: 10 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import org.jetbrains.gradle.ext.ActionDelegationConfig

import static org.eclipse.jgit.lib.Repository.shortenRefName

plugins {
Expand All @@ -8,10 +10,18 @@ plugins {
alias(libs.plugins.indra.sonatype)
alias(libs.plugins.indra.git)
alias(libs.plugins.gitPublish)
alias(libs.plugins.ideaExt)
id 'java-base'
id 'org.spongepowered.configurate.build.base'
}

idea.project.settings {
delegateActions {
delegateBuildRunToGradle = false
testRunner = ActionDelegationConfig.TestRunner.PLATFORM
}
}

tasks.named('aggregateJavadoc').configure {
def gradleJdk = JavaVersion.current()
// at least java 11, but not 12 (java 12 is broken for some reason :( )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private UnmodifiableCollections() {}
*
* @since 4.0.0
*/
public static <E> List<E> copyOf(final List<E> original) {
public static <E> List<E> copyOf(final List<? extends E> original) {
switch (original.size()) {
case 0:
return Collections.emptyList();
Expand All @@ -67,7 +67,7 @@ public static <E> List<E> copyOf(final List<E> original) {
* @return a unmodifiable copy of the given {@link Set} instance
* @since 4.0.0
*/
public static <E> Set<E> copyOf(final Set<E> original) {
public static <E> Set<E> copyOf(final Set<? extends E> original) {
switch (original.size()) {
case 0:
return Collections.emptySet();
Expand All @@ -87,12 +87,12 @@ public static <E> Set<E> copyOf(final Set<E> original) {
* @return an unmodifiable copy of the given {@link Map} instance.
* @since 4.1.0
*/
public static <K, V> Map<K, V> copyOf(final Map<K, V> original) {
public static <K, V> Map<K, V> copyOf(final Map<? extends K, ? extends V> original) {
switch (original.size()) {
case 0:
return Collections.emptyMap();
case 1:
final Map.Entry<K, V> entry = original.entrySet().iterator().next();
final Map.Entry<? extends K, ? extends V> entry = original.entrySet().iterator().next();
return Collections.singletonMap(entry.getKey(), entry.getValue());
default:
if (original instanceof LinkedHashMap<?, ?>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private UnmodifiableCollections() {}
* @return a unmodifiable copy of the given {@link List} instance
* @since 4.0.0
*/
public static <E> List<E> copyOf(final List<E> original) {
public static <E> List<E> copyOf(final List<? extends E> original) {
return List.copyOf(original);
}

Expand All @@ -54,7 +54,7 @@ public static <E> List<E> copyOf(final List<E> original) {
* @return a unmodifiable copy of the given {@link Set} instance
* @since 4.0.0
*/
public static <E> Set<E> copyOf(final Set<E> original) {
public static <E> Set<E> copyOf(final Set<? extends E> original) {
return Set.copyOf(original);
}

Expand Down
22 changes: 22 additions & 0 deletions extra/groovy/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
plugins {
id 'org.spongepowered.configurate.build.component'
id 'groovy'
}

tasks.processResources {
inputs.property("version", project.version)
expand version: project.version
}

dependencies {
api projects.core

[
libs.groovy,
libs.groovy.nio
].each {
implementation variantOf(it) { classifier('indy') }
}

testImplementation variantOf(libs.groovy.test) { classifier('indy') }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Configurate
* Copyright (C) zml and Configurate contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.spongepowered.configurate.extra.groovy

import org.spongepowered.configurate.ScopedConfigurationNode;

class ConfigurationNodeExtensions {

static <N extends ScopedConfigurationNode<N>> N getAt(final ScopedConfigurationNode<N> self, Iterable<?> path) {
return self.node(path)
}

static <N extends ScopedConfigurationNode<N>> N getAt(final ScopedConfigurationNode<N> self, Object... path) {
return self.node(path)
}

static <N extends ScopedConfigurationNode<N>> boolean isCase(final ScopedConfigurationNode<N> self, Iterable<?> path) {
return self.hasChild(path)
}

static <N extends ScopedConfigurationNode<N>> boolean isCase(final ScopedConfigurationNode<N> self, Object... path) {
return self.hasChild(path)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Configurate
* Copyright (C) zml and Configurate contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.spongepowered.configurate.extra.groovy

import org.spongepowered.configurate.ConfigurationNode
import org.spongepowered.configurate.serialize.SerializationException
import org.spongepowered.configurate.serialize.TypeSerializer

import java.lang.reflect.Type;

class GStringTypeSerializer implements TypeSerializer<GString> {

@Override
GString deserialize(final Type type, final ConfigurationNode node) throws SerializationException {
return GString.
return null;
}

@Override
public void serialize(
final Type type, final GString obj, final ConfigurationNode node) throws SerializationException {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Configurate
* Copyright (C) zml and Configurate contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.spongepowered.configurate.extra.groovy

import io.leangen.geantyref.GenericTypeReflector
import org.spongepowered.configurate.objectmapping.FieldDiscoverer
import org.spongepowered.configurate.serialize.SerializationException

import java.lang.reflect.AnnotatedType
import java.lang.reflect.Field

class POGOFieldDiscoverer implements FieldDiscoverer<Map<String, Field>> {

@Override
def <V> InstanceFactory<Map<String, Field>> discover(final AnnotatedType target, final FieldCollector<Map<String, Field>, V> collector)
throws SerializationException {
def clazz = GenericTypeReflector.erase(target.type)
clazz.metaClass.properties
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
moduleName=Extensions to configurate for compatibility with the Groovy environment
moduleVersion=${version}
extensionClasses=org.spongepowered.configurate.extra.groovy.ConfigurationNodeExtensions
staticExtensionClasses=
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Configurate
* Copyright (C) zml and Configurate contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.spongepowered.configurate.extra.groovy

import org.junit.jupiter.api.Test

class PogoFieldDiscovererTest {

@Test
void testPogoFieldDiscoverer() {

}
}
11 changes: 11 additions & 0 deletions format/yaml/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id 'org.spongepowered.configurate.build.component'
alias(libs.plugins.shadow)
id 'groovy' // for tests
}

description = "YAML format loader for Configurate"
Expand All @@ -12,9 +13,19 @@ configurations {
testImplementation { extendsFrom shaded }
}

configurate.useAutoValue()
dependencies {
api projects.core
shaded "configurate.thirdparty:snakeyaml:version-from-submodule"

[
libs.groovy,
libs.groovy.nio,
libs.groovy.test.junit5,
libs.groovy.templates
].each {
testImplementation it
}
}

tasks {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,17 @@ static DumperOptions.FlowStyle asSnakeYaml(final @Nullable NodeStyle style) {
return style == null ? DumperOptions.FlowStyle.AUTO : style.snake;
}

static @Nullable NodeStyle fromSnakeYaml(final DumperOptions.FlowStyle style) {
switch (style) {
case AUTO:
return null;
case BLOCK:
return BLOCK;
case FLOW:
return FLOW;
default:
throw new IllegalArgumentException("Unknown style " + style);
}
}

}
Loading

0 comments on commit 0ece4c2

Please sign in to comment.