Skip to content

Commit 7eaf0f5

Browse files
committed
Minor refactor and code-style fixes
1 parent e0b2bd6 commit 7eaf0f5

8 files changed

+55
-55
lines changed

src/main/java/com/kobylynskyi/graphql/codegen/generators/FilesGeneratorsFactory.java

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
*/
2323
public class FilesGeneratorsFactory {
2424

25+
private FilesGeneratorsFactory() {
26+
}
27+
2528
/**
2629
* Factory method for building files generators
2730
*

src/main/java/com/kobylynskyi/graphql/codegen/mapper/AnnotationsMapper.java

+13-30
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,10 @@ public List<String> getAnnotations(MappingContext mappingContext, String graphQL
8787
}
8888
}
8989

90-
// 2. Add custom annotations from the configuration
91-
List<String> typeAnnotations = getTypeAnnotations(mappingContext, graphQLTypeName, name, parentTypeName);
92-
annotations.addAll(typeAnnotations);
90+
// 2.1. Add custom annotations from the configuration for: GraphQLObjectName.fieldName
91+
annotations.addAll(getTypeAnnotationsForKey(mappingContext, parentTypeName + "." + name));
92+
// 2.2. Add custom annotations from the configuration for: GraphQLTypeName
93+
annotations.addAll(getTypeAnnotationsForKey(mappingContext, graphQLTypeName));
9394

9495
// 3. Add Jackson-related annotations
9596
annotations.addAll(getJacksonTypeIdAnnotations(mappingContext, def));
@@ -110,40 +111,22 @@ public List<String> getAnnotations(MappingContext mappingContext, String graphQL
110111
return annotations;
111112
}
112113

113-
private List<String> getTypeAnnotations(MappingContext mappingContext,
114-
String graphQLType,
115-
String fieldName,
116-
String parentTypeName) {
114+
private static List<String> getTypeAnnotationsForKey(MappingContext mappingContext,
115+
String key) {
116+
List<String> result = new ArrayList<>();
117117
Map<String, List<String>> customAnnotationsMapping = mappingContext.getCustomAnnotationsMapping();
118-
119-
List<String> typeAnnotations = new ArrayList<>();
120-
121-
if (parentTypeName != null) {
122-
String typeNameKey = parentTypeName + "." + fieldName;
123-
List<String> fullTypeAnnotations = customAnnotationsMapping.get(typeNameKey);
124-
if (!Utils.isEmpty(fullTypeAnnotations)) {
125-
typeAnnotations.addAll(fullTypeAnnotations);
126-
} else {
127-
// try finding if there's a RegEx present for this type
128-
for (Map.Entry<String, List<String>> entry : customAnnotationsMapping.entrySet()) {
129-
if (typeNameKey.matches(entry.getKey()) && !Utils.isEmpty(entry.getValue())) {
130-
typeAnnotations.addAll(entry.getValue());
131-
}
132-
}
133-
}
134-
}
135-
List<String> specificTypeAnnotations = customAnnotationsMapping.get(graphQLType);
136-
if (!Utils.isEmpty(specificTypeAnnotations)) {
137-
typeAnnotations.addAll(specificTypeAnnotations);
118+
List<String> typeAnnotations = customAnnotationsMapping.get(key);
119+
if (!Utils.isEmpty(typeAnnotations)) {
120+
result.addAll(typeAnnotations);
138121
} else {
139122
// try finding if there's a RegEx present for this type
140123
for (Map.Entry<String, List<String>> entry : customAnnotationsMapping.entrySet()) {
141-
if (graphQLType.matches(entry.getKey()) && !Utils.isEmpty(entry.getValue())) {
142-
typeAnnotations.addAll(entry.getValue());
124+
if (key.matches(entry.getKey()) && !Utils.isEmpty(entry.getValue())) {
125+
result.addAll(entry.getValue());
143126
}
144127
}
145128
}
146-
return typeAnnotations;
129+
return result;
147130
}
148131

149132
private String getModelValidationAnnotation(MappingContext mappingContext, String graphQLTypeName) {

src/main/java/com/kobylynskyi/graphql/codegen/model/MappingConfigDefaultValuesInitializer.java

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
*/
66
public class MappingConfigDefaultValuesInitializer {
77

8+
private MappingConfigDefaultValuesInitializer() {
9+
}
10+
811
/**
912
* Initializes mapping config with default values
1013
*

src/main/java/com/kobylynskyi/graphql/codegen/model/MappingConfigValidator.java

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
*/
88
public class MappingConfigValidator {
99

10+
private MappingConfigValidator() {
11+
}
12+
1013
/**
1114
* Validator of the mapping config
1215
*

src/main/java/com/kobylynskyi/graphql/codegen/model/MappingContext.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@ private MappingContext(File outputDirectory,
4747
this.dataModelMapperFactory = dataModelMapperFactory;
4848
}
4949

50-
public static MappingContext.Builder builder() {
51-
return new MappingContext.Builder();
52-
}
53-
5450
@Override
5551
public GeneratedLanguage getGeneratedLanguage() {
5652
return config.getGeneratedLanguage();
@@ -378,6 +374,10 @@ private List<ParameterDefinition> getFields(List<ExtendedFieldDefinition> fieldD
378374
.mapFields(this, fieldDefinitions, parentDefinition);
379375
}
380376

377+
public static MappingContext.Builder builder() {
378+
return new MappingContext.Builder();
379+
}
380+
381381
/**
382382
* Builder of the mapping context
383383
*/
@@ -389,7 +389,7 @@ public static class Builder {
389389
private GeneratedInformation generatedInformation;
390390
private DataModelMapperFactory dataModelMapperFactory;
391391

392-
public Builder() {
392+
private Builder() {
393393
}
394394

395395
public Builder setOutputDirectory(File outputDirectory) {

src/main/java/com/kobylynskyi/graphql/codegen/model/builders/DeprecatedDefinitionBuilder.java

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ public class DeprecatedDefinitionBuilder {
1919
private static final String JAVA_ANNOTATION = "Deprecated";
2020
private static final String KOTLIN_ANNOTATION = "Deprecated";
2121

22+
private DeprecatedDefinitionBuilder() {
23+
}
24+
2225
/**
2326
* Get a definition of @deprecated based on a given directive
2427
*

src/main/java/com/kobylynskyi/graphql/codegen/model/builders/JavaDocBuilder.java

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
*/
1919
public class JavaDocBuilder {
2020

21+
private JavaDocBuilder() {
22+
}
23+
2124
/**
2225
* Get java doc from description of the definition and it's extensions.
2326
* If no description is present in the definition and extension then return from comments

src/main/java/com/kobylynskyi/graphql/codegen/scala/ScalaAnnotationsMapper.java

+22-20
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
import com.kobylynskyi.graphql.codegen.model.MappingContext;
77
import com.kobylynskyi.graphql.codegen.utils.Utils;
88

9-
import java.util.ArrayList;
9+
import java.util.Collections;
1010
import java.util.List;
11+
import java.util.Set;
1112

1213
/**
1314
* Mapper class for converting GraphQL types to Scala types
@@ -33,27 +34,28 @@ public String getJacksonResolverTypeIdAnnotation(String modelPackageName) {
3334

3435
@Override
3536
public List<String> getAdditionalAnnotations(MappingContext mappingContext, String typeName) {
36-
List<String> defaults = new ArrayList<>();
37-
String typeNameWithPrefixAndSuffix = (mappingContext.getModelNamePrefix() == null ? ""
38-
: mappingContext.getModelNamePrefix())
39-
+ typeName
40-
+ (mappingContext.getModelNameSuffix() == null ? "" : mappingContext.getModelNameSuffix());
41-
boolean exists = null != mappingContext.getEnumImportItSelfInScala()
42-
&& mappingContext.getEnumImportItSelfInScala()
43-
.contains(typeNameWithPrefixAndSuffix);
44-
// todo use switch
37+
StringBuilder typeNameWithPrefixAndSuffixBuilder = new StringBuilder();
38+
if (mappingContext.getModelNamePrefix() != null) {
39+
typeNameWithPrefixAndSuffixBuilder.append(mappingContext.getModelNamePrefix());
40+
}
41+
typeNameWithPrefixAndSuffixBuilder.append(typeName);
42+
if (mappingContext.getModelNameSuffix() != null) {
43+
typeNameWithPrefixAndSuffixBuilder.append(mappingContext.getModelNameSuffix());
44+
}
45+
Set<String> enumImportItSelf = mappingContext.getEnumImportItSelfInScala();
46+
if (enumImportItSelf == null ||
47+
!enumImportItSelf.contains(typeNameWithPrefixAndSuffixBuilder.toString())) {
48+
return Collections.emptyList();
49+
}
4550
// Inspired by the pr https://github.com/kobylynskyi/graphql-java-codegen/pull/637/files
46-
if (exists) {
47-
String modelPackageName = DataModelMapper.getModelPackageName(mappingContext);
48-
if (modelPackageName == null) {
49-
modelPackageName = "";
50-
} else if (Utils.isNotBlank(modelPackageName)) {
51-
modelPackageName += ".";
52-
}
53-
defaults.add("com.fasterxml.jackson.module.scala.JsonScalaEnumeration(classOf[" + modelPackageName
54-
+ typeNameWithPrefixAndSuffix + "TypeRefer])");
51+
String modelPackageName = DataModelMapper.getModelPackageName(mappingContext);
52+
if (Utils.isNotBlank(modelPackageName)) {
53+
typeNameWithPrefixAndSuffixBuilder.insert(0, modelPackageName + ".");
5554
}
56-
return defaults;
55+
String annotation = String.format(
56+
"com.fasterxml.jackson.module.scala.JsonScalaEnumeration(classOf[%sTypeRefer])",
57+
typeNameWithPrefixAndSuffixBuilder);
58+
return Collections.singletonList(annotation);
5759
}
5860

5961
@Override

0 commit comments

Comments
 (0)