Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
梦境迷离
Graphql Java Codegen
提交
600c902c
G
Graphql Java Codegen
项目概览
梦境迷离
/
Graphql Java Codegen
10 个月 前同步成功
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
Graphql Java Codegen
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
600c902c
编写于
2月 25, 2021
作者:
梦境迷离
提交者:
GitHub
2月 25, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix Scala and Kotlin minor code generation issues (#558)
* Optimize template * add test
上级
c8df4854
变更
11
隐藏空白更改
内联
并排
Showing
11 changed file
with
217 addition
and
15 deletion
+217
-15
src/main/java/com/kobylynskyi/graphql/codegen/scala/ScalaDataModelMapper.java
...bylynskyi/graphql/codegen/scala/ScalaDataModelMapper.java
+6
-0
src/main/resources/templates/kotlin-lang/kotlinClassGraphqlType.ftl
...esources/templates/kotlin-lang/kotlinClassGraphqlType.ftl
+4
-4
src/test/java/com/kobylynskyi/graphql/codegen/kotlin/GraphQLCodegenNullableTest.java
...yi/graphql/codegen/kotlin/GraphQLCodegenNullableTest.java
+22
-1
src/test/java/com/kobylynskyi/graphql/codegen/scala/GraphQLCodegenExtendTest.java
...nskyi/graphql/codegen/scala/GraphQLCodegenExtendTest.java
+26
-4
src/test/java/com/kobylynskyi/graphql/codegen/scala/GraphQLCodegenReactorToStringTest.java
...phql/codegen/scala/GraphQLCodegenReactorToStringTest.java
+28
-0
src/test/resources/expected-classes/kt/optional/InterfaceWithOptionalField.kt.txt
...ted-classes/kt/optional/InterfaceWithOptionalField.kt.txt
+9
-0
src/test/resources/expected-classes/kt/optional/TypeWithMandatoryField.kt.txt
...xpected-classes/kt/optional/TypeWithMandatoryField.kt.txt
+36
-0
src/test/resources/expected-classes/scala/optional/InterfaceWithOptionalField.scala.txt
...asses/scala/optional/InterfaceWithOptionalField.scala.txt
+9
-0
src/test/resources/expected-classes/scala/optional/TypeWithMandatoryField.scala.txt
...d-classes/scala/optional/TypeWithMandatoryField.scala.txt
+34
-0
src/test/resources/expected-classes/scala/tostring/Synchronized.scala.txt
...es/expected-classes/scala/tostring/Synchronized.scala.txt
+6
-6
src/test/resources/expected-classes/scala/tostring/TOSTRING_Synchronized.scala.txt
...ed-classes/scala/tostring/TOSTRING_Synchronized.scala.txt
+37
-0
未找到文件。
src/main/java/com/kobylynskyi/graphql/codegen/scala/ScalaDataModelMapper.java
浏览文件 @
600c902c
...
...
@@ -31,6 +31,12 @@ public class ScalaDataModelMapper implements DataModelMapper {
if
(
SCALA_RESTRICTED_KEYWORDS
.
contains
(
fieldName
))
{
return
wrapString
(
fieldName
,
RESTRICTED_WORDS_WRAP_WITH
);
}
// Scala case class's Fields cannot also use the names of these methods.
if
(
SCALA_RESTRICTED_METHOD_NAMES
.
contains
(
fieldName
))
{
return
Utils
.
capitalize
(
fieldName
);
}
return
fieldName
;
}
...
...
src/main/resources/templates/kotlin-lang/kotlinClassGraphqlType.ftl
浏览文件 @
600c902c
...
...
@@ -91,9 +91,9 @@ open class ${className}()<#if implements?has_content> : <#list implements as int
joiner
.
add
(
"${field.originalName}: "
+
GraphQLRequestSerializer
.
getEntry
(${
field
.
name
}))
<#
else
>
<#
if
field
.
type
==
"String?"
>
joiner
.
add
(
"${field.originalName}:
\"
${field.name}
\"
"
)
joiner
.
add
(
"${field.originalName}:
\"
"
+
${
field
.
name
}
+
"
\"
"
);
<#
else
>
joiner
.
add
(
${
field
.
originalName
}:
${
field
.
name
}
")
joiner
.
add
(
"${field.originalName}: "
+
${
field
.
name
});
</#
if
>
</#
if
>
}
...
...
@@ -102,9 +102,9 @@ open class ${className}()<#if implements?has_content> : <#list implements as int
joiner
.
add
(
"${field.originalName}: "
+
GraphQLRequestSerializer
.
getEntry
(${
field
.
name
}))
<#
else
>
<#
if
field
.
type
==
"String"
>
joiner.add("
${
field
.
originalName
}:
\
"
${field.name}
\"
"
)
joiner
.
add
(
"${field.originalName}:
\"
"
+
${
field
.
name
}
+
"
\"
"
);
<#
else
>
joiner
.
add
(
${
field
.
originalName
}:
${
field
.
name
}
")
joiner
.
add
(
"${field.originalName}: "
+
${
field
.
name
});
</#
if
>
</#
if
>
</#
if
>
...
...
src/test/java/com/kobylynskyi/graphql/codegen/kotlin/GraphQLCodegenNullableTest.java
浏览文件 @
600c902c
...
...
@@ -85,4 +85,25 @@ class GraphQLCodegenNullableTest {
new
File
(
"src/test/resources/expected-classes/kt/nullable/QueryResolver_WITH_Prefix_Suffix.kt.txt"
),
getFileByName
(
files
,
"QueryResolver.kt"
));
}
}
\ No newline at end of file
/**
* Verify Int to override Int?
*/
@Test
void
generate_OptionalFieldInInterfaceAndMandatoryInType
()
throws
Exception
{
mappingConfig
.
setGenerateBuilder
(
true
);
mappingConfig
.
setGenerateToString
(
true
);
mappingConfig
.
setGenerateEqualsAndHashCode
(
true
);
schemaFinder
.
setIncludePattern
(
"optional-vs-mandatory-types.graphqls"
);
new
KotlinGraphQLCodegen
(
schemaFinder
.
findSchemas
(),
outputBuildDir
,
mappingConfig
,
TestUtils
.
getStaticGeneratedInfo
())
.
generate
();
File
[]
files
=
Objects
.
requireNonNull
(
outputJavaClassesDir
.
listFiles
());
assertSameTrimmedContent
(
new
File
(
"src/test/resources/expected-classes/kt/optional/InterfaceWithOptionalField.kt.txt"
),
getFileByName
(
files
,
"InterfaceWithOptionalField.kt"
));
assertSameTrimmedContent
(
new
File
(
"src/test/resources/expected-classes/kt/optional/TypeWithMandatoryField.kt.txt"
),
getFileByName
(
files
,
"TypeWithMandatoryField.kt"
));
}
}
src/test/java/com/kobylynskyi/graphql/codegen/scala/GraphQLCodegenExtendTest.java
浏览文件 @
600c902c
...
...
@@ -11,10 +11,7 @@ import org.junit.jupiter.api.Test;
import
java.io.File
;
import
java.nio.file.Paths
;
import
java.util.Arrays
;
import
java.util.HashSet
;
import
java.util.Objects
;
import
java.util.Set
;
import
java.util.*
;
import
static
com
.
kobylynskyi
.
graphql
.
codegen
.
TestUtils
.
assertSameTrimmedContent
;
import
static
com
.
kobylynskyi
.
graphql
.
codegen
.
TestUtils
.
getFileByName
;
...
...
@@ -217,4 +214,29 @@ class GraphQLCodegenExtendTest {
new
File
(
"src/test/resources/expected-classes/scala/extend/nullable/QueryResolver.scala.txt"
),
getFileByName
(
files
,
"QueryResolver.scala"
));
}
/**
* Verify Int! to override Int in graphql.
* In scala, Int type cannot be null, So, option Int is Option[Int]. Can not override from Option[Int] to Int.
*/
@Test
void
generate_OptionalFieldInInterfaceAndMandatoryInType
()
throws
Exception
{
mappingConfig
.
setGenerateBuilder
(
true
);
mappingConfig
.
setGenerateToString
(
true
);
mappingConfig
.
setGenerateEqualsAndHashCode
(
true
);
Map
<
String
,
String
>
maps
=
new
HashMap
<>();
maps
.
put
(
"Int"
,
"Int"
);
// Now, That's the only way to do it.
mappingConfig
.
setCustomTypesMapping
(
maps
);
schemaFinder
.
setIncludePattern
(
"optional-vs-mandatory-types.graphqls"
);
new
ScalaGraphQLCodegen
(
schemaFinder
.
findSchemas
(),
outputBuildDir
,
mappingConfig
,
TestUtils
.
getStaticGeneratedInfo
())
.
generate
();
File
[]
files
=
Objects
.
requireNonNull
(
outputJavaClassesDir
.
listFiles
());
assertSameTrimmedContent
(
new
File
(
"src/test/resources/expected-classes/scala/optional/InterfaceWithOptionalField.scala.txt"
),
getFileByName
(
files
,
"InterfaceWithOptionalField.scala"
));
assertSameTrimmedContent
(
new
File
(
"src/test/resources/expected-classes/scala/optional/TypeWithMandatoryField.scala.txt"
),
getFileByName
(
files
,
"TypeWithMandatoryField.scala"
));
}
}
\ No newline at end of file
src/test/java/com/kobylynskyi/graphql/codegen/scala/GraphQLCodegenReactorToStringTest.java
浏览文件 @
600c902c
...
...
@@ -87,4 +87,32 @@ class GraphQLCodegenReactorToStringTest {
}
}
@Test
void
generate_SetGenerateClient_False
()
throws
Exception
{
mappingConfig
.
setPackageName
(
"com.kobylynskyi.graphql.codegen.prot"
);
mappingConfig
.
setGenerateEqualsAndHashCode
(
true
);
mappingConfig
.
setGenerateClient
(
false
);
mappingConfig
.
setGenerateToString
(
true
);
mappingConfig
.
setUseObjectMapperForRequestSerialization
(
singleton
(
"TestEnum"
));
mappingConfig
.
putCustomTypeMappingIfAbsent
(
"DateTime"
,
"java.time.ZonedDateTime"
);
mappingConfig
.
setGenerateApis
(
true
);
mappingConfig
.
setGenerateModelsForRootTypes
(
true
);
mappingConfig
.
setApiNameSuffix
(
"API"
);
new
ScalaGraphQLCodegen
(
singletonList
(
"src/test/resources/schemas/scala/restricted-words.graphqls"
),
outputBuildDir
,
mappingConfig
,
TestUtils
.
getStaticGeneratedInfo
()).
generate
();
File
[]
files
=
Objects
.
requireNonNull
(
outputJavaClassesDir
.
listFiles
());
List
<
String
>
generatedFileNames
=
Arrays
.
stream
(
files
).
map
(
File:
:
getName
).
filter
(
f
->
Objects
.
equals
(
"Synchronized.scala"
,
f
)).
sorted
().
collect
(
toList
());
assertEquals
(
singletonList
(
"Synchronized.scala"
),
generatedFileNames
);
for
(
File
file
:
files
)
{
if
(
Objects
.
equals
(
"Synchronized.scala"
,
file
.
getName
()))
{
assertSameTrimmedContent
(
new
File
(
String
.
format
(
"src/test/resources/expected-classes/scala/tostring/%s.txt"
,
"TOSTRING_Synchronized.scala"
)),
file
);
}
}
}
}
src/test/resources/expected-classes/kt/optional/InterfaceWithOptionalField.kt.txt
0 → 100644
浏览文件 @
600c902c
@javax.annotation.Generated(
value = ["com.kobylynskyi.graphql.codegen.GraphQLCodegen"],
date = "2020-12-31T23:59:59-0500"
)
interface InterfaceWithOptionalField {
val test: Int?
}
\ No newline at end of file
src/test/resources/expected-classes/kt/optional/TypeWithMandatoryField.kt.txt
0 → 100644
浏览文件 @
600c902c
import java.util.StringJoiner
@javax.annotation.Generated(
value = ["com.kobylynskyi.graphql.codegen.GraphQLCodegen"],
date = "2020-12-31T23:59:59-0500"
)
data class TypeWithMandatoryField(
override
val test: Int
) : InterfaceWithOptionalField {
companion object {
fun builder(): Builder = Builder()
}
// In the future, it maybe change.
override fun toString(): String {
val joiner = StringJoiner(", ", "{ ", " }")
joiner.add("test: " + test);
return joiner.toString()
}
class Builder {
private var test: Int = 0
fun setTest(test: Int): Builder {
this.test = test
return this
}
fun build(): TypeWithMandatoryField {
return TypeWithMandatoryField(test)
}
}
}
\ No newline at end of file
src/test/resources/expected-classes/scala/optional/InterfaceWithOptionalField.scala.txt
0 → 100644
浏览文件 @
600c902c
@javax.annotation.Generated(
value = Array("com.kobylynskyi.graphql.codegen.GraphQLCodegen"),
date = "2020-12-31T23:59:59-0500"
)
trait InterfaceWithOptionalField {
val test: Int
}
\ No newline at end of file
src/test/resources/expected-classes/scala/optional/TypeWithMandatoryField.scala.txt
0 → 100644
浏览文件 @
600c902c
import scala.collection.JavaConverters._
@javax.annotation.Generated(
value = Array("com.kobylynskyi.graphql.codegen.GraphQLCodegen"),
date = "2020-12-31T23:59:59-0500"
)
case class TypeWithMandatoryField(
override val test: Int
) extends InterfaceWithOptionalField {
override def toString(): String = {
Seq(
"test: " + test
).filter(_ != "").mkString("{", ",", "}")
}
}
object TypeWithMandatoryField {
def builder(): TypeWithMandatoryField.Builder = new Builder()
class Builder {
private var test: Int = _
def setTest(test: Int): Builder = {
this.test = test
this
}
def build(): TypeWithMandatoryField = TypeWithMandatoryField(test)
}
}
\ No newline at end of file
src/test/resources/expected-classes/scala/tostring/Synchronized.scala.txt
浏览文件 @
600c902c
...
...
@@ -10,7 +10,7 @@ import TestEnum._
)
case class Synchronized(
void: String,
w
ait: Char,
W
ait: Char,
`this`: Char,
`super`: Char,
`private`: Char,
...
...
@@ -24,7 +24,7 @@ case class Synchronized(
override def toString(): String = {
Seq(
if (void != null) "void: " + GraphQLRequestSerializer.getEntry(void) else "",
"wait: " + GraphQLRequestSerializer.getEntry(
w
ait),
"wait: " + GraphQLRequestSerializer.getEntry(
W
ait),
"this: " + GraphQLRequestSerializer.getEntry(`this`),
"super: " + GraphQLRequestSerializer.getEntry(`super`),
"private: " + GraphQLRequestSerializer.getEntry(`private`),
...
...
@@ -44,7 +44,7 @@ object Synchronized {
class Builder {
private var void: String = _
private var
w
ait: Char = _
private var
W
ait: Char = _
private var `this`: Char = _
private var `super`: Char = _
private var `private`: Char = _
...
...
@@ -59,8 +59,8 @@ object Synchronized {
this
}
def setWait(
w
ait: Char): Builder = {
this.
wait = w
ait
def setWait(
W
ait: Char): Builder = {
this.
Wait = W
ait
this
}
...
...
@@ -104,7 +104,7 @@ object Synchronized {
this
}
def build(): Synchronized = Synchronized(void,
w
ait, `this`, `super`, `private`, native, that, enum, Synchronized, date)
def build(): Synchronized = Synchronized(void,
W
ait, `this`, `super`, `private`, native, that, enum, Synchronized, date)
}
}
\ No newline at end of file
src/test/resources/expected-classes/scala/tostring/TOSTRING_Synchronized.scala.txt
0 → 100644
浏览文件 @
600c902c
package com.kobylynskyi.graphql.codegen.prot
import scala.collection.JavaConverters._
import TestEnum._
@javax.annotation.Generated(
value = Array("com.kobylynskyi.graphql.codegen.GraphQLCodegen"),
date = "2020-12-31T23:59:59-0500"
)
case class Synchronized(
void: String,
Wait: Char,
`this`: Char,
`super`: Char,
`private`: Char,
native: Char,
that: Char,
enum: TestEnum,
Synchronized: Synchronized,
date: java.time.ZonedDateTime
) {
override def toString(): String = {
Seq(
if (void != null) "void: \"void\"" else "",
"wait: " + Wait,
"this: " + `this`,
"super: " + `super`,
"private: " + `private`,
"native: " + native,
"that: " + that,
if (enum != null) "enum: enum" else "",
if (Synchronized != null) "Synchronized: Synchronized" else "",
if (date != null) "date: date" else ""
).filter(_ != "").mkString("{", ",", "}")
}
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录