-
Notifications
You must be signed in to change notification settings - Fork 5
BailHook default handler #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9dc4cce
add default handler to bail hook
brocollie08 2e74672
tests
brocollie08 589beed
reverse, call handler if not bail
brocollie08 f5d2055
smarter way to do the same thing
brocollie08 65f4d5b
bail hook generation
brocollie08 68cd163
multiple call function generation
brocollie08 f28a6f1
extract callBuilder
brocollie08 3a4f405
rename second call
brocollie08 7c57254
remove git add .
brocollie08 549367a
fixed other bail hooks
brocollie08 73e843a
apiDump
brocollie08 73d153c
Update processor/src/main/kotlin/com/intuit/hooks/plugin/codegen/Poet.kt
brocollie08 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 3 additions & 2 deletions
5
hooks/src/main/kotlin/com/intuit/hooks/AsyncSeriesBailHook.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,16 @@ | ||
package com.intuit.hooks | ||
|
||
public abstract class AsyncSeriesBailHook<F : Function<BailResult<R>>, R> : AsyncBaseHook<F>("AsyncSeriesBailHook") { | ||
protected suspend fun call(invokeWithContext: suspend (F, HookContext) -> BailResult<R>): R? { | ||
protected suspend fun call(invokeWithContext: suspend (F, HookContext) -> BailResult<R>, default: (suspend (HookContext) -> R)? = null): R? { | ||
val context = setup(invokeWithContext) | ||
|
||
taps.forEach { tapInfo -> | ||
when (val result = invokeWithContext(tapInfo.f, context)) { | ||
is BailResult.Bail<R> -> return@call result.value | ||
is BailResult.Continue -> {} | ||
} | ||
} | ||
|
||
return null | ||
return default?.invoke(context) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,23 +40,24 @@ private fun HooksContainer.generateContainerClass(): TypeSpec { | |
}.build() | ||
} | ||
|
||
internal val HookInfo.callBuilder get() = FunSpec.builder("call") | ||
.addParameters(parameterSpecs) | ||
.apply { | ||
if (isAsync) | ||
addModifiers(KModifier.SUSPEND) | ||
} | ||
|
||
internal fun HookInfo.generateClass(): TypeSpec { | ||
val callBuilder = FunSpec.builder("call") | ||
.addParameters(parameterSpecs) | ||
.apply { | ||
if ([email protected]) | ||
addModifiers(KModifier.SUSPEND) | ||
} | ||
|
||
val (superclass, call) = when (hookType) { | ||
val (superclass, calls) = when (hookType) { | ||
HookType.SyncHook, HookType.AsyncSeriesHook, HookType.AsyncParallelHook -> { | ||
val superclass = createSuperClass() | ||
|
||
val call = callBuilder | ||
.returns(UNIT) | ||
.addStatement("return super.call { f, context -> f(context, $paramsWithoutTypes) }") | ||
|
||
Pair(superclass, call) | ||
Pair(superclass, listOf(call)) | ||
} | ||
HookType.SyncLoopHook, HookType.AsyncSeriesLoopHook -> { | ||
val superclass = createSuperClass(interceptParameter) | ||
|
@@ -69,7 +70,7 @@ internal fun HookInfo.generateClass(): TypeSpec { | |
CodeBlock.of("{ f, context -> f(context, $paramsWithoutTypes) }") | ||
) | ||
|
||
Pair(superclass, call) | ||
Pair(superclass, listOf(call)) | ||
} | ||
HookType.SyncWaterfallHook, HookType.AsyncSeriesWaterfallHook -> { | ||
val superclass = createSuperClass(params.first().type) | ||
|
@@ -84,31 +85,51 @@ internal fun HookInfo.generateClass(): TypeSpec { | |
CodeBlock.of("{ f, context -> f(context, $paramsWithoutTypes) }") | ||
) | ||
|
||
Pair(superclass, call) | ||
Pair(superclass, listOf(call)) | ||
} | ||
HookType.SyncBailHook, HookType.AsyncSeriesBailHook -> { | ||
requireNotNull(hookSignature.nullableReturnTypeType) | ||
val superclass = createSuperClass(hookSignature.returnTypeType) | ||
|
||
val call = callBuilder | ||
.addParameter( | ||
ParameterSpec.builder( | ||
"default", | ||
LambdaTypeName.get( | ||
parameters = parameterSpecs, | ||
returnType = hookSignature.returnTypeType!! | ||
) | ||
).build() | ||
) | ||
.returns(hookSignature.nullableReturnTypeType) | ||
.addStatement("return super.call { f, context -> f(context, $paramsWithoutTypes) }") | ||
.addStatement("return call ($paramsWithoutTypes) { _, arg1 -> default.invoke(arg1) }") | ||
brocollie08 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
val contextCall = callBuilder | ||
.addParameter( | ||
ParameterSpec.builder( | ||
"default", | ||
createHookContextLambda(hookSignature.returnTypeType).copy(nullable = true) | ||
).defaultValue(CodeBlock.of("null")).build() | ||
) | ||
.returns(hookSignature.nullableReturnTypeType) | ||
.addStatement("return super.call ({ f, context -> f(context, $paramsWithoutTypes) }, default?.let { { context -> default(context, $paramsWithoutTypes) } } )") | ||
|
||
Pair(superclass, call) | ||
Pair(superclass, listOf(call, contextCall)) | ||
} | ||
// parallel bail requires the concurrency parameter, otherwise it would be just like the other bail hooks | ||
HookType.AsyncParallelBailHook -> { | ||
requireNotNull(hookSignature.nullableReturnTypeType) | ||
val superclass = createSuperClass(hookSignature.returnTypeType) | ||
|
||
// force the concurrency parameter to be first | ||
callBuilder.parameters.add(0, ParameterSpec("concurrency", INT)) | ||
val call = with(callBuilder) { | ||
// force the concurrency parameter to be first | ||
parameters.add(0, ParameterSpec("concurrency", INT)) | ||
|
||
val call = callBuilder | ||
.returns(hookSignature.nullableReturnTypeType) | ||
.addStatement("return super.call(concurrency) { f, context -> f(context, $paramsWithoutTypes) }") | ||
returns(hookSignature.nullableReturnTypeType) | ||
.addStatement("return super.call(concurrency) { f, context -> f(context, $paramsWithoutTypes) }") | ||
} | ||
|
||
Pair(superclass, call) | ||
Pair(superclass, listOf(call)) | ||
} | ||
} | ||
|
||
|
@@ -117,7 +138,7 @@ internal fun HookInfo.generateClass(): TypeSpec { | |
addFunctions(tapMethods) | ||
hookType.addedAnnotation?.let(::addAnnotation) | ||
superclass(superclass) | ||
addFunction(call.build()) | ||
addFunctions(calls.map { it.build() }) | ||
}.build() | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.