Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support adding Expose Annotation for Gson by new Extension #366

Merged
merged 4 commits into from
Jul 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 1 addition & 12 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,7 @@ changelog {
excludeLabels = listOf("duplicate", "invalid", "question", "wontfix")
sinceTag = "V3.0.0"
skipTags = listOf(
"v3.2.0-EAP",
"3.3.0-EAP",
"3.3.0-EAP-2",
"3.4.0-EAP",
"3.4.0-EAP-2",
"3.5.0-EAP",
"3.5.1-EAP",
"3.6.0-EAP",
"3.6.0-EAP-2",
"3.7.0-EAP",
"3.7.0-EAP-2",
"3.7.0-EAP-3"

)
useMilestoneAsTag = true
timezone = DEFAULT_TIMEZONE
Expand Down
13 changes: 12 additions & 1 deletion src/main/kotlin/extensions/Extension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,30 @@ abstract class Extension : IImportClassDeclarationInterceptor, IKotlinClassInter
return originClassImportDeclaration
}

@Deprecated("Please use val testHelper")
@JvmName("getTestHelperForJVM")
fun getTestHelper() = TestHelper(this)

val testHelper = TestHelper(this)
/**
* Test helper for test config settings
*/
class TestHelper(private val extension: Extension) {

fun setConfig(key: String, value: String) {
extension.setConfig(key,value)
extension.setConfig(key, value)
}

fun getConfig(key: String): String {
return extension.getConfig(key)
}
}

/**
* val configKey = "xxConfig"
*
* val configBooleanValue = configKey.booleanConfig()
*/
protected val String.booleanConfigValue: Boolean
get() = getConfig(this).toBoolean()
}
3 changes: 2 additions & 1 deletion src/main/kotlin/extensions/ExtensionsCollector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ object ExtensionsCollector {
CamelCaseSupport,
BuildFromJsonObjectSupport,
NeedNonNullableClassesSupport,
InternalModifierSupport
InternalModifierSupport,
AddGsonExposeAnnotation,
)
}
44 changes: 44 additions & 0 deletions src/main/kotlin/extensions/wu/seal/AddGsonExposeAnnotation.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package extensions.wu.seal

import com.google.gson.annotations.Expose
import extensions.Extension
import wu.seal.jsontokotlin.model.classscodestruct.Annotation
import wu.seal.jsontokotlin.model.classscodestruct.DataClass
import wu.seal.jsontokotlin.model.classscodestruct.KotlinClass
import wu.seal.jsontokotlin.ui.addSelectListener
import wu.seal.jsontokotlin.ui.jCheckBox
import wu.seal.jsontokotlin.ui.jHorizontalLinearLayout
import javax.swing.JPanel

object AddGsonExposeAnnotation : Extension() {

@Expose
val config_key = "wu.seal.add_gson_expose_annotation"

override fun createUI(): JPanel {
return jHorizontalLinearLayout {
jCheckBox("Add Gson Expose Annotation", config_key.booleanConfigValue) {
addSelectListener { setConfig(config_key, it.toString()) }
}
fillSpace()
}
}

override fun intercept(kotlinClass: KotlinClass): KotlinClass {
return if (config_key.booleanConfigValue) {
if (kotlinClass is DataClass) {
val newProperties = kotlinClass.properties.map {
val newAnnotations = it.annotations + Annotation.fromAnnotationString("@Expose")
it.copy(annotations = newAnnotations)
}
kotlinClass.copy(properties = newProperties)
} else kotlinClass
} else kotlinClass
}

override fun intercept(originClassImportDeclaration: String): String {
return if (config_key.booleanConfigValue) {
originClassImportDeclaration.append("import com.google.gson.annotations.Expose")
} else originClassImportDeclaration
}
}
8 changes: 6 additions & 2 deletions src/main/kotlin/wu/seal/jsontokotlin/ui/UIDSLV2.kt
Original file line number Diff line number Diff line change
Expand Up @@ -172,18 +172,22 @@ fun Any.jTextInput(
fun Any.jCheckBox(
text: String,
initValue: Boolean = false,
actionListener: (isSelected: Boolean) -> Unit,
selectListener: (isSelected: Boolean) -> Unit = {},
init: JBCheckBox.() -> Unit = {}
): JCheckBox {
val jCheckBox = JBCheckBox(text, initValue)
jCheckBox.addActionListener {
actionListener.invoke(jCheckBox.isSelected)
selectListener.invoke(jCheckBox.isSelected)
}
jCheckBox.init()
checkAddView(this, jCheckBox)
return jCheckBox
}

fun JCheckBox.addSelectListener(selectListener: (isSelected: Boolean) -> Unit) = addActionListener {
selectListener.invoke(isSelected)
}


/**
* generate a scrollable component
Expand Down
31 changes: 31 additions & 0 deletions src/test/kotlin/extensions/wu/seal/AddGsonExposeAnnotationTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package extensions.wu.seal

import com.winterbe.expekt.should
import org.junit.Test

import org.junit.Assert.*
import wu.seal.jsontokotlin.generateKotlinClass
import wu.seal.jsontokotlin.utils.BaseTest

class AddGsonExposeAnnotationTest : BaseTest() {
private val json = """{"a":1}"""

val expected = """
data class Test(
@Expose
val a: Int // 1
)
""".trimIndent()
@Test
fun intercept() {
AddGsonExposeAnnotation.testHelper.setConfig(AddGsonExposeAnnotation.config_key, true.toString())
val result = json.generateKotlinClass().applyInterceptor(AddGsonExposeAnnotation).getCode()
result.should.be.equal(expected)
}

@Test
fun testIntercept() {
AddGsonExposeAnnotation.testHelper.setConfig(AddGsonExposeAnnotation.config_key, true.toString())
AddGsonExposeAnnotation.intercept("").should.equal("\nimport com.google.gson.annotations.Expose")
}
}