Skip to content

Commit e89ddc2

Browse files
valinhakobylynskyi
authored andcommitted
External MappingConfig (#16)
1 parent f0396c6 commit e89ddc2

File tree

12 files changed

+223
-20
lines changed

12 files changed

+223
-20
lines changed

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
out
22
build
33
.idea
4-
.gradle
4+
.gradle
5+
*.iml
6+
modules.xml
7+
.idea/misc.xml
8+
*.ipr

build.gradle

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ plugins {
77
id "maven-publish"
88
}
99

10-
version = "1.2.3"
10+
version = "1.3.0"
1111
group = "io.github.kobylynskyi"
1212

1313
repositories {
@@ -18,6 +18,7 @@ repositories {
1818
dependencies {
1919
compile "org.freemarker:freemarker:2.3.28"
2020
compile "com.graphql-java:graphql-java:13.0"
21+
compile "com.google.code.gson:gson:2.8.6"
2122

2223
compileOnly "org.projectlombok:lombok:1.18.8"
2324
annotationProcessor "org.projectlombok:lombok:1.18.8"
@@ -99,6 +100,11 @@ publishing {
99100
name = "Bogdan Kobylynskyi"
100101
email = "92bogdan@gmail.com"
101102
}
103+
developer {
104+
id = "valinha"
105+
name = "Alberto Valiña"
106+
email = "valinhadev@gmail.com"
107+
}
102108
}
103109
}
104110
}

src/main/java/com/kobylynskyi/graphql/codegen/GraphqlCodegen.java

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package com.kobylynskyi.graphql.codegen;
22

33
import com.kobylynskyi.graphql.codegen.mapper.*;
4-
import com.kobylynskyi.graphql.codegen.model.DefinitionTypeDeterminer;
5-
import com.kobylynskyi.graphql.codegen.model.GraphqlDefinitionType;
6-
import com.kobylynskyi.graphql.codegen.model.MappingConfig;
7-
import com.kobylynskyi.graphql.codegen.model.UnsupportedGraphqlDefinitionException;
4+
import com.kobylynskyi.graphql.codegen.model.*;
5+
import com.kobylynskyi.graphql.codegen.supplier.MappingConfigSupplier;
86
import freemarker.template.TemplateException;
97
import graphql.language.*;
108
import lombok.Getter;
@@ -14,6 +12,7 @@
1412
import java.io.IOException;
1513
import java.util.List;
1614
import java.util.Map;
15+
import java.util.function.Supplier;
1716

1817
/**
1918
* Generator of:
@@ -25,6 +24,7 @@
2524
* - Class for each GraphQL scalar type
2625
*
2726
* @author kobylynskyi
27+
* @author valinhadev
2828
*/
2929
@Getter
3030
@Setter
@@ -33,13 +33,37 @@ public class GraphqlCodegen {
3333
private List<String> schemas;
3434
private File outputDir;
3535
private MappingConfig mappingConfig;
36+
private MappingConfig result;
3637

3738
public GraphqlCodegen(List<String> schemas, File outputDir, MappingConfig mappingConfig) {
39+
this(schemas, outputDir, mappingConfig, null);
40+
}
41+
42+
43+
public GraphqlCodegen(List<String> schemas, File outputDir, MappingConfig mappingConfig, MappingConfigSupplier externalMappingConfigSupplier) {
3844
this.schemas = schemas;
3945
this.outputDir = outputDir;
4046
this.mappingConfig = mappingConfig;
47+
this.mappingConfig.combine(externalMappingConfigSupplier != null ? externalMappingConfigSupplier.get() : null);
48+
initDefaultValues(mappingConfig);
49+
}
50+
51+
private void initDefaultValues(MappingConfig mappingConfig) {
52+
if (mappingConfig.getModelValidationAnnotation() == null) {
53+
mappingConfig.setModelValidationAnnotation(DefaultMappingConfigValues.DEFAULT_VALIDATION_ANNOTATION);
54+
}
55+
if (mappingConfig.getGenerateEqualsAndHashCode() == null) {
56+
mappingConfig.setGenerateEqualsAndHashCode(DefaultMappingConfigValues.DEFAULT_EQUALS_AND_HASHCODE);
57+
}
58+
if (mappingConfig.getGenerateToString() == null) {
59+
mappingConfig.setGenerateToString(DefaultMappingConfigValues.DEFAULT_TO_STRING);
60+
}
61+
if (mappingConfig.getGenerateApis() == null) {
62+
mappingConfig.setGenerateApis(DefaultMappingConfigValues.DEFAULT_GENERATE_APIS);
63+
}
4164
}
4265

66+
4367
public void generate() throws Exception {
4468
GraphqlCodegenFileCreator.prepareOutputDir(outputDir);
4569
for (String schema : schemas) {
@@ -94,7 +118,7 @@ private void generateInterface(InterfaceTypeDefinition definition) throws IOExce
94118
}
95119

96120
private void generateOperation(ObjectTypeDefinition definition) throws IOException, TemplateException {
97-
if (mappingConfig.isGenerateApis()) {
121+
if (Boolean.TRUE.equals(mappingConfig.getGenerateApis())) {
98122
for (FieldDefinition fieldDef : definition.getFieldDefinitions()) {
99123
Map<String, Object> dataModel = FieldDefinitionToDataModelMapper.map(mappingConfig, fieldDef, definition.getName());
100124
GraphqlCodegenFileCreator.generateFile(FreeMarkerTemplatesRegistry.operationsTemplate, dataModel, outputDir);

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ public static Map<String, Object> map(MappingConfig mappingConfig, InputObjectTy
3030
dataModel.put(CLASS_NAME, MapperUtils.getClassNameWithPrefixAndSuffix(mappingConfig, typeDefinition));
3131
dataModel.put(NAME, typeDefinition.getName());
3232
dataModel.put(FIELDS, InputValueDefinitionToParameterMapper.map(mappingConfig, typeDefinition.getInputValueDefinitions(), typeDefinition.getName()));
33-
dataModel.put(EQUALS_AND_HASH_CODE, mappingConfig.isGenerateEqualsAndHashCode());
34-
dataModel.put(TO_STRING, mappingConfig.isGenerateToString());
33+
dataModel.put(EQUALS_AND_HASH_CODE, mappingConfig.getGenerateEqualsAndHashCode());
34+
dataModel.put(TO_STRING, mappingConfig.getGenerateToString());
35+
3536
return dataModel;
3637
}
3738

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ public static Map<String, Object> map(MappingConfig mappingConfig, ObjectTypeDef
4848
.map(i -> FieldDefinitionToParameterMapper.map(mappingConfig, i.getFieldDefinitions(), i.getName()))
4949
.forEach(allParameters::addAll);
5050
dataModel.put(FIELDS, allParameters);
51-
dataModel.put(EQUALS_AND_HASH_CODE, mappingConfig.isGenerateEqualsAndHashCode());
52-
dataModel.put(TO_STRING, mappingConfig.isGenerateToString());
51+
dataModel.put(EQUALS_AND_HASH_CODE, mappingConfig.getGenerateEqualsAndHashCode());
52+
dataModel.put(TO_STRING, mappingConfig.getGenerateToString());
53+
5354

5455
return dataModel;
5556
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.kobylynskyi.graphql.codegen.model;
2+
3+
/**
4+
* The interface Combinable.
5+
*
6+
* @param <T> the type parameter
7+
* @author valinha
8+
*/
9+
@FunctionalInterface
10+
public interface Combinable<T> {
11+
/**
12+
* Combine with source.
13+
*
14+
* @param source the source
15+
*/
16+
void combine(T source);
17+
}

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

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
package com.kobylynskyi.graphql.codegen.model;
22

3-
import lombok.Data;
4-
53
import java.util.HashMap;
64
import java.util.Map;
75

6+
import lombok.Data;
7+
8+
/**
9+
* The type Mapping config.
10+
*
11+
* @author kobylynskyi
12+
* @author valinha
13+
*/
814
@Data
9-
public class MappingConfig {
15+
public class MappingConfig implements Combinable<MappingConfig> {
1016

1117
/**
1218
* Scalars mapping can be defined here.
@@ -21,16 +27,23 @@ public class MappingConfig {
2127
*/
2228
private Map<String, String> customAnnotationsMapping = new HashMap<>();
2329

24-
private boolean generateApis = DefaultMappingConfigValues.DEFAULT_GENERATE_APIS;
30+
private Boolean generateApis;
2531
private String packageName;
2632
private String apiPackageName;
2733
private String modelPackageName;
2834
private String modelNamePrefix;
2935
private String modelNameSuffix;
30-
private String modelValidationAnnotation = DefaultMappingConfigValues.DEFAULT_VALIDATION_ANNOTATION;
31-
private boolean generateEqualsAndHashCode = DefaultMappingConfigValues.DEFAULT_EQUALS_AND_HASHCODE;
32-
private boolean generateToString = DefaultMappingConfigValues.DEFAULT_TO_STRING;
36+
private String modelValidationAnnotation;
37+
private Boolean generateEqualsAndHashCode;
38+
private Boolean generateToString;
39+
3340

41+
/**
42+
* Put custom type mapping if absent.
43+
*
44+
* @param from the from
45+
* @param to the to
46+
*/
3447
public void putCustomTypeMappingIfAbsent(String from, String to) {
3548
if (customTypesMapping == null) {
3649
customTypesMapping = new HashMap<>();
@@ -40,4 +53,30 @@ public void putCustomTypeMappingIfAbsent(String from, String to) {
4053
}
4154
}
4255

56+
57+
@Override
58+
public void combine(MappingConfig source) {
59+
if (source != null) {
60+
if (this.customTypesMapping != null && source.customTypesMapping != null) {
61+
this.customTypesMapping.putAll(source.customTypesMapping);
62+
} else if (this.customTypesMapping == null && source.customTypesMapping != null) {
63+
this.customTypesMapping = source.customTypesMapping;
64+
}
65+
if (this.customAnnotationsMapping != null && source.customAnnotationsMapping != null) {
66+
this.customAnnotationsMapping.putAll(source.customAnnotationsMapping);
67+
} else if (this.customAnnotationsMapping == null && source.customAnnotationsMapping != null) {
68+
this.customAnnotationsMapping = source.customAnnotationsMapping;
69+
}
70+
this.generateApis = source.generateApis != null ? source.generateApis : this.generateApis;
71+
this.packageName = source.packageName != null ? source.packageName : this.packageName;
72+
this.apiPackageName = source.apiPackageName != null ? source.apiPackageName : this.apiPackageName;
73+
this.modelPackageName = source.modelPackageName != null ? source.modelPackageName : this.modelPackageName;
74+
this.modelNamePrefix = source.modelNamePrefix != null ? source.modelNamePrefix : this.modelNamePrefix;
75+
this.modelNameSuffix = source.modelNameSuffix != null ? source.modelNameSuffix : this.modelNameSuffix;
76+
this.modelValidationAnnotation = source.modelValidationAnnotation != null ? source.modelValidationAnnotation : this.modelValidationAnnotation;
77+
this.generateEqualsAndHashCode = source.generateEqualsAndHashCode != null ? source.generateEqualsAndHashCode : this.generateEqualsAndHashCode;
78+
this.generateToString = source.generateToString != null ? source.generateToString : this.generateToString;
79+
80+
}
81+
}
4382
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.kobylynskyi.graphql.codegen.supplier;
2+
3+
import java.io.File;
4+
import java.io.FileNotFoundException;
5+
import java.io.FileReader;
6+
7+
import com.google.gson.GsonBuilder;
8+
import com.kobylynskyi.graphql.codegen.model.MappingConfig;
9+
10+
/**
11+
* Retrieve a MappingConfig fro json configuration file.
12+
*
13+
* @author valinha
14+
*/
15+
public class JsonMappingConfigSupplier implements MappingConfigSupplier {
16+
17+
private String jsonConfigFile;
18+
19+
/**
20+
* Instantiates a new Json configuration file supplier.
21+
*
22+
* @param jsonConfigFile the json config file
23+
*/
24+
public JsonMappingConfigSupplier(String jsonConfigFile) {
25+
this.jsonConfigFile = jsonConfigFile;
26+
}
27+
28+
@Override
29+
public MappingConfig get() {
30+
if (jsonConfigFile != null && !jsonConfigFile.isEmpty()) {
31+
try {
32+
return new GsonBuilder().create().fromJson(new FileReader(new File(jsonConfigFile)), MappingConfig.class);
33+
} catch (FileNotFoundException e) {
34+
throw new IllegalArgumentException(e);
35+
}
36+
}
37+
return null;
38+
}
39+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.kobylynskyi.graphql.codegen.supplier;
2+
3+
import java.util.function.Supplier;
4+
5+
import com.kobylynskyi.graphql.codegen.model.MappingConfig;
6+
7+
/**
8+
* The interface Mapping config supplier.
9+
* @author valinha
10+
*/
11+
public interface MappingConfigSupplier extends Supplier<MappingConfig> {
12+
//mark interface
13+
}

src/main/java/com/kobylynskyi/graphql/codegen/utils/Utils.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package com.kobylynskyi.graphql.codegen.utils;
22

3-
import graphql.language.OperationDefinition;
4-
53
import java.io.File;
64
import java.io.IOException;
75
import java.nio.file.Files;
86
import java.nio.file.Paths;
97

8+
import graphql.language.OperationDefinition;
9+
1010
/**
1111
* Various utilities
1212
*
@@ -113,4 +113,6 @@ public static void createDirIfAbsent(File dir) throws IOException {
113113
throw new IOException("Unable to create output directory");
114114
}
115115
}
116+
117+
116118
}

0 commit comments

Comments
 (0)