Skip to content

Commit d26c81c

Browse files
committed
add doc for extensions module
1 parent 1706328 commit d26c81c

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ Have a try with the settings dialog :stuck_out_tongue_winking_eye:
5959
* [Android Parcelable support](https://github.com/wuseal/JsonToKotlinClass/issues/44)
6060
* [Customize parent class declaration](https://github.com/wuseal/JsonToKotlinClass/issues/44)
6161
* Generating kotlin data class property order by alphabetical
62+
* Support customizing this plugin by `Extensions` module
6263

6364
### Generate Example
6465
This is the example JSON from json.org

doc_for_extensions.md

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
If you want to customize you own `JsonToKotlinClass` Plugin or want to contribute some new featrues for this project, This guide will help you.
2+
3+
#### Customize Your Own Plugin
4+
5+
Customize your own plugin by these steps, and you will have your own plugin, in these guide we will create a extension that will make all the property name to be all upper case
6+
7+
1. Fork `JsonToKotlinClass` from Github
8+
9+
2. Clone your forked project to local
10+
11+
3. Open Project with Intellij Idea
12+
13+
4. Create package under ..src/main/kotlin/extensions
14+
15+
![image](https://user-images.githubusercontent.com/9211902/51250876-ccb8cb00-19d2-11e9-9620-9c45c8975bfa.png)
16+
17+
Package name could be your own domain name. in this case we will you `wu.seal`
18+
19+
5. Create a Extension object class file under your created package
20+
21+
![image](https://user-images.githubusercontent.com/9211902/51249232-a47a9d80-19cd-11e9-866f-b7e283da4853.png)
22+
23+
6. Now the created file would like this:
24+
25+
![image](https://user-images.githubusercontent.com/9211902/51249378-0fc46f80-19ce-11e9-8abd-0c926e8a726f.png)
26+
27+
7. Add interfaces to be implemente
28+
29+
![image](https://user-images.githubusercontent.com/9211902/51249511-75b0f700-19ce-11e9-9e74-ab014d3d77e3.png)
30+
31+
8. Implement all the needed interface
32+
33+
```kotlin
34+
package extensions.wu.seal
35+
36+
import extensions.Extension
37+
import wu.seal.jsontokotlin.classscodestruct.KotlinDataClass
38+
import javax.swing.JPanel
39+
40+
object AllUpperCase :Extension(){
41+
42+
override fun createUI(): JPanel {
43+
44+
}
45+
46+
override fun intercept(kotlinDataClass: KotlinDataClass): KotlinDataClass {
47+
48+
}
49+
}
50+
```
51+
52+
9. Then we create a new UI for switching to enable all property name to be all upper case
53+
54+
```kotlin
55+
override fun createUI(): JPanel {
56+
57+
val configKey = "wu.seal.all_to_be_upper_case"
58+
59+
val checkBox = JCheckBox("Make all properties name to be all upper case").apply {
60+
isSelected = getConfig(configKey).toBoolean()
61+
addActionListener {
62+
setConfig(configKey, isSelected.toString())
63+
}
64+
}
65+
66+
return panel {
67+
row {
68+
checkBox()
69+
}
70+
}
71+
72+
}
73+
```
74+
75+
10. And make the properties names to be all upper case
76+
77+
```kotlin
78+
override fun intercept(kotlinDataClass: KotlinDataClass): KotlinDataClass {
79+
80+
//make all properties name to be all upper case
81+
val newProperties = kotlinDataClass.properties.map { it.copy(name = it.name.toUpperCase()) }
82+
83+
return kotlinDataClass.copy(properties = newProperties)
84+
}
85+
```
86+
87+
We use `map`method to create a new properties which all properties names are translate to be all upper case, And copy the incoming parameter `kotlinDataClass` with replacing the new created properties, and return it to the system.
88+
89+
11. Add the new object class into `ExtensionsCollector` object class which inside extensions package
90+
91+
```kotlin
92+
/**
93+
* extension collect, all extensions will be hold by this class's extensions property
94+
*/
95+
object ExtensionsCollector {
96+
/**
97+
* all extensions
98+
*/
99+
val extensions = listOf(
100+
PropertyPrefixSupport,
101+
PropertySuffixSupport,
102+
AllUpperCase
103+
)
104+
}
105+
```
106+
107+
12. Run the plugin by clicking`runIde`task
108+
109+
![image](https://user-images.githubusercontent.com/9211902/51250409-40f26f00-19d1-11e9-962c-7f44fb5549e4.png)
110+
111+
Then we will see the new IDE opened
112+
113+
13. Create a new project in new opened IDE and create a new Kotlin File for inserting new Kotlin Data Class code, and open `JsonToKotlinClass` Plugin, Then we will see that new Extension was added in the extensions tab,Let's selected the new added checkbox and have a test if it works ok.
114+
115+
![image](https://user-images.githubusercontent.com/9211902/51250670-15bc4f80-19d2-11e9-9cee-798489108adf.png)
116+
117+
14. Then Let's see the tested result:
118+
119+
![image](https://user-images.githubusercontent.com/9211902/51250749-5a47eb00-19d2-11e9-9824-c8f9ec0cc664.png)
120+
121+
```kotlin
122+
import com.google.gson.annotations.SerializedName
123+
data class TestCode(
124+
@SerializedName("hello")
125+
val HELLO: String = ""
126+
)
127+
```
128+
129+
Yeah! That what we want. Greate we have created the first our style plugin.
130+
131+
Any Question? Welcome to raise an issue [here](https://github.com/wuseal/JsonToKotlinClass/issues)
132+
133+
Demo could be found [here](https://github.com/wuseal/JsonToKotlinClass/tree/3.0-extension-demo)
134+
135+

0 commit comments

Comments
 (0)