Skip to content

Commit cc4112a

Browse files
author
seal
committed
add property nullable option in property tab
1 parent 52dda72 commit cc4112a

File tree

4 files changed

+102
-40
lines changed

4 files changed

+102
-40
lines changed

src/wu/seal/jsontokotlin/ConfigManager.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ interface IConfigManager {
1818
private val IS_COMMENT_OFF: String
1919
get() = "need_comment_key"
2020

21+
private val IS_PROPERTY_NULLABLE_KEY: String
22+
get() = "jsonToKotlin_is_property_nullable_key"
2123

2224
var isPropertiesVar: Boolean
2325
get() = if (isTestModel) TestConfig.isPropertiesVar else PropertiesComponent.getInstance().isTrueValue(IS_PROPERTIES_VAR_KEY)
@@ -35,6 +37,11 @@ interface IConfigManager {
3537
get() = if (isTestModel) TestConfig.targetJsonConvertLib else TargetJsonConverter.valueOf(PropertiesComponent.getInstance().getValue(TARGET_JSON_CONVERTER_LIB_KEY) ?: TargetJsonConverter.None.name)
3638
set(value) = if (isTestModel) {
3739
} else PropertiesComponent.getInstance().setValue(TARGET_JSON_CONVERTER_LIB_KEY, value.name)
40+
41+
var isPropertyNullable: Boolean
42+
get() = if (isTestModel) TestConfig.isPropertyNullable else PropertiesComponent.getInstance().isTrueValue(IS_PROPERTY_NULLABLE_KEY)
43+
set(value) = if (isTestModel) {
44+
} else PropertiesComponent.getInstance().setValue(IS_PROPERTY_NULLABLE_KEY, value)
3845
}
3946

4047

@@ -60,4 +67,5 @@ object TestConfig {
6067
var isCommentOff = false
6168
var isPropertiesVar = false
6269
var targetJsonConvertLib = TargetJsonConverter.Gson
70+
var isPropertyNullable = true
6371
}

src/wu/seal/jsontokotlin/KotlinMaker.kt

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,27 +58,25 @@ class KotlinMaker {
5858

5959
for ((property, jsonElementValue) in jsonObject.entrySet()) {
6060

61-
var type = "String"
6261
if (jsonElementValue.isJsonArray) {
63-
64-
type = getArrayType(property, jsonElementValue.asJsonArray)
62+
val type = getArrayType(property, jsonElementValue.asJsonArray)
6563

6664
if (isExpectedJsonObjArrayType(jsonElementValue.asJsonArray)) {
67-
toBeAppend.add(KotlinMaker(type, jsonElementValue.asJsonArray.first()).makeKotlinData())
65+
toBeAppend.add(KotlinMaker(getChildType(getRawType(type)), jsonElementValue.asJsonArray.first()).makeKotlinData())
6866
}
6967
addProperty(stringBuilder, property, type, "")
7068

7169
} else if (jsonElementValue.isJsonPrimitive) {
72-
type = getPrimitiveType(jsonElementValue.asJsonPrimitive)
70+
val type = getPrimitiveType(jsonElementValue.asJsonPrimitive)
7371
addProperty(stringBuilder, property, type, jsonElementValue.asString)
7472

7573
} else if (jsonElementValue.isJsonObject) {
76-
type = getJsonObjectType(property)
77-
toBeAppend.add(KotlinMaker(type, jsonElementValue).makeKotlinData())
74+
val type = getJsonObjectType(property)
75+
toBeAppend.add(KotlinMaker(getRawType(type), jsonElementValue).makeKotlinData())
7876
addProperty(stringBuilder, property, type, "")
7977

8078
} else if (jsonElementValue.isJsonNull) {
81-
addProperty(stringBuilder, property, type, null)
79+
addProperty(stringBuilder, property, DEFAULT_TYPE, null)
8280
}
8381
}
8482
}
@@ -89,7 +87,7 @@ class KotlinMaker {
8987
if (innerValue == null) {
9088
innerValue = "null"
9189
}
92-
stringBuilder.append(KProperty(property, type, innerValue).getPropertyStringBlock())
90+
stringBuilder.append(KProperty(property, getOutType(type), innerValue).getPropertyStringBlock())
9391
stringBuilder.append("\n")
9492
}
9593

@@ -129,6 +127,7 @@ fun main(args: Array<String>) {
129127
TestConfig.targetJsonConvertLib = TargetJsonConverter.None
130128
TestConfig.isCommentOff = true
131129
TestConfig.isPropertiesVar = true
130+
TestConfig.isPropertyNullable = false
132131

133132
println("===========================================Change to none json lib support========================================= ")
134133

src/wu/seal/jsontokotlin/Panel.kt

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package wu.seal.jsontokotlin
22

3-
import java.awt.BorderLayout
4-
import java.awt.FlowLayout
5-
import java.awt.LayoutManager
3+
import java.awt.*
64
import javax.swing.ButtonGroup
5+
import javax.swing.JCheckBox
76
import javax.swing.JPanel
87
import javax.swing.JRadioButton
8+
import javax.swing.border.EmptyBorder
99

1010
/**
1111
* todo //beautify interface
@@ -21,7 +21,8 @@ class PropertyPanel(layout: LayoutManager?, isDoubleBuffered: Boolean) : JPanel(
2121

2222
init {
2323

24-
setLayout(BorderLayout(5, 5))
24+
border = EmptyBorder(10, 10, 10, 10)
25+
setLayout(GridLayout(5, 1, 10, 10))
2526

2627
val radioButtonVal = JRadioButton("Val")
2728

@@ -44,11 +45,28 @@ class PropertyPanel(layout: LayoutManager?, isDoubleBuffered: Boolean) : JPanel(
4445
buttonGroupProperty.add(radioButtonVal)
4546
buttonGroupProperty.add(radioButtonVar)
4647

47-
add(radioButtonVal, BorderLayout.NORTH)
48-
add(radioButtonVar, BorderLayout.CENTER)
49-
}
48+
add(radioButtonVal)
49+
add(radioButtonVar)
50+
51+
val nullableConatainer = JPanel(BorderLayout(10, 10))
52+
val line = JPanel()
53+
line.preferredSize = Dimension(500, 1)
54+
line.background = Color.BLACK
55+
nullableConatainer.add(line, BorderLayout.NORTH)
5056

57+
val nullAbleCheck = JCheckBox("Property to be Nullable?")
58+
if (ConfigManager.isPropertyNullable) {
59+
nullAbleCheck.isSelected = true
60+
}
61+
62+
nullAbleCheck.addChangeListener {
63+
ConfigManager.isPropertyNullable = nullAbleCheck.isSelected
64+
}
65+
nullableConatainer.add(nullAbleCheck, BorderLayout.CENTER)
5166

67+
add(nullableConatainer)
68+
69+
}
5270
}
5371

5472

@@ -63,9 +81,11 @@ class CommentConfigPanel(layout: LayoutManager?, isDoubleBuffered: Boolean) : JP
6381

6482
init {
6583

66-
setLayout(BorderLayout(5, 5))
84+
border = EmptyBorder(10, 10, 10, 10)
6785

68-
val radioButtonOpen = JRadioButton("Open")
86+
setLayout(GridLayout(5, 1, 10, 10))
87+
88+
val radioButtonOpen = JRadioButton("On")
6989

7090
radioButtonOpen.addActionListener {
7191
ConfigManager.isCommentOff = false
@@ -86,8 +106,8 @@ class CommentConfigPanel(layout: LayoutManager?, isDoubleBuffered: Boolean) : JP
86106
buttonGroupProperty.add(radioButtonOpen)
87107
buttonGroupProperty.add(radioButtonOff)
88108

89-
add(radioButtonOpen, BorderLayout.NORTH)
90-
add(radioButtonOff, BorderLayout.CENTER)
109+
add(radioButtonOpen)
110+
add(radioButtonOff)
91111
}
92112

93113
}
@@ -103,7 +123,9 @@ class TargetJsonLibConfigPanel(layout: LayoutManager?, isDoubleBuffered: Boolean
103123

104124
init {
105125

106-
setLayout(BorderLayout(5, 5))
126+
border = EmptyBorder(10, 10, 10, 10)
127+
128+
setLayout(GridLayout(5, 1, 10, 10))
107129

108130
val radioButtonNone = JRadioButton("None")
109131

@@ -126,8 +148,8 @@ class TargetJsonLibConfigPanel(layout: LayoutManager?, isDoubleBuffered: Boolean
126148
buttonGroupProperty.add(radioButtonNone)
127149
buttonGroupProperty.add(radioButtonGson)
128150

129-
add(radioButtonNone, BorderLayout.NORTH)
130-
add(radioButtonGson, BorderLayout.CENTER)
151+
add(radioButtonNone)
152+
add(radioButtonGson)
131153
}
132154

133155
}

src/wu/seal/jsontokotlin/TypeHelper.kt

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ import com.google.gson.JsonPrimitive
99
*/
1010

1111

12+
/**
13+
* the default type
14+
*/
15+
const val DEFAULT_TYPE = "Any"
16+
17+
val DEFAULT_OUTPUT_TYPE = getOutType(DEFAULT_TYPE)
18+
19+
1220
fun getPrimitiveType(jsonPrimitive: JsonPrimitive): String {
1321
var subType = "String"
1422
if (jsonPrimitive.isBoolean) {
@@ -29,49 +37,74 @@ fun getPrimitiveType(jsonPrimitive: JsonPrimitive): String {
2937

3038

3139
fun getJsonObjectType(type: String): String {
40+
3241
return KClassName.getName(type)
3342
}
3443

3544

45+
/**
46+
* get the inmost child type of array type
47+
*/
48+
fun getChildType(arrayType: String): String {
49+
50+
return arrayType.replace(Regex("[<,>,List]"), "")
51+
}
52+
53+
/**
54+
* get the type output to the edit file
55+
*/
56+
fun getOutType(rawType: String): String {
57+
if (ConfigManager.isPropertyNullable) {
58+
val innerRawType = rawType.replace("?", "").replace(">", "?>")
59+
val outputType = innerRawType.plus("?")
60+
return outputType
61+
}
62+
return rawType
63+
}
64+
65+
/**
66+
* get the type string without '?' character
67+
*/
68+
fun getRawType(outputType: String): String {
69+
70+
return outputType.replace("?", "")
71+
}
72+
3673
fun getArrayType(propertyName: String, jsonElementValue: JsonArray): String {
37-
var innerPropertyName = propertyName
38-
var type = "List<String>"
74+
val innerPropertyName = adjustPropertyNameForGettingArrayChildType(propertyName)
75+
var subType = DEFAULT_TYPE
3976
val jsonArray = jsonElementValue
4077

4178
val iterator = jsonArray.iterator()
4279
if (iterator.hasNext()) {
4380
val next = iterator.next()
44-
val subType =
81+
subType =
4582
if (next.isJsonPrimitive) {
4683
getPrimitiveType(next.asJsonPrimitive)
4784

4885
} else if (next.isJsonObject) {
4986
getJsonObjectType(innerPropertyName)
5087

5188
} else if (next.isJsonArray) {
52-
innerPropertyName = modifyPropertyForArrayObjType(innerPropertyName)
53-
getArrayType(innerPropertyName, next as JsonArray)
54-
55-
} else if (next.isJsonNull) {
56-
"Any"
57-
89+
getArrayType(innerPropertyName, next.asJsonArray)
5890
} else {
59-
"String"
91+
DEFAULT_TYPE
6092
}
61-
62-
type = "List<$subType>"
63-
} else {
64-
type = "List<Any>"
65-
6693
}
67-
return type
94+
return "List<$subType>"
6895
}
6996

7097
fun isExpectedJsonObjArrayType(jsonElementArray: JsonArray): Boolean {
7198
return jsonElementArray.firstOrNull()?.isJsonObject ?: false
7299
}
73100

74-
private fun modifyPropertyForArrayObjType(property: String): String {
101+
/**
102+
* when get the child type in an array
103+
* ,we need to modify the property name to make it's type name looks like a child type.
104+
* filter the sequence as 'list' ,"List'
105+
* and remove the last character 's' to make it like a child rather than a list
106+
*/
107+
fun adjustPropertyNameForGettingArrayChildType(property: String): String {
75108
var innerProperty = property
76109
if (innerProperty.endsWith("ies")) {
77110
innerProperty = innerProperty.substring(0, innerProperty.length - 3) + "y"

0 commit comments

Comments
 (0)