NTP-42975 - upi spend analyser (#15356)
Co-authored-by: Aparna Vadlamani <aparna.vadlamani@navi.com> Co-authored-by: Abhinav Gupta <abhinav.g@navi.com> Co-authored-by: Sanjay P <sanjay.p@navi.com>
This commit is contained in:
@@ -19,12 +19,15 @@ import com.google.devtools.ksp.symbol.KSFunctionDeclaration
|
||||
import com.google.devtools.ksp.validate
|
||||
import com.navi.code.annotations.AutoGenerate
|
||||
import com.navi.code.annotations.EventName
|
||||
import com.navi.code.utils.generateOverrideMethodWithDefaultSuperCall
|
||||
import com.navi.code.utils.isDefaultKotlinFunction
|
||||
import com.navi.code.utils.toSnakeCase
|
||||
import com.squareup.kotlinpoet.FileSpec
|
||||
import com.squareup.kotlinpoet.FunSpec
|
||||
import com.squareup.kotlinpoet.KModifier
|
||||
import com.squareup.kotlinpoet.ParameterSpec
|
||||
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
|
||||
import com.squareup.kotlinpoet.PropertySpec
|
||||
import com.squareup.kotlinpoet.TypeSpec
|
||||
import com.squareup.kotlinpoet.ksp.toClassName
|
||||
import java.io.OutputStreamWriter
|
||||
@@ -74,9 +77,31 @@ class EventTrackerProcessor(
|
||||
FileSpec.builder(packageName, generatedClassName)
|
||||
.addImport("com.navi.analytics.utils", "NaviTrackEvent")
|
||||
|
||||
val classBuilder =
|
||||
TypeSpec.objectBuilder(generatedClassName).addModifiers(KModifier.INTERNAL)
|
||||
val constructor =
|
||||
FunSpec.constructorBuilder()
|
||||
.addParameter(
|
||||
ParameterSpec.builder(
|
||||
"parameters",
|
||||
Map::class.parameterizedBy(String::class, String::class),
|
||||
)
|
||||
.defaultValue("emptyMap()")
|
||||
.build()
|
||||
)
|
||||
.build()
|
||||
|
||||
val classBuilder =
|
||||
TypeSpec.classBuilder(generatedClassName)
|
||||
.primaryConstructor(constructor)
|
||||
.addSuperinterface(classDeclaration.toClassName())
|
||||
.addProperty(
|
||||
PropertySpec.builder(
|
||||
"parameters",
|
||||
Map::class.parameterizedBy(String::class, String::class),
|
||||
)
|
||||
.initializer("parameters")
|
||||
.addModifiers(KModifier.PRIVATE)
|
||||
.build()
|
||||
)
|
||||
classDeclaration
|
||||
.getAllFunctions()
|
||||
.filterNot { it.isDefaultKotlinFunction() }
|
||||
@@ -97,44 +122,51 @@ class EventTrackerProcessor(
|
||||
private fun generateMethod(function: KSFunctionDeclaration): FunSpec {
|
||||
|
||||
val methodName = function.simpleName.asString()
|
||||
return if (function.isAbstract.not()) {
|
||||
function.generateOverrideMethodWithDefaultSuperCall()
|
||||
} else {
|
||||
|
||||
val parameters = function.parameters
|
||||
val parameters = function.parameters
|
||||
|
||||
val eventName =
|
||||
function.annotations
|
||||
.find { it.shortName.asString() == EventName::class.simpleName }
|
||||
?.arguments
|
||||
?.firstOrNull()
|
||||
?.value as? String
|
||||
?: throw IllegalArgumentException("Event Name is missing for method $methodName.")
|
||||
val eventName =
|
||||
function.annotations
|
||||
.find { it.shortName.asString() == EventName::class.simpleName }
|
||||
?.arguments
|
||||
?.firstOrNull()
|
||||
?.value as? String
|
||||
?: throw IllegalArgumentException(
|
||||
"Event Name is missing for method $methodName."
|
||||
)
|
||||
|
||||
if (eventName.isEmpty()) {
|
||||
throw IllegalArgumentException("Event Name is missing for method $methodName.")
|
||||
if (eventName.isEmpty()) {
|
||||
throw IllegalArgumentException("Event Name is missing for method $methodName.")
|
||||
}
|
||||
|
||||
val paramList =
|
||||
parameters.map { param ->
|
||||
val paramName =
|
||||
param.name?.asString()
|
||||
?: throw IllegalArgumentException(
|
||||
"Parameter name is missing for method $methodName."
|
||||
)
|
||||
val paramType = param.type.resolve().toClassName()
|
||||
ParameterSpec.builder(paramName, paramType).build()
|
||||
}
|
||||
|
||||
val paramEntries =
|
||||
parameters.joinToString(", ") { param ->
|
||||
val paramName = param.name?.asString() ?: "param"
|
||||
"\"${paramName.toSnakeCase()}\" to $paramName.toString()"
|
||||
}
|
||||
|
||||
return FunSpec.builder(methodName)
|
||||
.addModifiers(KModifier.OVERRIDE)
|
||||
.addParameters(paramList)
|
||||
.addStatement(
|
||||
"NaviTrackEvent.trackEventOnClickStream(eventName = %S, eventValues = parameters.plus(mapOf($paramEntries)))",
|
||||
eventName,
|
||||
)
|
||||
.build()
|
||||
}
|
||||
|
||||
val paramList =
|
||||
parameters.map { param ->
|
||||
val paramName =
|
||||
param.name?.asString()
|
||||
?: throw IllegalArgumentException(
|
||||
"Parameter name is missing for method $methodName."
|
||||
)
|
||||
val paramType = param.type.resolve().toClassName()
|
||||
ParameterSpec.builder(paramName, paramType).build()
|
||||
}
|
||||
|
||||
val paramEntries =
|
||||
parameters.joinToString(", ") { param ->
|
||||
val paramName = param.name?.asString() ?: "param"
|
||||
"\"${paramName.toSnakeCase()}\" to $paramName.toString()"
|
||||
}
|
||||
|
||||
return FunSpec.builder(methodName)
|
||||
.addParameters(paramList)
|
||||
.addStatement(
|
||||
"NaviTrackEvent.trackEventOnClickStream(eventName = %S, eventValues = mapOf($paramEntries))",
|
||||
eventName,
|
||||
)
|
||||
.build()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user