generated from creek-service/multi-module-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Common code for generating schema file names (#267)
* Common code for generating schema file names
- Loading branch information
1 parent
e907364
commit 6072561
Showing
4 changed files
with
118 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -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; | ||
} |
56 changes: 56 additions & 0 deletions
56
type/src/main/java/org/creekservice/api/base/type/schema/GeneratedSchemas.java
This file contains 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,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"; | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
type/src/test/java/org/creekservice/api/base/type/schema/GeneratedSchemasTest.java
This file contains 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,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 {} | ||
} |