@@ -12,6 +12,7 @@ import com.intuit.hooks.plugin.codegen.HookParameter
12
12
import com.intuit.hooks.plugin.codegen.HookSignature
13
13
import com.intuit.hooks.plugin.codegen.HookType
14
14
import com.intuit.hooks.plugin.codegen.HookType.Companion.annotationDslMarkers
15
+ import com.intuit.hooks.plugin.ensure
15
16
import com.intuit.hooks.plugin.ksp.HooksProcessor
16
17
import com.intuit.hooks.plugin.ksp.text
17
18
import com.squareup.kotlinpoet.KModifier
@@ -43,6 +44,7 @@ import com.squareup.kotlinpoet.ksp.toTypeName
43
44
override fun toString () = " ${symbol.shortName.asString()} Hook"
44
45
}
45
46
47
+ /* * Build [HookInfo] from the validated [HookAnnotation] found on the [property] */
46
48
context(Raise <Nel <HookValidationError >>)
47
49
internal fun KSPropertyDeclaration.validateHookAnnotation (parentResolver : TypeParameterResolver ): HookInfo {
48
50
val annotation = ensure { onlyHasASingleDslAnnotation() }
@@ -57,24 +59,6 @@ internal fun KSPropertyDeclaration.validateHookAnnotation(parentResolver: TypePa
57
59
)
58
60
}
59
61
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
-
78
62
// TODO: This'd be a good smart constructor use case
79
63
context(Raise <HookValidationError >) private fun KSPropertyDeclaration.onlyHasASingleDslAnnotation (): HookAnnotation {
80
64
val annotations = annotations.filter { it.shortName.asString() in annotationDslMarkers }.toList()
@@ -85,15 +69,6 @@ context(Raise<HookValidationError>) private fun KSPropertyDeclaration.onlyHasASi
85
69
}.let (::HookAnnotation )
86
70
}
87
71
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
-
97
72
context(Raise <HookValidationError >) private fun HookAnnotation.validateParameters (parentResolver : TypeParameterResolver ): List <HookParameter > = try {
98
73
hookFunctionSignatureReference.functionParameters.mapIndexed { index: Int , parameter: KSValueParameter ->
99
74
val name = parameter.name?.asString()
@@ -104,25 +79,9 @@ context(Raise<HookValidationError>) private fun HookAnnotation.validateParameter
104
79
raise(HookValidationError .MustBeHookTypeSignature (this ))
105
80
}
106
81
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
-
117
82
// TODO: This would be obsolete with smart constructor
118
83
context(Raise <HookValidationError .NoCodeGenerator >) private fun HookAnnotation.hasCodeGenerator (): HookType = type
119
84
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
-
126
85
/* * TODO: Another good smart constructor example */
127
86
context(Raise <HookValidationError >)
128
87
private fun HookAnnotation.mustBeHookType (parentResolver : TypeParameterResolver ): HookSignature = try {
@@ -143,21 +102,3 @@ private fun HookAnnotation.mustBeHookType(parentResolver: TypeParameterResolver)
143
102
} catch (exception: Exception ) {
144
103
raise(HookValidationError .MustBeHookTypeSignature (this ))
145
104
}
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