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

Add checkstyle and license header checks #73

Merged
merged 1 commit into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions checkstyle-suppressions.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0"?>

<!DOCTYPE suppressions PUBLIC
"-//Checkstyle//DTD SuppressionFilter Configuration 1.0//EN"
"https://checkstyle.org/dtds/suppressions_1_0.dtd">

<suppressions>
<suppress checks="^(?!Header$)"
files="src/main/java/com/google/cloud/solutions/spannerddl/parser/AST.*\.java$"/>
<suppress checks=".*"
files="target/generated-sources/.*$"/>
</suppressions>
15 changes: 15 additions & 0 deletions licence_header.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright 2024 Google LLC
*
* 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
*
* https://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.
*/
48 changes: 46 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<build-helper-maven-plugin.version>3.5.0</build-helper-maven-plugin.version>
<auto-value.version>1.10.4</auto-value.version>
<error-prone.version>2.24.1</error-prone.version>
<maven-checkstyle.version>3.3.1</maven-checkstyle.version>
<maven-checkstyle-plugin.version>3.3.1</maven-checkstyle-plugin.version>
</properties>
<dependencies>
<dependency>
Expand Down Expand Up @@ -134,7 +134,51 @@
</annotationProcessorPaths>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>${maven-checkstyle-plugin.version}</version>
<configuration>
<encoding>UTF-8</encoding>
<consoleOutput>true</consoleOutput>
<failsOnError>true</failsOnError>
<failOnViolation>true</failOnViolation>
<linkXRef>false</linkXRef>
<suppressionsFileExpression>checkstyle.suppressions.xml</suppressionsFileExpression>
<excludeGeneratedSources>true</excludeGeneratedSources>
<violationSeverity>warning</violationSeverity>
</configuration>
<executions>
<execution>
<id>google-checkstyle</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<configLocation>google_checks.xml</configLocation>
</configuration>
</execution>
<execution>
<id>licence-header</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<checkstyleRules>
<module name="Checker">
<module name="Header">
<property name="headerFile" value="licence_header.java"></property>
<property name="ignoreLines" value="2"></property>
<property name="fileExtensions" value="java"></property>
<message key="header.mismatch" value="File must start with the Apache2 licence header (see licence_header.java)."></message>
<message key="header.missing" value="File must start with the Apache2 licence header (see licence_header.java)."></message>
</module>
</module>
</checkstyleRules>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
import java.util.stream.StreamSupport;

