Skip to content

Commit e102a80

Browse files
committed
feat: add InputParameters interface
1 parent fa0f358 commit e102a80

File tree

4 files changed

+95
-14
lines changed

4 files changed

+95
-14
lines changed

src/main/java/es/nachobrito/jsonschema/compiler/domain/Compiler.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,31 +26,42 @@
2626
import java.lang.classfile.ClassFile;
2727
import java.lang.constant.ClassDesc;
2828
import java.net.URI;
29-
import java.nio.file.Path;
3029
import java.util.SortedMap;
3130

3231
public class Compiler {
33-
private final Path destinationFolder;
34-
private final String packageName;
32+
private final InputParameters inputParameters;
3533
private final SchemaReader schemaReader;
3634

37-
public Compiler(Path destinationFolder, String packageName, SchemaReader schemaReader) {
38-
this.destinationFolder = destinationFolder;
39-
this.packageName = packageName;
35+
public Compiler(InputParameters inputParameters, SchemaReader schemaReader) {
36+
this.inputParameters = inputParameters;
4037
this.schemaReader = schemaReader;
4138
}
4239

40+
/**
41+
* Compiles the schema defined in a file, represented by the schemaURI param
42+
*
43+
* @param schemaURI the uri of the file to compile
44+
*/
4345
public void compile(URI schemaURI) {
4446
compile(schemaReader.read(schemaURI));
4547
}
4648

49+
/**
50+
* Compiles the schema defined in a String with the JSON
51+
*
52+
* @param jsonSchema the schema definition
53+
*/
4754
public void compile(String jsonSchema) {
4855
compile(schemaReader.read(jsonSchema));
4956
}
5057

5158
private void compile(Schema schema) {
52-
var className = this.packageName + schema.className();
53-
var destinationPath = destinationFolder;
59+
var className =
60+
inputParameters
61+
.getPackageName()
62+
.map(pkg -> "%s.%s".formatted(pkg, schema.className()))
63+
.orElse(schema.className());
64+
var destinationPath = inputParameters.getOutputFolder();
5465
var properties = schema.properties();
5566

5667
try {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2024 Nacho Brito
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package es.nachobrito.jsonschema.compiler.domain;
18+
19+
import java.nio.file.Path;
20+
import java.util.Optional;
21+
22+
public interface InputParameters {
23+
/**
24+
* @return the base package name for generated classes
25+
*/
26+
default Optional<String> getPackageName() {
27+
return Optional.empty();
28+
}
29+
30+
/**
31+
* @return the output folder for generated class files
32+
*/
33+
default Path getOutputFolder() {
34+
return Path.of(".");
35+
}
36+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2024 Nacho Brito
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package es.nachobrito.jsonschema.compiler.domain;
18+
19+
import java.nio.file.Path;
20+
import java.util.Optional;
21+
22+
public record InputParametersRecord(Path destPath, String packageName) implements InputParameters {
23+
@Override
24+
public Optional<String> getPackageName() {
25+
return (packageName == null || packageName.isEmpty())
26+
? Optional.empty()
27+
: Optional.of(packageName);
28+
}
29+
30+
@Override
31+
public Path getOutputFolder() {
32+
return destPath;
33+
}
34+
}

src/test/java/es/nachobrito/jsonschema/compiler/CompilerTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@
1515
*/
1616
package es.nachobrito.jsonschema.compiler;
1717

18+
import static org.junit.jupiter.api.Assertions.assertTrue;
19+
1820
import es.nachobrito.jsonschema.compiler.domain.Compiler;
21+
import es.nachobrito.jsonschema.compiler.domain.InputParametersRecord;
1922
import es.nachobrito.jsonschema.compiler.infrastructure.jsonrefparser.JsonSchemaReader;
20-
import org.junit.jupiter.api.BeforeAll;
21-
2223
import java.io.IOException;
2324
import java.lang.reflect.InvocationTargetException;
2425
import java.net.URI;
2526
import java.net.URL;
2627
import java.net.URLClassLoader;
2728
import java.nio.file.Files;
2829
import java.nio.file.Path;
29-
30-
import static org.junit.jupiter.api.Assertions.assertTrue;
30+
import org.junit.jupiter.api.BeforeAll;
3131

3232
public class CompilerTest {
3333
/**
@@ -72,7 +72,7 @@ protected static Class<?> compileSampleSchemaFromFile(String filePath, String ex
7272
.toURL();
7373
var destPath =
7474
Path.of("%s/%s.class".formatted(CompilerSmokeTest.TARGET_GENERATED_CLASSES, expectedName));
75-
var compiler = new Compiler(destPath, "", new JsonSchemaReader());
75+
var compiler = new Compiler(new InputParametersRecord(destPath, ""), new JsonSchemaReader());
7676

7777
Files.deleteIfExists(destPath);
7878
compiler.compile(uri);
@@ -99,7 +99,7 @@ protected static Class<?> compileSampleSchemaFromString(String jsonSchema, Strin
9999
.toURL();
100100
var destPath =
101101
Path.of("%s/%s.class".formatted(CompilerSmokeTest.TARGET_GENERATED_CLASSES, expectedName));
102-
var compiler = new Compiler(destPath, "", new JsonSchemaReader());
102+
var compiler = new Compiler(new InputParametersRecord(destPath, ""), new JsonSchemaReader());
103103

104104
Files.deleteIfExists(destPath);
105105
compiler.compile(jsonSchema);

0 commit comments

Comments
 (0)