Skip to content

Commit

Permalink
Introduce model annotations (for Lombok) #10
Browse files Browse the repository at this point in the history
  • Loading branch information
kobylynskyi committed Dec 5, 2019
1 parent 8e332ed commit 41cbfae
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Please refer to:
| modelValidationAnnotation | String | @javax.validation.<br>constraints.NotNull | Annotation for mandatory (NonNull) fields. Can be null/empty. |
| modelNamePrefix | String | Empty | Sets the prefix for GraphQL model classes (type, input, interface, enum, union). |
| modelNameSuffix | String | Empty | Sets the suffix for GraphQL model classes (type, input, interface, enum, union). |
| modelAnnotations | String | Empty | Annotations that will be added to model classes (type, input). |
| generateEqualsAndHashCode | Boolean | False | Specifies whether generated model classes should have equals and hashCode methods defined. |
| generateToString | Boolean | False | Specifies whether generated model classes should have toString method defined. |

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ public static Map<String, Object> map(MappingConfig mappingConfig, InputObjectTy
dataModel.put(PACKAGE, packageName);
dataModel.put(IMPORTS, MapperUtils.getImports(mappingConfig, packageName));
dataModel.put(CLASS_NAME, MapperUtils.getClassNameWithPrefixAndSuffix(mappingConfig, typeDefinition));
dataModel.put(ANNOTATIONS, mappingConfig.getModelAnnotations());
dataModel.put(NAME, typeDefinition.getName());
dataModel.put(FIELDS, InputValueDefinitionToParameterMapper.map(mappingConfig, typeDefinition.getInputValueDefinitions(), typeDefinition.getName()));
dataModel.put(EQUALS_AND_HASH_CODE, mappingConfig.getGenerateEqualsAndHashCode());
dataModel.put(TO_STRING, mappingConfig.getGenerateToString());

return dataModel;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,17 @@ public static Map<String, Object> map(MappingConfig mappingConfig, ObjectTypeDef
dataModel.put(PACKAGE, packageName);
dataModel.put(IMPORTS, MapperUtils.getImports(mappingConfig, packageName));
dataModel.put(CLASS_NAME, MapperUtils.getClassNameWithPrefixAndSuffix(mappingConfig, typeDefinition));
Set<String> allInterfaces = new LinkedHashSet<>();
allInterfaces.addAll(MapperUtils.getUnionsHavingType(mappingConfig, typeDefinition, document));
dataModel.put(ANNOTATIONS, mappingConfig.getModelAnnotations());
Set<String> allInterfaces = new LinkedHashSet<>(
MapperUtils.getUnionsHavingType(mappingConfig, typeDefinition, document));
typeDefinition.getImplements().stream()
.map(anImplement -> GraphqlTypeToJavaTypeMapper.getJavaType(mappingConfig, anImplement))
.forEach(allInterfaces::add);
dataModel.put(IMPLEMENTS, allInterfaces);

Set<ParameterDefinition> allParameters = new LinkedHashSet<>();
// Merge attributes from the type and attributes from the interface
allParameters.addAll(FieldDefinitionToParameterMapper.map(mappingConfig, typeDefinition.getFieldDefinitions(), typeDefinition.getName()));
Set<ParameterDefinition> allParameters = new LinkedHashSet<>(FieldDefinitionToParameterMapper
.map(mappingConfig, typeDefinition.getFieldDefinitions(), typeDefinition.getName()));
List<InterfaceTypeDefinition> interfaces = getInterfacesOfType(mappingConfig, typeDefinition, document);
interfaces.stream()
.map(i -> FieldDefinitionToParameterMapper.map(mappingConfig, i.getFieldDefinitions(), i.getName()))
Expand All @@ -51,7 +52,6 @@ public static Map<String, Object> map(MappingConfig mappingConfig, ObjectTypeDef
dataModel.put(EQUALS_AND_HASH_CODE, mappingConfig.getGenerateEqualsAndHashCode());
dataModel.put(TO_STRING, mappingConfig.getGenerateToString());


return dataModel;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public final class DataModelFields {
public static final String NAME = "name";
public static final String FIELDS = "fields";
public static final String IMPLEMENTS = "implements";
public static final String ANNOTATIONS = "annotations";
public static final String OPERATIONS = "operations";
public static final String EQUALS_AND_HASH_CODE = "equalsAndHashCode";
public static final String TO_STRING = "toString";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.kobylynskyi.graphql.codegen.model;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import lombok.Data;

Expand All @@ -27,6 +29,11 @@ public class MappingConfig implements Combinable<MappingConfig> {
*/
private Map<String, String> customAnnotationsMapping = new HashMap<>();

/**
* Custom annotations for model classes.
*/
private Set<String> modelAnnotations = new HashSet<>();

private Boolean generateApis;
private String packageName;
private String apiPackageName;
Expand Down Expand Up @@ -67,6 +74,11 @@ public void combine(MappingConfig source) {
} else if (this.customAnnotationsMapping == null && source.customAnnotationsMapping != null) {
this.customAnnotationsMapping = source.customAnnotationsMapping;
}
if (this.modelAnnotations != null && source.modelAnnotations != null) {
this.modelAnnotations.addAll(source.modelAnnotations);
} else if (this.modelAnnotations == null && source.modelAnnotations != null) {
this.modelAnnotations = source.modelAnnotations;
}
this.generateApis = source.generateApis != null ? source.generateApis : this.generateApis;
this.packageName = source.packageName != null ? source.packageName : this.packageName;
this.apiPackageName = source.apiPackageName != null ? source.apiPackageName : this.apiPackageName;
Expand All @@ -76,7 +88,6 @@ public void combine(MappingConfig source) {
this.modelValidationAnnotation = source.modelValidationAnnotation != null ? source.modelValidationAnnotation : this.modelValidationAnnotation;
this.generateEqualsAndHashCode = source.generateEqualsAndHashCode != null ? source.generateEqualsAndHashCode : this.generateEqualsAndHashCode;
this.generateToString = source.generateToString != null ? source.generateToString : this.generateToString;

}
}
}
3 changes: 3 additions & 0 deletions src/main/resources/templates/javaClassGraphqlType.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ package ${package};
import ${import}.*;
</#list>

<#list annotations as annotation>
@${annotation}
</#list>
public class ${className} <#if implements?has_content>implements <#list implements as interface>${interface}<#if interface_has_next>, </#if></#list></#if>{

<#list fields as field>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,19 @@ void generate_CustomAnnotationMappings() throws Exception {
System.lineSeparator() + " private org.joda.time.DateTime createdDateTime;"));
}

