Skip to content

Commit

Permalink
Format everything (opensearch-project#2866)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephen-crawford authored Jun 16, 2023
1 parent 83573b8 commit c808692
Show file tree
Hide file tree
Showing 218 changed files with 13,406 additions and 8,840 deletions.
51 changes: 1 addition & 50 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -70,56 +70,7 @@ apply plugin: 'opensearch.opensearchplugin'
apply plugin: 'opensearch.pluginzip'
apply plugin: 'opensearch.rest-test'
apply plugin: 'opensearch.testclusters'
// apply from: 'gradle/formatting.gradle'

spotless {
java {
// Normally this isn't necessary, but we have Java sources in
// non-standard places
target '**/com/amazon/dlic/**/*.java'
target '**/com/amazon/security/**/*.java'
target '**/test/**/*.java'
target '**/integrationTest/**/*.java'

removeUnusedImports()
eclipse().configFile rootProject.file('formatter/formatterConfig.xml')
trimTrailingWhitespace()
endWithNewline();

// note: you can use an empty string for all the imports you didn't specify explicitly, and '\\#` prefix for static imports
importOrder('java', 'javax', '', 'com.amazon', 'org.opensearch', '\\#')

custom 'Refuse wildcard imports', {
// Wildcard imports can't be resolved; fail the build
if (it =~ /\s+import .*\*;/) {
throw new AssertionError("Do not use wildcard imports. 'spotlessApply' cannot resolve this issue.")
}
}

// See DEVELOPER_GUIDE.md for details of when to enable this.
if (System.getProperty('spotless.paddedcell') != null) {
paddedCell()
}
}
format 'misc', {
target '*.md', '*.gradle', '**/*.json', '**/*.yaml', '**/*.yml', '**/*.svg'

trimTrailingWhitespace()
endWithNewline()
}
format('javaFoo', JavaExtension) {

importOrder('java', 'javax', '', 'com.amazon', 'org.opensearch', '\\#')
target '**/*.java'
targetExclude '**/com/amazon/dlic/**/*.java'
targetExclude '**/com/amazon/security/**/*.java'
targetExclude '**/test/**/*.java'
targetExclude '**/integrationTest/**/*.java'

trimTrailingWhitespace()
endWithNewline();
}
}
apply from: 'gradle/formatting.gradle'

licenseFile = rootProject.file('LICENSE.txt')
noticeFile = rootProject.file('NOTICE.txt')
Expand Down
111 changes: 21 additions & 90 deletions gradle/formatting.gradle
Original file line number Diff line number Diff line change
@@ -1,95 +1,26 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you 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.
*/

import org.opensearch.gradle.BuildPlugin

/*
* This script plugin configures formatting for Java source using Spotless
* for Gradle. Since the act of formatting existing source can interfere
* with developers' workflows, we don't automatically format all code
* (yet). Instead, we maintain a list of projects that are excluded from
* formatting, until we reach a point where we can comfortably format them
* in one go without too much disruption.
*
* Any new sub-projects must not be added to the exclusions list!
*
* To perform a reformat, run:
*
* ./gradlew spotlessApply
*
* To check the current format, run:
*
* ./gradlew spotlessJavaCheck
*
* This is also carried out by the `precommit` task.
*
* For more about Spotless, see:
*
* https://github.com/diffplug/spotless/tree/master/plugin-gradle
*/

org.opensearch.gradle.BuildPlugin {
plugins.withType(BuildPlugin).whenPluginAdded {
project.apply plugin: "com.diffplug.spotless"

spotless {
java {
// Normally this isn't necessary, but we have Java sources in
// non-standard places
target '**/*.java'

removeUnusedImports()
eclipse().configFile rootProject.file('buildSrc/formatterConfig.xml')
trimTrailingWhitespace()
endWithNewline()

custom 'Refuse wildcard imports', {
// Wildcard imports can't be resolved; fail the build
if (it =~ /\s+import .*\*;/) {
throw new AssertionError("Do not use wildcard imports. 'spotlessApply' cannot resolve this issue.")
}
}

// See DEVELOPER_GUIDE.md for details of when to enable this.
if (System.getProperty('spotless.paddedcell') != null) {
paddedCell()
}
}
format 'misc', {
target '*.md', '*.gradle', '**/*.yaml', '**/*.yml', '**/*.svg'

trimTrailingWhitespace()
endWithNewline()
allprojects {
project.apply plugin: "com.diffplug.spotless"
spotless {
java {
// Normally this isn't necessary, but we have Java sources in
// non-standard places
target '**/*.java'

removeUnusedImports()
eclipse().configFile rootProject.file('formatter/formatterConfig.xml')
trimTrailingWhitespace()
endWithNewline();

// See DEVELOPER_GUIDE.md for details of when to enable this.
if (System.getProperty('spotless.paddedcell') != null) {
paddedCell()
}
}
format 'misc', {
target '*.md', '*.gradle', '**/*.json', '**/*.yaml', '**/*.yml', '**/*.svg'

precommit.dependsOn 'spotlessJavaCheck'
trimTrailingWhitespace()
endWithNewline()
}
}
}
38 changes: 22 additions & 16 deletions src/main/java/org/opensearch/security/DefaultObjectMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class DefaultObjectMapper {

static {
objectMapper.setSerializationInclusion(Include.NON_NULL);
//objectMapper.enable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS);
// objectMapper.enable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS);
objectMapper.enable(JsonParser.Feature.STRICT_DUPLICATE_DETECTION);
defaulOmittingObjectMapper.setSerializationInclusion(Include.NON_DEFAULT);
defaulOmittingObjectMapper.enable(JsonParser.Feature.STRICT_DUPLICATE_DETECTION);
Expand All @@ -75,24 +75,31 @@ public static boolean getOrDefault(Map<String, Object> properties, String key, b
if (value == null) {
return defaultValue;
} else if (value instanceof Boolean) {
return (boolean)value;
return (boolean) value;
} else if (value instanceof String) {
String text = ((String)value).trim();
String text = ((String) value).trim();
if ("true".equals(text) || "True".equals(text)) {
return true;
}
if ("false".equals(text) || "False".equals(text)) {
return false;
}
throw InvalidFormatException.from(null,
"Cannot deserialize value of type 'boolean' from String \"" + text + "\": only \"true\" or \"false\" recognized)",
null, Boolean.class);
throw InvalidFormatException.from(
null,
"Cannot deserialize value of type 'boolean' from String \"" + text + "\": only \"true\" or \"false\" recognized)",
null,
Boolean.class
);
}
throw MismatchedInputException.from(null, Boolean.class, "Cannot deserialize instance of 'boolean' out of '" + value + "' (Property: " + key + ")");
throw MismatchedInputException.from(
null,
Boolean.class,
"Cannot deserialize instance of 'boolean' out of '" + value + "' (Property: " + key + ")"
);
}

public static <T> T getOrDefault(Map<String, Object> properties, String key, T defaultValue) {
T value = (T)properties.get(key);
T value = (T) properties.get(key);
return value != null ? value : defaultValue;
}

Expand Down Expand Up @@ -172,7 +179,7 @@ public static String writeValueAsString(Object value, boolean omitDefaults) thro
return AccessController.doPrivileged(new PrivilegedExceptionAction<String>() {
@Override
public String run() throws Exception {
return (omitDefaults?defaulOmittingObjectMapper:objectMapper).writeValueAsString(value);
return (omitDefaults ? defaulOmittingObjectMapper : objectMapper).writeValueAsString(value);
}
});
} catch (final PrivilegedActionException e) {
Expand Down Expand Up @@ -229,12 +236,11 @@ public static TypeFactory getTypeFactory() {
}

public static Set<String> getFields(Class cls) {
return objectMapper
.getSerializationConfig()
.introspect(getTypeFactory().constructType(cls))
.findProperties()
.stream()
.map(BeanPropertyDefinition::getName)
.collect(ImmutableSet.toImmutableSet());
return objectMapper.getSerializationConfig()
.introspect(getTypeFactory().constructType(cls))
.findProperties()
.stream()
.map(BeanPropertyDefinition::getName)
.collect(ImmutableSet.toImmutableSet());
}
}
Loading

0 comments on commit c808692

Please sign in to comment.