JsonToKotlinClass icon indicating copy to clipboard operation
JsonToKotlinClass copied to clipboard

Splitting classes creation into two stages and using a real definitions to code generation

Open rodion-m opened this issue 6 years ago • 6 comments

Hello everyone! I want to suggest a new approach to code generation by splitting it in two stages:

  1. Restoring structures (classes and enums) from given data type:
  • Regular json data (we have the parser already)
  • Json Schema (we have it too)
  • Or any another input even the real code on a different language for example (it's just possible to do in future) and creating a simple structures (definitions) of the classes and enums of this data (I'll describe it below).
  1. Universal function to generate code (using kotlinpoet) from given definitions. We have the basic implementation of the code generator in my PR #148 (thanks @kezhenxu94 ). So, now about structures. As we have seen, JsonSchema is too complex and actually it's not for code generation, so I suggest to create a special classes, that will describe another classes and properties. We can call it like RealDefinitions or RealSchema. Here is a simple example how it can be implemented:
/** Base class for [PropertyDef] and [ClassDef] */
abstract class ObjectDefBase(
    val name: String,
    val doc: String? = null
)
class ClassDef(
    name: String,
    val properties: List<PropertyDef>? = null,
    val type: ClassType
) : ObjectDefBase(name)

enum class ClassType {
    Class, Interface, Annotation
}
/** Definition of a property or field */
class PropertyDef(
    /** name of the property or field */
    name: String,

    /** type of the property (example: "string") */
    val type: String,

    /** format of the property (example: "date-time") */
    val format: String,

    /** indicates if the property can be null */
    val nullable: Boolean,

    /** default value of the property */
    val value: String? = null
): ObjectDefBase(name)

And the same for enums. Also the great benefit of this approach that this definitions can be used for code generation in any other languages, since the schema is static (unlike JsonSchema) it can be easily serialized and deserialized. If you like the idea, I'll finish the definitions and try to split my solution from PR #148 in two stages and then together we can modify the resulting definitions and you will be able to improve the code generation function taking to user's configs.

rodion-m avatar Apr 21 '19 10:04 rodion-m

@Rody66 Good idea, I like it! now we have two data structure for describing kotlin data class definition:

try to refer these two data structures and keep the function compatible。 Also, please take a look at the interceptor mechanisms , now the config options almost do its work by interceptors. I look forward to seeing your complete definition of the relevant data structure and its features.

wuseal avatar Apr 21 '19 11:04 wuseal

@Rody66 Any progress?

wuseal avatar May 13 '19 12:05 wuseal

@wuseal I'm sorry, at the moment I don't have time to explore your existed api and continue working on module improvements JsonSchema -> Kotlin class. In my roadmap come back to this after some time, when our project will be needed of this module. Currently other priorities, sorry. Anyway feel free to commit my PR #148 . It's still not bad.

rodion-m avatar May 14 '19 11:05 rodion-m

Hi, @rodion-m #148 has been merged, now can you help to finish this design base on current code struct? Now we have similar data struct like DataClasst, KotilnClass, ListClass, etc.. here, but the name is only for kotlin, can you help upgrade current data struct?

wuseal avatar Mar 20 '20 04:03 wuseal

Hi, @rodion-m #148 has been merged, now can you help to finish this design base on current code struct? Now we have similar data struct like DataClasst, KotilnClass, ListClass, etc.. here, but the name is only for kotlin, can you help upgrade current data struct?

Hi! Thank you for contacting me. Unfortunately now again I'm very busy on another project, so in the near time I just cannot help you, sorry((.

rodion-m avatar Apr 01 '20 22:04 rodion-m

Clear! It's Okay, Hope we could hear your voice in the future 😃 @rodion-m

wuseal avatar Apr 02 '20 07:04 wuseal