Skip to content

feat: codegen for waitUntil[State] #318

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 12 commits into from
May 7, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -70,27 +70,44 @@ private void generateWaiter() {
writer.addImport("createWaiter", "createWaiter", WAITABLE_UTIL_PACKAGE);
writer.addImport("WaiterResult", "WaiterResult", WAITABLE_UTIL_PACKAGE);
writer.addImport("WaiterState", "WaiterState", WAITABLE_UTIL_PACKAGE);
writer.addImport("checkExceptions", "checkExceptions", WAITABLE_UTIL_PACKAGE);
writer.addImport("WaiterConfiguration", "WaiterConfiguration", WAITABLE_UTIL_PACKAGE);

// generates (deprecated) WaitFor....
writer.writeDocs(waiter.getDocumentation().orElse("") + " \n"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove waiter documentation, as waitFor is deprecated. Users should read documentation for waitUntil instead.

Suggested change
writer.writeDocs(waiter.getDocumentation().orElse("") + " \n"
writer.writeDocs(

+ " @param params : Waiter configuration options.\n"
+ " @param input : the input to " + operationSymbol.getName() + " for polling.");
+ " @deprecated Use waitUntil" + waiterName + " instead. "
+ "waitFor" + waiterName + " does not throw error in non-success cases.");
writer.openBlock("export const waitFor$L = async (params: WaiterConfiguration<$T>, input: $T): "
+ "Promise<WaiterResult> => {", "}", waiterName, serviceSymbol, inputSymbol, () -> {
writer.write("const serviceDefaults = { minDelay: $L, maxDelay: $L };", waiter.getMinDelay(),
waiter.getMaxDelay());
writer.write("return createWaiter({...serviceDefaults, ...params}, input, checkState);");
});

// generates WaitUtil....
writer.writeDocs(waiter.getDocumentation().orElse("") + " \n"
+ " @param params - Waiter configuration options.\n"
+ " @param input - The input to " + operationSymbol.getName() + " for polling.");
writer.openBlock("export const waitUntil$L = async (params: WaiterConfiguration<$T>, input: $T): "
+ "Promise<WaiterResult> => {", "}", waiterName, serviceSymbol, inputSymbol, () -> {
writer.write("const serviceDefaults = { minDelay: $L, maxDelay: $L };", waiter.getMinDelay(),
waiter.getMaxDelay());
writer.write("const result = await createWaiter({...serviceDefaults, ...params}, input, checkState);");
writer.write("return checkExceptions(result);");
});
}

private void generateAcceptors() {
writer.openBlock("const checkState = async (client: $T, input: $T): Promise<WaiterResult> => {", "}",
serviceSymbol, inputSymbol, () -> {
writer.write("let reason;");
writer.openBlock("try {", "}", () -> {
writer.write("let result: any = await client.send(new $T(input))", operationSymbol);
writer.write("reason = result;");
writeAcceptors("result", false);
});
writer.openBlock("catch (exception) {", "}", () -> {
writer.write("reason = exception;");
writeAcceptors("exception", true);
});
writer.write("return $L;", makeWaiterResult(AcceptorState.RETRY));
Expand Down Expand Up @@ -165,11 +182,11 @@ private void generatePathMatcher(String accessor, PathMatcher pathMatcher, Accep

private String makeWaiterResult(AcceptorState resultantState) {
if (resultantState == AcceptorState.SUCCESS) {
return "{ state: WaiterState.SUCCESS }";
return "{ state: WaiterState.SUCCESS, reason }";
} else if (resultantState == AcceptorState.FAILURE) {
return "{ state: WaiterState.FAILURE }";
return "{ state: WaiterState.FAILURE, reason }";
} else if (resultantState == AcceptorState.RETRY) {
return "{ state: WaiterState.RETRY }";
return "{ state: WaiterState.RETRY, reason }";
}
throw new CodegenException("Hit an invalid acceptor state to codegen " + resultantState.toString());
}
Expand Down