@@ -12,6 +12,7 @@ import com.intuit.hooks.plugin.codegen.HookParameter
1212import com.intuit.hooks.plugin.codegen.HookSignature
1313import com.intuit.hooks.plugin.codegen.HookType
1414import com.intuit.hooks.plugin.codegen.HookType.Companion.annotationDslMarkers
15+ import com.intuit.hooks.plugin.ensure
1516import com.intuit.hooks.plugin.ksp.HooksProcessor
1617import com.intuit.hooks.plugin.ksp.text
1718import com.squareup.kotlinpoet.KModifier
@@ -43,6 +44,7 @@ import com.squareup.kotlinpoet.ksp.toTypeName
4344 override fun toString () = " ${symbol.shortName.asString()} Hook"
4445}
4546
47+ /* * Build [HookInfo] from the validated [HookAnnotation] found on the [property] */
4648context(Raise <Nel <HookValidationError >>)
4749internal fun KSPropertyDeclaration.validateHookAnnotation (parentResolver : TypeParameterResolver ): HookInfo {
4850 val annotation = ensure { onlyHasASingleDslAnnotation() }
@@ -57,24 +59,6 @@ internal fun KSPropertyDeclaration.validateHookAnnotation(parentResolver: TypePa
5759 )
5860}
5961
60- /* * Build [HookInfo] from the validated [HookAnnotation] found on the [property] */
61- internal fun KSPropertyDeclaration.validateHookAnnotation (parentResolver : TypeParameterResolver ): ValidatedNel <HookValidationError , HookInfo > =
62- onlyHasASingleDslAnnotation().andThen { annotation ->
63-
64- val hasCodeGenerator = hasCodeGenerator(annotation)
65- val mustBeHookType = mustBeHookType(annotation, parentResolver)
66- val validateParameters = validateParameters(annotation, parentResolver)
67- val hookMember = simpleName.asString()
68- val propertyVisibility = this .getVisibility().toKModifier() ? : KModifier .PUBLIC
69-
70- hasCodeGenerator.zip(
71- mustBeHookType,
72- validateParameters
73- ) { hookType: HookType , hookSignature: HookSignature , hookParameters: List <HookParameter > ->
74- HookInfo (hookMember, hookType, hookSignature, hookParameters, propertyVisibility)
75- }
76- }
77-
7862// TODO: This'd be a good smart constructor use case
7963context(Raise <HookValidationError >) private fun KSPropertyDeclaration.onlyHasASingleDslAnnotation (): HookAnnotation {
8064 val annotations = annotations.filter { it.shortName.asString() in annotationDslMarkers }.toList()
@@ -85,15 +69,6 @@ context(Raise<HookValidationError>) private fun KSPropertyDeclaration.onlyHasASi
8569 }.let (::HookAnnotation )
8670}
8771
88- private fun KSPropertyDeclaration.onlyHasASingleDslAnnotation (): ValidatedNel <HookValidationError , HookAnnotation > {
89- val annotations = annotations.filter { it.shortName.asString() in annotationDslMarkers }.toList()
90- return when (annotations.size) {
91- 0 -> HookValidationError .NoHookDslAnnotations (this ).invalidNel()
92- 1 -> annotations.single().let (::HookAnnotation ).valid()
93- else -> HookValidationError .TooManyHookDslAnnotations (annotations, this ).invalidNel()
94- }
95- }
96-
9772context(Raise <HookValidationError >) private fun HookAnnotation.validateParameters (parentResolver : TypeParameterResolver ): List <HookParameter > = try {
9873 hookFunctionSignatureReference.functionParameters.mapIndexed { index: Int , parameter: KSValueParameter ->
9974 val name = parameter.name?.asString()
@@ -104,25 +79,9 @@ context(Raise<HookValidationError>) private fun HookAnnotation.validateParameter
10479 raise(HookValidationError .MustBeHookTypeSignature (this ))
10580}
10681
107- private fun validateParameters (annotation : HookAnnotation , parentResolver : TypeParameterResolver ): ValidatedNel <HookValidationError , List <HookParameter >> = try {
108- annotation.hookFunctionSignatureReference.functionParameters.mapIndexed { index: Int , parameter: KSValueParameter ->
109- val name = parameter.name?.asString()
110- val type = parameter.type.toTypeName(parentResolver)
111- HookParameter (name, type, index)
112- }.valid()
113- } catch (exception: Exception ) {
114- HookValidationError .MustBeHookTypeSignature (annotation).invalidNel()
115- }
116-
11782// TODO: This would be obsolete with smart constructor
11883context(Raise <HookValidationError .NoCodeGenerator >) private fun HookAnnotation.hasCodeGenerator (): HookType = type
11984
120- private fun hasCodeGenerator (annotation : HookAnnotation ): ValidatedNel <HookValidationError , HookType > = try {
121- annotation.type!! .valid()
122- } catch (e: Exception ) {
123- HookValidationError .NoCodeGenerator (annotation).invalidNel()
124- }
125-
12685/* * TODO: Another good smart constructor example */
12786context(Raise <HookValidationError >)
12887private fun HookAnnotation.mustBeHookType (parentResolver : TypeParameterResolver ): HookSignature = try {
@@ -143,21 +102,3 @@ private fun HookAnnotation.mustBeHookType(parentResolver: TypeParameterResolver)
143102} catch (exception: Exception ) {
144103 raise(HookValidationError .MustBeHookTypeSignature (this ))
145104}
146- private fun mustBeHookType (annotation : HookAnnotation , parentResolver : TypeParameterResolver ): ValidatedNel <HookValidationError , HookSignature > = try {
147- val isSuspend: Boolean = annotation.hookFunctionSignatureType.modifiers.contains(Modifier .SUSPEND )
148- // I'm leaving this here because KSP knows that it's (String) -> Int, whereas once it gets to Poet, it's just kotlin.Function1<kotlin.Int, kotlin.String>
149- val text = annotation.hookFunctionSignatureType.text
150- val hookFunctionSignatureType = annotation.hookFunctionSignatureType.toTypeName(parentResolver)
151- val returnType = annotation.hookFunctionSignatureReference.returnType.toTypeName(parentResolver)
152- val returnTypeType = annotation.hookFunctionSignatureReference.returnType.element?.typeArguments?.firstOrNull()?.toTypeName(parentResolver)
153-
154- HookSignature (
155- text,
156- isSuspend,
157- returnType,
158- returnTypeType,
159- hookFunctionSignatureType
160- ).valid()
161- } catch (exception: Exception ) {
162- HookValidationError .MustBeHookTypeSignature (annotation).invalidNel()
163- }
0 commit comments