Skip to content
This repository was archived by the owner on Feb 1, 2024. It is now read-only.

Commit b7bb501

Browse files
Merge pull request #1 from OpenEdgn/dev
Dev
2 parents 16e472f + 7d1b731 commit b7bb501

File tree

4 files changed

+41
-12
lines changed

4 files changed

+41
-12
lines changed

build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ buildscript {
77

88
repositories {
99
mavenLocal()
10+
maven { url = uri("https://maven.aliyun.com/repository/public/") }
1011
mavenCentral()
11-
// maven { url = uri("https://maven.aliyun.com/repository/public/") }
1212
jcenter()
1313
maven { url = uri("https://jitpack.io") }
1414

@@ -21,8 +21,8 @@ buildscript {
2121
allprojects {
2222
repositories {
2323
mavenLocal()
24+
maven { url = uri("https://maven.aliyun.com/repository/public/") }
2425
mavenCentral()
25-
// maven { url = uri("https://maven.aliyun.com/repository/public/") }
2626
jcenter()
2727
maven { url = uri("https://jitpack.io") }
2828
}

core/src/main/kotlin/com/github/open_edgn/data/format/Annotations.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ package com.github.open_edgn.data.format
77
* 此字段可以忽略,同样会扫描,但会按照默认样式来绑定
88
*
99
* @property defaultValue String 默认值
10+
* @param nullable Boolean 指定此字段是否可以为 null
1011
* @property alias Array<out String>
1112
* @constructor
1213
*/
1314
@Retention(AnnotationRetention.RUNTIME)
1415
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY, AnnotationTarget.FIELD)
1516
annotation class ArgsItem(
1617
val defaultValue: String = "",
18+
val nullable: Boolean = true,
1719
vararg val alias: String
1820
)
1921

core/src/main/kotlin/com/github/open_edgn/data/format/ArgsReader.kt

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,27 +43,30 @@ class ArgsReader(args: Array<String>, vararg argsBeans: KClass<*>) {
4343
continue
4444
}
4545
val argsItem = property.findAnnotation<ArgsItem>()
46+
val canNullable = property.returnType.isMarkedNullable
4647
try {
4748
if (argsItem != null) {
4849
val alias = argsItem.alias.toMutableList()
4950
if (alias.isEmpty()) {
5051
alias.add(property.name)
5152
}
5253
if (argsItem.defaultValue.isNotEmpty()) {
53-
54-
logger.debug("字段 {} 指定默认值为 {},在不存在可注入的数据时将使用此默认值.", property.name, argsItem.defaultValue)
54+
logger.debug("字段 {} 指定未找到时的默认值为 {}.", property.name, argsItem.defaultValue)
5555
} else {
56-
logger.debug("字段 {} 不存在默认值.", property.name)
56+
logger.debug("字段 {} 不存在默认值." +
57+
if (canNullable && argsItem.nullable)
58+
"且字段允许为null,如未找到匹配的数据则默认为 null"
59+
else "但字段不允许为null,如果未找到匹配的数据,程序将抛出异常!", property.name)
5760
}
58-
injection(result, index, property.returnType.jvmErasure, args, alias, argsItem.defaultValue)
61+
injection(result, index, property.returnType.jvmErasure, args, alias, argsItem.defaultValue, canNullable && argsItem.nullable)
5962
} else {
6063
logger.debug("字段 {} 不存在默认值.", property.name)
61-
injection(result, index, property.returnType.jvmErasure, args, listOf(property.name), null)
64+
injection(result, index, property.returnType.jvmErasure, args, listOf(property.name), null, canNullable)
6265
}
6366
} catch (e: Exception) {
6467
throw FormatErrorException("解析 Arg 时出现错误![${e.message}]", e)
6568
}
66-
if (property.returnType.isMarkedNullable.not() && result[index] == null) {
69+
if (canNullable.not() && result[index] == null) {
6770
throw NullPointerException("无法初始化类型,因为字段${property.name} 为空,但此字段不允许空的数据!")
6871
}
6972
}
@@ -104,7 +107,8 @@ class ArgsReader(args: Array<String>, vararg argsBeans: KClass<*>) {
104107
type: KClass<*>,
105108
args: Array<String>,
106109
alias: List<String>,
107-
defaultValue: String?) {
110+
defaultValue: String?,
111+
canNullable: Boolean) {
108112
for (item in alias) {
109113
val data = when {
110114
item.length == 1 -> {
@@ -123,10 +127,12 @@ class ArgsReader(args: Array<String>, vararg argsBeans: KClass<*>) {
123127
}
124128
}
125129

126-
if (defaultValue == null || defaultValue.isEmpty()) {
127-
throw FormatErrorException("未发现 Args 下存在可注入的数据且默认值.")
128-
} else {
130+
if (defaultValue != null && defaultValue.isNotEmpty()) {
129131
result[index] = StringFormatUtils.parse(defaultValue, type, true)
132+
} else {
133+
if (!canNullable) {
134+
throw FormatErrorException("未发现 Args 下存在可注入的数据且默认值.")
135+
}
130136
}
131137
}
132138

core/src/test/kotlin/com/github/open_edgn/data/format/ArgsReaderTest.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,25 @@ internal class ArgsReaderTest {
6969
@ArgsItem(defaultValue = "false", alias = ["debug", "d"])
7070
val debug: Boolean
7171
)
72+
73+
@Test
74+
fun test4() {
75+
val argsBean = ArgsReader(
76+
arrayOf("-d"),
77+
Test4Property::class).getArgsBean(Test4Property::class)
78+
assertEquals(argsBean.workDirectory, null)
79+
}
80+
81+
data class Test4Property(
82+
/**
83+
* 工作目录
84+
*/
85+
@ArgsItem(defaultValue = "", nullable = true, alias = ["work-dir"])
86+
var workDirectory: File?,
87+
/**
88+
* 开启debug 模式
89+
*/
90+
@ArgsItem(defaultValue = "false", alias = ["debug", "d"])
91+
val debug: Boolean
92+
)
7293
}

0 commit comments

Comments
 (0)