Skip to content

Commit

Permalink
Common code for generating schema file names (#267)
Browse files Browse the repository at this point in the history
* Common code for generating schema file names
  • Loading branch information
big-andy-coates authored Jan 17, 2024
1 parent e907364 commit 6072561
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 4 deletions.
3 changes: 1 addition & 2 deletions type/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ val spotBugsVersion : String by extra

dependencies {
api(project(":annotation"))

implementation("com.github.spotbugs:spotbugs-annotations:$spotBugsVersion")
api("com.github.spotbugs:spotbugs-annotations:$spotBugsVersion")

// Do not add any other non-test runtime dependencies
}
5 changes: 3 additions & 2 deletions type/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/** Module containing types used by many other Creek modules */
module creek.base.type {
requires transitive creek.base.annotation;
requires com.github.spotbugs.annotations;
requires transitive com.github.spotbugs.annotations;

exports org.creekservice.api.base.type;
exports org.creekservice.api.base.type.config;
exports org.creekservice.api.base.type.temporal;
exports org.creekservice.api.base.type.json;
exports org.creekservice.api.base.type.temporal;
exports org.creekservice.api.base.type.schema;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2024 Creek Contributors (https://github.com/creek-service)
*
* 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.creekservice.api.base.type.schema;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
* Utility class for working with {@link
* org.creekservice.api.base.annotation.schema.GeneratesSchema} types.
*/
public final class GeneratedSchemas {

private GeneratedSchemas() {}

/**
* The schema filename for a given type.
*
* @param type the type
* @param extension the expected file extension, e.g. {@code ".yml"}.
* @return the filename the schema will be stored in.
*/
@SuppressFBWarnings(value = "PATH_TRAVERSAL_IN", justification = "False positive")
public static Path schemaFileName(final Class<?> type, final String extension) {
final String name =
type.getName()
.replaceAll("([A-Z])", "_$1")
.replaceFirst("_", "")
.replaceAll("\\$_", "\\$")
.toLowerCase();

return Paths.get(name + extension);
}

/**
* @return the file extension used for YAML files.
*/
public static String yamlExtension() {
return ".yml";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2024 Creek Contributors (https://github.com/creek-service)
*
* 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.creekservice.api.base.type.schema;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

import java.nio.file.Path;
import org.junit.jupiter.api.Test;

class GeneratedSchemasTest {

@Test
void shouldGenerateFileName() {
assertThat(
GeneratedSchemas.schemaFileName(
GeneratedSchemasTest.class, GeneratedSchemas.yamlExtension()),
is(Path.of("org.creekservice.api.base.type.schema.generated_schemas_test.yml")));
}

@Test
void shouldWriteSchemaForNestedType() {
assertThat(
GeneratedSchemas.schemaFileName(Nested.class, GeneratedSchemas.yamlExtension()),
is(
Path.of(
"org.creekservice.api.base.type.schema.generated_schemas_test$nested.yml")));
}

@Test
void shouldWriteSchemaForLocalType() {
// Given:
final class Model {}

// Then:
assertThat(
GeneratedSchemas.schemaFileName(Model.class, GeneratedSchemas.yamlExtension()),
is(
Path.of(
"org.creekservice.api.base.type.schema.generated_schemas_test$1_model.yml")));
}

private static final class Nested {}
}

0 comments on commit 6072561

Please sign in to comment.