@Test
void generate_ModelAnnotations() throws Exception {
mappingConfig.setModelAnnotations(new HashSet<>(Arrays.asList("lombok.Builder", "lombok.Builder", "lombok.Data")));

generator.generate();

File eventFile = Arrays.stream(Objects.requireNonNull(outputJavaClassesDir.listFiles()))
.filter(file -> file.getName().equalsIgnoreCase("Event.java"))
.findFirst().orElseThrow(FileNotFoundException::new);
assertEquals(Utils.getFileContent(new File("src/test/resources/expected-classes/Event_with_lombok_annotations.java.txt").getPath()),
Utils.getFileContent(eventFile.getPath()));
}

@Test
void generate_CustomAnnotationMappings_FieldType() throws Exception {
mappingConfig.setCustomTypesMapping(new HashMap<>(Collections.singletonMap(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.kobylynskyi.graphql.test1;

import java.util.*;

@lombok.Builder
@lombok.Data
public class Event {

private String id;
private String categoryId;
private Collection<EventProperty> properties;
private EventStatus status;
private String createdBy;
private String createdDateTime;
private Boolean active;
private Integer rating;

public Event() {
}

public Event(String id, String categoryId, Collection<EventProperty> properties, EventStatus status, String createdBy, String createdDateTime, Boolean active, Integer rating) {
this.id = id;
this.categoryId = categoryId;
this.properties = properties;
this.status = status;
this.createdBy = createdBy;
this.createdDateTime = createdDateTime;
this.active = active;
this.rating = rating;
}

public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}

public String getCategoryId() {
return categoryId;
}
public void setCategoryId(String categoryId) {
this.categoryId = categoryId;
}

public Collection<EventProperty> getProperties() {
return properties;
}
public void setProperties(Collection<EventProperty> properties) {
this.properties = properties;
}

public EventStatus getStatus() {
return status;
}
public void setStatus(EventStatus status) {
this.status = status;
}

public String getCreatedBy() {
return createdBy;
}
public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}

public String getCreatedDateTime() {
return createdDateTime;
}
public void setCreatedDateTime(String createdDateTime) {
this.createdDateTime = createdDateTime;
}

public Boolean getActive() {
return active;
}
public void setActive(Boolean active) {
this.active = active;
}

public Integer getRating() {
return rating;
}
public void setRating(Integer rating) {
this.rating = rating;
}

}

0 comments on commit 41cbfae

Please sign in to comment.