Skip to content

Commit

Permalink
modify review
Browse files Browse the repository at this point in the history
Signed-off-by: yuanzicong <[email protected]>
  • Loading branch information
yuanzicong committed May 20, 2021
1 parent 260e84a commit c71b6c3
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 8 deletions.
44 changes: 44 additions & 0 deletions build_from_jsonobject_tip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
Enable this extension,you can make a kotlin data class with a static function to build from JSONObject.

e.g.
<br>
json:
```json
{
"a":1,
"b":"abc",
"c":true
}
```

It can convert to the following kotlin data class

```kotlin
data class Test(
val a: Int?,
val b: String?,
val c: Boolean?
) {

companion object {
@JvmStatic
fun buildFromJson(jsonObject: JSONObject?): Test? {

jsonObject?.run {
return Test(
optInt("a"),
optString("b"),
optBoolean("c")
)
}
return null
}
}
}
```

Then you can use this function to make a instance from a JSONObjcet, for example
```kotlin
val jsonObject: JSONObject = TestApi.rquestTest()
val test: Test = Test.buildFromJson(jsonObject)
```
37 changes: 37 additions & 0 deletions classes_non_nullable.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Enable this extension and type the classes' names into the input area, you can make the classes you typed non-nullable.

In the text area, the classes should split with ','.

Such as
```
Int,Double,Boolean,Long,List
```
In particular, the ``List<Any>`` only need to type ``List``

e.g.
<br>
json:
```json
{
"a": 1,
"b": true,
"c": "hhhh",
"d": [
"a","b","c","d"
],
"e": [
1,2,3,4
]
}
```

It can convert to the following kotlin data class
```kotlin
data class Test(
val a: Int,
val b: Boolean,
val c: String?,
val d: List<String?>,
val e: List<Int>
)
```
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import wu.seal.jsontokotlin.model.builder.KotlinCodeBuilder
import wu.seal.jsontokotlin.model.classscodestruct.KotlinClass
import wu.seal.jsontokotlin.ui.jCheckBox
import wu.seal.jsontokotlin.ui.jHorizontalLinearLayout
import wu.seal.jsontokotlin.ui.jLink
import javax.swing.JPanel

object BuildFromJsonObjectSupport : Extension() {
Expand All @@ -15,10 +16,11 @@ object BuildFromJsonObjectSupport : Extension() {
override fun createUI(): JPanel {
return jHorizontalLinearLayout {
jCheckBox(
"Enable build from JsonObject",
"Make a static function that can build from JSONObject",
getConfig(configKey).toBoolean(),
{ isSelected -> setConfig(configKey, isSelected.toString()) }
)
add(jLink("Know about this extension", "https://github.com/wuseal/JsonToKotlinClass/blob/master/classes_non_nullable.md"))
fillSpace()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ object CamelCaseSupport : Extension() {
override fun createUI(): JPanel {
return jHorizontalLinearLayout {
jCheckBox(
"Enable Camel-Case",
"Let properties' name to be camel case",
getConfig(configKey).toBoolean(),
{ isSelected -> setConfig(configKey, isSelected.toString()) })
fillSpace()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,29 @@ object NeedNonNullableClassesSupport : Extension() {
maximumSize = JBDimension(400, 30)
}

return jHorizontalLinearLayout {
jCheckBox("Need NonNullable classes: ", getConfig(prefixKeyEnable).toBoolean(), { isSelected ->
setConfig(prefixKeyEnable, isSelected.toString())
prefixJField.isEnabled = isSelected
})
add(prefixJField)
return jVerticalLinearLayout {
jHorizontalLinearLayout {
jCheckBox("Classes non-nullable: ", getConfig(prefixKeyEnable).toBoolean(), { isSelected ->
setConfig(prefixKeyEnable, isSelected.toString())
prefixJField.isEnabled = isSelected
})
add(prefixJField)
}
jHorizontalLinearLayout {
fixedSpace(15)
jLink("Know about this extension", "https://github.com/wuseal/JsonToKotlinClass/blob/master/classes_non_nullable.md")
}
}

}

override fun intercept(kotlinClass: KotlinClass): KotlinClass {
return if (kotlinClass is DataClass) {
if (getConfig(prefixKeyEnable).toBoolean()) {
val list = getConfig(prefixKey).split(',')
if (list.isEmpty()) {
return kotlinClass
}
val originProperties = kotlinClass.properties
val newProperties = originProperties.map {
val oldType = it.type
Expand Down

0 comments on commit c71b6c3

Please sign in to comment.