Skip to content

Commit 71c1c2f

Browse files
committed
Refactor RepositoryProcessor to improve context parameter validation and replace parameter lists with function declarations in validation methods.
1 parent adf6de0 commit 71c1c2f

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

sqlx4k-codegen/src/jvmMain/kotlin/io/github/smyrgeorge/sqlx4k/processor/RepositoryProcessor.kt

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -249,21 +249,32 @@ class RepositoryProcessor(
249249
}
250250

251251
/**
252-
* Validates the context parameter for a repository method, ensuring that it meets the required structure and conventions
253-
* when the context parameter is expected.
252+
* Validates the context parameter for a given function declaration based on the specified mode.
254253
*
255-
* @param name The name of the repository method being validated.
256-
* @param params The list of parameters declared by the repository method.
257-
* @param useContextParameters A flag indicating whether context parameters are used in the repository.
258-
* If `true`, context parameter validation is skipped.
254+
* @param fn The function declaration to validate.
255+
* @param useContextParameters A flag indicating whether context parameters mode is enabled.
259256
*/
260257
private fun validateContextParameter(
261-
name: String,
262-
params: List<KSValueParameter>,
258+
fn: KSFunctionDeclaration,
263259
useContextParameters: Boolean
264260
) {
265-
// TODO: somehow validate that method has context(context: QueryExecutor).
266-
if (useContextParameters) return
261+
val name = fn.simpleName()
262+
val params = fn.parameters
263+
if (useContextParameters) {
264+
// TODO: somehow validate that method has context(context: QueryExecutor).
265+
// Ensure there is no explicit parameter of type QueryExecutor named 'context'
266+
val hasExplicitContextParam = params.any { p ->
267+
val pType = p.type.resolve()
268+
val qn = pType.declaration.qualifiedName()
269+
val pName = p.name?.asString()
270+
qn == TypeNames.QUERY_EXECUTOR && pName == "context"
271+
}
272+
if (hasExplicitContextParam) {
273+
error("Repository method '$name' must not declare parameter 'context: ${TypeNames.QUERY_EXECUTOR}' when using context parameters")
274+
}
275+
return
276+
}
277+
// Non-context-parameters mode: the first parameter must be 'context: QueryExecutor'
267278
if (params.isEmpty())
268279
error("Repository method '$name' must declare first parameter 'context: ${TypeNames.QUERY_EXECUTOR}'")
269280
val first = params.first()
@@ -343,17 +354,17 @@ class RepositoryProcessor(
343354
* Validates the arity of the parameters in a repository method based on its prefix and other constraints.
344355
*
345356
* @param prefix The prefix of the method, which determines the expected parameter count and validation rules.
346-
* @param name The name of the method being validated.
347-
* @param params The list of parameters declared by the method.
357+
* @param fn The function declaration to validate.
348358
* @param useContextParameters A flag indicating whether the method uses context parameters.
349359
* If true, context parameters are excluded from standard parameter arity validation.
350360
*/
351361
private fun validateParameterArity(
352362
prefix: Prefix,
353-
name: String,
354-
params: List<KSValueParameter>,
363+
fn: KSFunctionDeclaration,
355364
useContextParameters: Boolean
356365
) {
366+
val name = fn.simpleName()
367+
val params = fn.parameters
357368
val nonContextCount = if (useContextParameters) params.size else params.size - 1
358369
when (prefix) {
359370
Prefix.FIND_ALL, Prefix.DELETE_ALL, Prefix.COUNT_ALL -> if (nonContextCount != 0) error("Method '$name' must not have parameters other than context")
@@ -456,8 +467,8 @@ class RepositoryProcessor(
456467
"$pName: $pType"
457468
}
458469

459-
validateContextParameter(name, params, useContextParameters)
460-
validateParameterArity(prefix, name, params, useContextParameters)
470+
validateContextParameter(fn, useContextParameters)
471+
validateParameterArity(prefix, fn, useContextParameters)
461472
validateParameters(sql, fn, useContextParameters)
462473
validateReturnType(prefix, fn, domainDecl)
463474
if (validateSyntax) SqlValidator.validateQuerySyntax(fn.simpleName(), sql)

0 commit comments

Comments
 (0)