/** Utility functions for getting and casting Nodes in the parsed AST. */
public class ASTTreeUtils {
public class AstTreeUtils {

/** Gets (and casts) the first found child of a specific node type */
/** Gets (and casts) the first found child of a specific node type. */
public static <T> T getOptionalChildByType(Node[] children, Class<T> type) {
for (Node child : children) {
if (type.isInstance(child)) {
Expand All @@ -40,6 +40,10 @@ public static <T> T getOptionalChildByType(Node[] children, Class<T> type) {
return null;
}

/**
* Gets (and casts) the first found child of a specific node type, throwing an exception if it
* does not exist.
*/
public static <T> T getChildByType(Node[] children, Class<T> type) {
T child = getOptionalChildByType(children, type);
if (child == null) {
Expand All @@ -55,6 +59,7 @@ public static <T> T getChildByType(Node[] children, Class<T> type) {
.map(s -> s.substring(1, s.length() - 1))
.collect(Collectors.toSet());

/** Checks if the word is a reserved word/known token. */
public static boolean isReservedWord(String word) {
return reservedWords.contains(word);
}
Expand All @@ -69,7 +74,7 @@ public static <T> List<T> getChildrenAssertType(Node[] children, Class<T> type)
.collect(Collectors.toList());
}

private ASTTreeUtils() {}
private AstTreeUtils() {}

/**
* Generate the original parsed text between the 2 specified tokens, normalizing the text with
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2024 Google LLC
*
* 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
*
* https://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.google.cloud.solutions.spannerddl.diff;

import com.google.auto.value.AutoValue;
import com.google.cloud.solutions.spannerddl.parser.ASTcheck_constraint;
import com.google.cloud.solutions.spannerddl.parser.ASTforeign_key;
import com.google.cloud.solutions.spannerddl.parser.SimpleNode;

/**
* Wrapper class for Check and Foreign Key constraints to include the table name for when they are
* separated from their create table/alter table statements in
* separateTablesIndexesConstraintsTtls().
*/
@AutoValue
abstract class ConstraintWrapper {

static ConstraintWrapper create(String tableName, SimpleNode constraint) {
if (!(constraint instanceof ASTforeign_key) && !(constraint instanceof ASTcheck_constraint)) {
throw new IllegalArgumentException("not a valid constraint type : " + constraint.toString());
}
return new AutoValue_ConstraintWrapper(tableName, constraint);
}

abstract String tableName();

abstract SimpleNode constraint();

String getName() {
if (constraint() instanceof ASTcheck_constraint) {
return ((ASTcheck_constraint) constraint()).getName();
}
if (constraint() instanceof ASTforeign_key) {
return ((ASTforeign_key) constraint()).getName();
}
throw new IllegalArgumentException("not a valid constraint type : " + constraint().toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
* Copyright 2024 Google LLC
*
* 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
*
* https://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.google.cloud.solutions.spannerddl.diff;

import com.google.auto.value.AutoValue;
import com.google.cloud.solutions.spannerddl.parser.ASTadd_row_deletion_policy;
import com.google.cloud.solutions.spannerddl.parser.ASTalter_database_statement;
import com.google.cloud.solutions.spannerddl.parser.ASTalter_table_statement;
import com.google.cloud.solutions.spannerddl.parser.ASTcheck_constraint;
import com.google.cloud.solutions.spannerddl.parser.ASTcreate_change_stream_statement;
import com.google.cloud.solutions.spannerddl.parser.ASTcreate_index_statement;
import com.google.cloud.solutions.spannerddl.parser.ASTcreate_table_statement;
import com.google.cloud.solutions.spannerddl.parser.ASTddl_statement;
import com.google.cloud.solutions.spannerddl.parser.ASTforeign_key;
import com.google.cloud.solutions.spannerddl.parser.ASTrow_deletion_policy_clause;
import com.google.cloud.solutions.spannerddl.parser.DdlParserTreeConstants;
import com.google.cloud.solutions.spannerddl.parser.SimpleNode;
import com.google.common.collect.ImmutableMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Optional;

/**
* Separarates the different DDL creation statements into separate maps.
*
* <p>Constraints which were created inline with their table are separated into a map with any other
* ALTER statements which adds constraints.
*
* <p>This allows the diff tool to handle these objects which are created inline with the table in
* the same way as if they were created separately with ALTER statements.
*/
@AutoValue
abstract class DatbaseDefinition {
static DatbaseDefinition create(List<ASTddl_statement> statements) {
// Use LinkedHashMap to preserve creation order in original DDL.
LinkedHashMap<String, ASTcreate_table_statement> tablesInCreationOrder = new LinkedHashMap<>();
LinkedHashMap<String, ASTcreate_index_statement> indexes = new LinkedHashMap<>();
LinkedHashMap<String, ConstraintWrapper> constraints = new LinkedHashMap<>();
LinkedHashMap<String, ASTrow_deletion_policy_clause> ttls = new LinkedHashMap<>();
LinkedHashMap<String, ASTcreate_change_stream_statement> changeStreams = new LinkedHashMap<>();
LinkedHashMap<String, String> alterDatabaseOptions = new LinkedHashMap<>();

for (ASTddl_statement ddlStatement : statements) {
final SimpleNode statement = (SimpleNode) ddlStatement.jjtGetChild(0);

switch (statement.getId()) {
case DdlParserTreeConstants.JJTCREATE_TABLE_STATEMENT:
ASTcreate_table_statement createTable = (ASTcreate_table_statement) statement;
// Remove embedded constraint statements from the CreateTable node
// as they are taken into account via `constraints`
tablesInCreationOrder.put(createTable.getTableName(), createTable.clearConstraints());

// convert embedded constraint statements into wrapper object with table name
// use a single map for all foreign keys, constraints and row deletion polcies whether
// created in table or externally
createTable.getConstraints().values().stream()
.map(c -> ConstraintWrapper.create(createTable.getTableName(), c))
.forEach(c -> constraints.put(c.getName(), c));

// Move embedded Row Deletion Policies
final Optional<ASTrow_deletion_policy_clause> rowDeletionPolicyClause =
createTable.getRowDeletionPolicyClause();
rowDeletionPolicyClause.ifPresent(rdp -> ttls.put(createTable.getTableName(), rdp));
break;
case DdlParserTreeConstants.JJTCREATE_INDEX_STATEMENT:
indexes.put(
((ASTcreate_index_statement) statement).getIndexName(),
(ASTcreate_index_statement) statement);
break;
case DdlParserTreeConstants.JJTALTER_TABLE_STATEMENT:
// Alter table can be adding Index, Constraint or Row Deletion Policy
ASTalter_table_statement alterTable = (ASTalter_table_statement) statement;
final String tableName = alterTable.jjtGetChild(0).toString();

if (alterTable.jjtGetChild(1) instanceof ASTforeign_key
|| alterTable.jjtGetChild(1) instanceof ASTcheck_constraint) {
ConstraintWrapper constraint =
ConstraintWrapper.create(tableName, (SimpleNode) alterTable.jjtGetChild(1));
constraints.put(constraint.getName(), constraint);

} else if (statement.jjtGetChild(1) instanceof ASTadd_row_deletion_policy) {
ttls.put(
tableName,
(ASTrow_deletion_policy_clause) alterTable.jjtGetChild(1).jjtGetChild(0));
} else {
// other ALTER statements are not supported.
throw new IllegalArgumentException(
"Unsupported ALTER TABLE statement: " + AstTreeUtils.tokensToString(ddlStatement));
}
break;
case DdlParserTreeConstants.JJTALTER_DATABASE_STATEMENT:
alterDatabaseOptions.putAll(
((ASTalter_database_statement) statement).getOptionsClause().getKeyValueMap());
break;
case DdlParserTreeConstants.JJTCREATE_CHANGE_STREAM_STATEMENT:
changeStreams.put(
((ASTcreate_change_stream_statement) statement).getName(),
(ASTcreate_change_stream_statement) statement);
break;
default:
throw new IllegalArgumentException(
"Unsupported statement: " + AstTreeUtils.tokensToString(ddlStatement));
}
}
return new AutoValue_DatbaseDefinition(
ImmutableMap.copyOf(tablesInCreationOrder),
ImmutableMap.copyOf(indexes),
ImmutableMap.copyOf(constraints),
ImmutableMap.copyOf(ttls),
ImmutableMap.copyOf(changeStreams),
ImmutableMap.copyOf(alterDatabaseOptions));
}

abstract ImmutableMap<String, ASTcreate_table_statement> tablesInCreationOrder();

abstract ImmutableMap<String, ASTcreate_index_statement> indexes();

abstract ImmutableMap<String, ConstraintWrapper> constraints();

abstract ImmutableMap<String, ASTrow_deletion_policy_clause> ttls();

abstract ImmutableMap<String, ASTcreate_change_stream_statement> changeStreams();

abstract ImmutableMap<String, String> alterDatabaseOptions();
}
Loading
Loading