All Downloads are FREE. Search and download functionalities are using the official Maven repository.

codegen.MutableClasses.kt Maven / Gradle / Ivy

There is a newer version: 0.1.7
Show newest version
/*
 * Copyright (C) 2024 Meowool 
 *
 * This file is part of the MMKV-KTX project .
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.meowool.mmkv.ktx.compiler.codegen

import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.google.devtools.ksp.symbol.KSValueParameter
import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.FunSpec
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.PropertySpec
import com.squareup.kotlinpoet.TypeSpec
import com.squareup.kotlinpoet.ksp.toClassName
import com.squareup.kotlinpoet.ksp.toTypeName

class MutableClasses : CodegenStep() {
  override fun generate() = context.preferences.process(::generateMutableClass)

  private fun generateMutableClass(preferences: KSClassDeclaration) {
    val className = context.mutableClassName(preferences)
    val constructor = requireNotNull(preferences.primaryConstructor) {
      "@Preferences class must have a primary constructor."
    }
    val properties = constructor.parameters

    val conversionSpec = FunSpec.builder("toImmutable")
      .addModifiers(KModifier.ABSTRACT)
      .returns(preferences.toClassName())
      .build()

    val classSpec = TypeSpec.interfaceBuilder(className)
      .addMappedProperties(properties)
      .addFunction(conversionSpec)
      .build()

    // TODO: Add KDoc for generated symbols.
    FileSpec.builder(className)
      .addType(classSpec)
      .build()
      .write(preferences)
  }

  private fun TypeSpec.Builder.addMappedProperties(raw: List) = apply {
    raw.mapProperties().forEach { addProperty(it) }
  }

  private fun List.mapProperties() = map {
    PropertySpec.builder(it.name!!.asString(), it.type.toTypeName())
      .mutable()
      .build()
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy