Skip to content

Commit 6a457bd

Browse files
Fix Scala ParameterizedInput issues and optimize templates (#588)
1 parent a7a529a commit 6a457bd

File tree

50 files changed

+87
-82
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+87
-82
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ protected void initCustomTypeMappings(Collection<ExtendedScalarTypeDefinition> s
6464
super.initCustomTypeMappings(scalarTypeDefinitions);
6565
mappingConfig.putCustomTypeMappingIfAbsent("ID", String.class.getSimpleName());
6666
mappingConfig.putCustomTypeMappingIfAbsent("String", String.class.getSimpleName());
67-
mappingConfig.putCustomTypeMappingIfAbsent("Int", "Option[Int]");
67+
mappingConfig.putCustomTypeMappingIfAbsent("Int", "scala.Option[Int]");
6868
mappingConfig.putCustomTypeMappingIfAbsent("Int!", "Int");
6969
mappingConfig.putCustomTypeMappingIfAbsent("Float",
70-
Utils.wrapString(Double.class.getSimpleName(), "Option[", "]"));
70+
Utils.wrapString(Double.class.getSimpleName(), "scala.Option[", "]"));
7171
mappingConfig.putCustomTypeMappingIfAbsent("Float!", Double.class.getSimpleName());
7272
mappingConfig.putCustomTypeMappingIfAbsent("Boolean",
73-
Utils.wrapString(Boolean.class.getSimpleName(), "Option[", "]"));
73+
Utils.wrapString(Boolean.class.getSimpleName(), "scala.Option[", "]"));
7474
mappingConfig.putCustomTypeMappingIfAbsent("Boolean!", Boolean.class.getSimpleName());
7575
}
7676

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
*/
1919
public class ScalaGraphQLTypeMapper implements GraphQLTypeMapper {
2020

21-
private static final String SCALA_UTIL_LIST = "Seq";
22-
private static final String SCALA_UTIL_OPTIONAL = "Option";
21+
private static final String SCALA_UTIL_LIST = "scala.Seq";
22+
private static final String SCALA_UTIL_OPTIONAL = "scala.Option";
2323
private static final Set<String> SCALA_PRIMITIVE_TYPES = new HashSet<>(asList(
2424
"Byte", "Short", "Int", "Long", "Float", "Double", "Char", "Boolean"));
2525

@@ -41,6 +41,10 @@ public static boolean isScalaCollection(String scalaType) {
4141
return scalaType.startsWith(SCALA_UTIL_LIST + "[") && scalaType.endsWith("]");
4242
}
4343

44+
public static String getGenericParameter(String scalaType) {
45+
return scalaType.substring(SCALA_UTIL_LIST.length() + 1, scalaType.length() - 1);
46+
}
47+
4448
@Override
4549
public String wrapIntoList(MappingContext mappingContext, String type, boolean mandatory) {
4650
return getGenericsString(mappingContext, SCALA_UTIL_LIST, type);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ public class ScalaValueFormatter implements ValueFormatter {
1111

1212
@Override
1313
public String getEmptyListValue() {
14-
return "Seq.empty";
14+
return "scala.Seq.empty";
1515
}
1616

1717
@Override
1818
public StringJoiner getListJoiner() {
19-
return new StringJoiner(", ", "Seq(", ")");
19+
return new StringJoiner(", ", "scala.Seq(", ")");
2020
}
2121

2222
@Override
2323
public StringJoiner getArrayJoiner() {
24-
return new StringJoiner(", ", "Array(", ")");
24+
return new StringJoiner(", ", "scala.Array(", ")");
2525
}
2626

2727
}

src/main/resources/templates/scala-lang/scalaClassGraphqlInterface.ftl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<#assign MapperUtil=statics["com.kobylynskyi.graphql.codegen.scala.ScalaGraphQLTypeMapper"]>
12
<#if package?has_content>
23
package ${package}
34

@@ -11,8 +12,8 @@ import ${import}._
1112
<#if enumImportItSelfInScala?has_content>
1213
<#list fields as field>
1314
<#list enumImportItSelfInScala as enum>
14-
<#if field.type?contains("Seq[")>
15-
<#if enum == field.type?replace("Seq[", "")?replace("]", "")>
15+
<#if MapperUtil.isScalaCollection(field.type)>
16+
<#if enum == MapperUtil.getGenericParameter(field.type)>
1617
import ${enum}._
1718
</#if>
1819
<#else >

src/main/resources/templates/scala-lang/scalaClassGraphqlOperations.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import ${import}._
1313
<#list operation.parameters as param>
1414
<#list enumImportItSelfInScala as enum>
1515
<#if MapperUtil.isScalaCollection(param.type)>
16-
<#if enum == param.type?replace("Seq[", "")?replace("]", "")>
16+
<#if enum == MapperUtil.getGenericParameter(param.type)>
1717
<#if !waitImports?seq_contains(enum)>
1818
<#assign waitImports = waitImports + [enum] />
1919
</#if>

src/main/resources/templates/scala-lang/scalaClassGraphqlParametrizedInput.ftl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import scala.collection.JavaConverters._
1111
<#list fields as field>
1212
<#list enumImportItSelfInScala as enum>
1313
<#if MapperUtil.isScalaCollection(field.type)>
14-
<#if enum == field.type?replace("Seq[", "")?replace("]", "")>
14+
<#if enum == MapperUtil.getGenericParameter(field.type)>
1515
import ${enum}._
1616
</#if>
1717
<#else >
@@ -54,9 +54,9 @@ case class ${className}(
5454
</#if>
5555
) extends GraphQLParametrizedInput {
5656

57-
override def toString(): String = {<#--There is no Option[Seq[T]]-->
57+
override def toString(): String = {<#--There is no Option[Seq[T]], Format is not supported in the generated code, so it is very difficult to write template for this format.-->
5858
<#if fields?has_content>
59-
Seq(<#list fields as field><#assign getMethod = ".get"><#assign asJava = ".asJava">
59+
scala.Seq(<#list fields as field><#assign getMethod = ".get"><#assign asJava = ".asJava">
6060
<#if MapperUtil.isScalaPrimitive(field.type)>"${field.originalName}: " + GraphQLRequestSerializer.getEntry(${field.name})<#if field_has_next>,</#if><#elseif MapperUtil.isScalaOption(field.type)>if (${field.name}.isDefined) "${field.originalName}: " + GraphQLRequestSerializer.getEntry(${field.name}${getMethod}) else ""<#if field_has_next>,</#if><#else>if (${field.name} != null)<#if MapperUtil.isScalaCollection(field.type)> "${field.originalName}: " + GraphQLRequestSerializer.getEntry(${field.name}${asJava}) else ""<#if field_has_next>,</#if><#else> "${field.originalName}: " + GraphQLRequestSerializer.getEntry(${field.name}) else ""<#if field_has_next>,</#if></#if></#if></#list>
6161
).filter(_ != "").mkString("(", ",", ")")
6262
<#else>

src/main/resources/templates/scala-lang/scalaClassGraphqlRequest.ftl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ package ${package}
66
import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLOperation
77
import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLOperationRequest
88
import java.util.{ LinkedHashMap => JLinkedHashMap }
9-
import java.util.{ Map => JMap }
9+
import java.util.{ Map => JMap, Set => JSet }
1010
<#if toString || equalsAndHashCode>
1111
import java.util.Objects
1212
</#if>
@@ -17,7 +17,7 @@ import scala.collection.JavaConverters._
1717
<#list fields as field>
1818
<#list enumImportItSelfInScala as enum>
1919
<#if MapperUtil.isScalaCollection(field.type)>
20-
<#if enum == field.type?replace("Seq[", "")?replace("]", "")>
20+
<#if enum == MapperUtil.getGenericParameter(field.type)>
2121
import ${enum}._
2222
</#if>
2323
<#else >
@@ -85,7 +85,7 @@ class ${className}(alias: String) extends GraphQLOperationRequest {
8585

8686
override def getInput(): JMap[String, java.lang.Object] = input
8787

88-
override def getUseObjectMapperForInputSerialization(): java.util.Set[String] = useObjectMapperForInputSerialization.asJava
88+
override def getUseObjectMapperForInputSerialization(): JSet[String] = useObjectMapperForInputSerialization.asJava
8989
<#if equalsAndHashCode>
9090

9191
override def equals(obj: Any): Boolean = {

src/main/resources/templates/scala-lang/scalaClassGraphqlType.ftl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import scala.collection.JavaConverters._
2020
<#list fields as field>
2121
<#list enumImportItSelfInScala as enum>
2222
<#if MapperUtil.isScalaCollection(field.type)>
23-
<#if enum == field.type?replace("Seq[", "")?replace("]", "")>
23+
<#if enum == MapperUtil.getGenericParameter(field.type)>
2424
import ${enum}._
2525
</#if>
2626
<#else >
@@ -88,7 +88,7 @@ import ${enum}._
8888
<#if toString>
8989
override def toString(): String = {
9090
<#if fields?has_content><#-- When you modify it, copy it out and make sure it is one line after modification, There is no Option[Seq[T]]. -->
91-
Seq(<#list fields as field><#assign getMethod = ""><#assign asJava = ""><#if MapperUtil.isScalaOption(field.type)><#assign getMethod = ".get"></#if><#if MapperUtil.isScalaCollection(field.type)><#assign asJava = ".asJava"></#if>
91+
scala.Seq(<#list fields as field><#assign getMethod = ""><#assign asJava = ""><#if MapperUtil.isScalaOption(field.type)><#assign getMethod = ".get"></#if><#if MapperUtil.isScalaCollection(field.type)><#assign asJava = ".asJava"></#if>
9292
<#if MapperUtil.isScalaPrimitive(field.type)><#if toStringForRequest>"${field.originalName}: " + GraphQLRequestSerializer.getEntry(${field.name}<#if field.serializeUsingObjectMapper>, true</#if>)<#else>"${field.originalName}: " + ${field.name}</#if><#else><#if MapperUtil.isScalaOption(field.type)>if (${field.name}.isDefined) <#else>if (${field.name} != null) </#if><#if toStringForRequest>"${field.originalName}: " + GraphQLRequestSerializer.getEntry(${field.name}${getMethod}${asJava}<#if field.serializeUsingObjectMapper>, true</#if>)<#else><#if field.type == "String"> "${field.originalName}: \"${field.name}\"" <#else> "${field.originalName}: ${field.name}"</#if></#if> else ""</#if><#if field_has_next>,</#if></#list>
9393
).filter(_ != "").mkString("{", ",", "}")
9494
<#else><#--Keep it on one line to make sure the code style remains the same-->

src/test/resources/expected-classes/scala/AddLabelsToLabelableInput.scala.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ import scala.collection.JavaConverters._
1010
case class AddLabelsToLabelableInput(
1111
clientMutationId: String,
1212
@javax.validation.constraints.NotNull
13-
labelIds: Seq[String],
13+
labelIds: scala.Seq[String],
1414
@javax.validation.constraints.NotNull
1515
labelableId: String
1616
) {
1717

1818
override def toString(): String = {
19-
Seq(
19+
scala.Seq(
2020
if (clientMutationId != null) "clientMutationId: " + GraphQLRequestSerializer.getEntry(clientMutationId) else "",
2121
if (labelIds != null) "labelIds: " + GraphQLRequestSerializer.getEntry(labelIds.asJava) else "",
2222
if (labelableId != null) "labelableId: " + GraphQLRequestSerializer.getEntry(labelableId) else ""

src/test/resources/expected-classes/scala/AddLabelsToLabelableMutationRequest.scala.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package com.github.graphql
33
import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLOperation
44
import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLOperationRequest
55
import java.util.{ LinkedHashMap => JLinkedHashMap }
6-
import java.util.{ Map => JMap }
6+
import java.util.{ Map => JMap, Set => JSet }
77
import java.util.Objects
88
import scala.collection.mutable
99
import scala.collection.JavaConverters._
@@ -29,7 +29,7 @@ class AddLabelsToLabelableMutationRequest(alias: String) extends GraphQLOperatio
2929

3030
override def getInput(): JMap[String, java.lang.Object] = input
3131

32-
override def getUseObjectMapperForInputSerialization(): java.util.Set[String] = useObjectMapperForInputSerialization.asJava
32+
override def getUseObjectMapperForInputSerialization(): JSet[String] = useObjectMapperForInputSerialization.asJava
3333

3434
override def equals(obj: Any): Boolean = {
3535
if (this == obj) {

0 commit comments

Comments
 (0)