-
Notifications
You must be signed in to change notification settings - Fork 481
YAML implementation with Jackson #1478
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
Merged
nedtwigg
merged 12 commits into
diffplug:main
from
solven-eu:IntroduceYamlJvmBased_withJackson
Jan 12, 2023
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d3052d3
Base implementation
blacelle a89ba3a
Progress with doc and tests
blacelle 3244f2d
Add unit-tests
blacelle 4307866
Improve testcases
blacelle 4438d18
Small fixes
blacelle 890ead9
Merge remote-tracking branch 'origin/main' into IntroduceYamlJvmBased…
blacelle dcf727c
Add not trivial json tests
blacelle 5041562
Fix reflection on stringToObject
blacelle dcb82ae
Fix cleaned YAML given limitations of Jackson
blacelle fb3bb16
Fix cleaned YAMLs
blacelle 13d9d3c
Fix YAML and JSON tests
blacelle fa203e4
Remove reflection impl to rely on glue impl
blacelle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -121,3 +121,6 @@ nbdist/ | |
nbactions.xml | ||
nb-configuration.xml | ||
.nb-gradle/ | ||
|
||
# MacOS jenv | ||
.java-version |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
lib/src/jackson/java/com/diffplug/spotless/glue/yaml/YamlJacksonFormatterFunc.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* Copyright 2021-2023 DiffPlug | ||
* | ||
* 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 com.diffplug.spotless.glue.yaml; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.SerializationFeature; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; | ||
|
||
import com.diffplug.spotless.FormatterFunc; | ||
|
||
public class YamlJacksonFormatterFunc implements FormatterFunc { | ||
private List<String> enabledFeatures; | ||
private List<String> disabledFeatures; | ||
|
||
public YamlJacksonFormatterFunc(List<String> enabledFeatures, List<String> disabledFeatures) { | ||
this.enabledFeatures = enabledFeatures; | ||
this.disabledFeatures = disabledFeatures; | ||
} | ||
|
||
@Override | ||
public String apply(String input) throws Exception { | ||
ObjectMapper objectMapper = makeObjectMapper(); | ||
|
||
return format(objectMapper, input); | ||
} | ||
|
||
protected ObjectMapper makeObjectMapper() { | ||
YAMLFactory yamlFactory = new YAMLFactory(); | ||
ObjectMapper objectMapper = new ObjectMapper(yamlFactory); | ||
|
||
// Configure the ObjectMapper | ||
// https://github.com/FasterXML/jackson-databind#commonly-used-features | ||
for (String rawFeature : enabledFeatures) { | ||
// https://stackoverflow.com/questions/3735927/java-instantiating-an-enum-using-reflection | ||
SerializationFeature feature = SerializationFeature.valueOf(rawFeature); | ||
|
||
objectMapper.enable(feature); | ||
} | ||
|
||
for (String rawFeature : disabledFeatures) { | ||
// https://stackoverflow.com/questions/3735927/java-instantiating-an-enum-using-reflection | ||
SerializationFeature feature = SerializationFeature.valueOf(rawFeature); | ||
|
||
objectMapper.disable(feature); | ||
} | ||
return objectMapper; | ||
} | ||
|
||
protected String format(ObjectMapper objectMapper, String input) throws IllegalArgumentException, IOException { | ||
// We may consider adding manually an initial '---' prefix to help management of multiple documents | ||
// if (!input.trim().startsWith("---")) { | ||
// input = "---" + "\n" + input; | ||
// } | ||
|
||
try { | ||
// https://stackoverflow.com/questions/25222327/deserialize-pojos-from-multiple-yaml-documents-in-a-single-file-in-jackson | ||
// https://github.com/FasterXML/jackson-dataformats-text/issues/66#issuecomment-375328648 | ||
// 2023-01: For now, we get 'Cannot deserialize value of type `com.fasterxml.jackson.databind.node.ObjectNode` from Array value' | ||
// JsonParser yamlParser = objectMapper.getFactory().createParser(input); | ||
// List<ObjectNode> docs = objectMapper.readValues(yamlParser, ObjectNode.class).readAll(); | ||
// return objectMapper.writeValueAsString(docs); | ||
|
||
// 2023-01: This returns JSON instead of YAML | ||
// This will transit with a JsonNode | ||
// A JsonNode may keep the comments from the input node | ||
// JsonNode jsonNode = objectMapper.readTree(input); | ||
//Not 'toPrettyString' as one could require no INDENT_OUTPUT | ||
// return jsonNode.toPrettyString(); | ||
ObjectNode objectNode = objectMapper.readValue(input, ObjectNode.class); | ||
return objectMapper.writeValueAsString(objectNode); | ||
} catch (JsonProcessingException e) { | ||
throw new AssertionError("Unable to format YAML. input='" + input + "'", e); | ||
} | ||
} | ||
|
||
// Spotbugs | ||
private static class ObjectNodeTypeReference extends TypeReference<ObjectNode> {} | ||
} |
85 changes: 85 additions & 0 deletions
85
lib/src/main/java/com/diffplug/spotless/yaml/YamlJacksonStep.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright 2021-2023 DiffPlug | ||
* | ||
* 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 com.diffplug.spotless.yaml; | ||
|
||
import java.io.IOException; | ||
import java.io.Serializable; | ||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import com.diffplug.spotless.FormatterFunc; | ||
import com.diffplug.spotless.FormatterStep; | ||
import com.diffplug.spotless.JarState; | ||
import com.diffplug.spotless.Provisioner; | ||
|
||
/** | ||
* Simple YAML formatter which reformats the file according to Jackson YAMLFactory. | ||
*/ | ||
// https://stackoverflow.com/questions/14515994/convert-json-string-to-pretty-print-json-output-using-jackson | ||
public class YamlJacksonStep { | ||
static final String MAVEN_COORDINATE = "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:"; | ||
// https://mvnrepository.com/artifact/com.fasterxml.jackson.dataformat/jackson-dataformat-yaml | ||
static final String DEFAULT_VERSION = "2.14.1"; | ||
|
||
private YamlJacksonStep() {} | ||
|
||
public static String defaultVersion() { | ||
return DEFAULT_VERSION; | ||
} | ||
|
||
public static FormatterStep create(List<String> enabledFeatures, | ||
List<String> disabledFeatures, | ||
String jacksonVersion, | ||
Provisioner provisioner) { | ||
Objects.requireNonNull(provisioner, "provisioner cannot be null"); | ||
return FormatterStep.createLazy("yaml", | ||
() -> new State(enabledFeatures, disabledFeatures, jacksonVersion, provisioner), | ||
State::toFormatter); | ||
} | ||
|
||
public static FormatterStep create(Provisioner provisioner) { | ||
return create(Arrays.asList("INDENT_OUTPUT"), Arrays.asList(), defaultVersion(), provisioner); | ||
} | ||
|
||
private static final class State implements Serializable { | ||
private static final long serialVersionUID = 1L; | ||
|
||
private final List<String> enabledFeatures; | ||
private final List<String> disabledFeatures; | ||
|
||
private final JarState jarState; | ||
|
||
private State(List<String> enabledFeatures, | ||
List<String> disabledFeatures, | ||
String jacksonVersion, | ||
Provisioner provisioner) throws IOException { | ||
this.enabledFeatures = enabledFeatures; | ||
this.disabledFeatures = disabledFeatures; | ||
|
||
this.jarState = JarState.from(YamlJacksonStep.MAVEN_COORDINATE + jacksonVersion, provisioner); | ||
} | ||
|
||
FormatterFunc toFormatter() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, | ||
InstantiationException, IllegalAccessException { | ||
Class<?> formatterFunc = jarState.getClassLoader().loadClass("com.diffplug.spotless.glue.yaml.YamlJacksonFormatterFunc"); | ||
Constructor<?> constructor = formatterFunc.getConstructor(List.class, List.class); | ||
return (FormatterFunc) constructor.newInstance(enabledFeatures, disabledFeatures); | ||
} | ||
} | ||
} | ||
nedtwigg marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
plugin-maven/src/main/java/com/diffplug/spotless/maven/yaml/Jackson.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright 2023 DiffPlug | ||
* | ||
* 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 com.diffplug.spotless.maven.yaml; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.apache.maven.plugins.annotations.Parameter; | ||
|
||
import com.diffplug.spotless.FormatterStep; | ||
import com.diffplug.spotless.maven.FormatterStepConfig; | ||
import com.diffplug.spotless.maven.FormatterStepFactory; | ||
import com.diffplug.spotless.yaml.YamlJacksonStep; | ||
|
||
public class Jackson implements FormatterStepFactory { | ||
|
||
@Parameter | ||
private String version = YamlJacksonStep.defaultVersion(); | ||
|
||
@Parameter | ||
private String[] enabledFeatures = new String[]{"INDENT_OUTPUT"}; | ||
|
||
@Parameter | ||
private String[] disabledFeatures = new String[0]; | ||
|
||
@Override | ||
public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) { | ||
List<String> enabledFeaturesAsList = Arrays.asList(enabledFeatures); | ||
List<String> disabledFeaturesAsList = Arrays.asList(disabledFeatures); | ||
return YamlJacksonStep | ||
.create(enabledFeaturesAsList, disabledFeaturesAsList, version, stepConfig.getProvisioner()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.