-
Notifications
You must be signed in to change notification settings - Fork 5
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
Yet another approach to type safe step parameters #1562
Draft
pshapiro4broad
wants to merge
15
commits into
develop
Choose a base branch
from
ps-typesafe-flight-map-2
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 13 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
56f7de1
Yet another approach to type safe step parameters. This uses field an…
pshapiro4broad 191c2b3
remove debugging code
pshapiro4broad 3668804
Restore accidentally deleted line
pshapiro4broad 60dff7e
Merge remote-tracking branch 'origin/develop' into ps-typesafe-flight…
pshapiro4broad 26af358
resolve merge conflicts from develop
pshapiro4broad 71cd40e
Support the case where a step input might not be set
pshapiro4broad 3fba7a3
Add back in error check and work around case where a field is both an…
pshapiro4broad 97ae6a1
Merge branch 'develop' into ps-typesafe-flight-map-2
pshapiro4broad a82ef71
update from main
pshapiro4broad 03ac4fe
fix tests
pshapiro4broad dd4b378
update test to junit 5; suppress spotbugs warnings for now
pshapiro4broad 615641d
resolve some warnings, add support for named injections
pshapiro4broad 93332ef
add test for named input/output fields
pshapiro4broad 99cef5f
begin to add dataflow analysis
pshapiro4broad 9819808
Merge branch 'refs/heads/develop' into ps-typesafe-flight-map-2
pshapiro4broad 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 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 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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package bio.terra.common; | ||
|
||
import bio.terra.model.ErrorModel; | ||
import bio.terra.stairway.FlightContext; | ||
import bio.terra.stairway.Step; | ||
import bio.terra.stairway.StepResult; | ||
import bio.terra.stairway.exception.RetryException; | ||
import com.google.common.annotations.VisibleForTesting; | ||
import org.springframework.http.HttpStatus; | ||
|
||
public abstract class BaseStep implements Step { | ||
@StepOutput protected Object response; | ||
@StepOutput protected HttpStatus statusCode; | ||
|
||
private FlightContext context; | ||
|
||
@Override | ||
public final StepResult doStep(FlightContext context) | ||
throws InterruptedException, RetryException { | ||
this.context = context; | ||
StepUtils.readInputs(this, context); | ||
try { | ||
return perform(); | ||
} finally { | ||
StepUtils.writeOutputs(this, context); | ||
} | ||
} | ||
|
||
@Override | ||
public final StepResult undoStep(FlightContext context) throws InterruptedException { | ||
this.context = context; | ||
StepUtils.readInputs(this, context); | ||
return undo(); | ||
} | ||
|
||
public abstract StepResult perform() throws InterruptedException, RetryException; | ||
|
||
public StepResult undo() throws InterruptedException { | ||
// Many steps aren't undoable. | ||
return StepResult.getStepResultSuccess(); | ||
} | ||
|
||
protected void setErrorResponse(String message, HttpStatus responseStatus) { | ||
ErrorModel errorModel = new ErrorModel().message(message); | ||
setResponse(errorModel, responseStatus); | ||
} | ||
|
||
protected void setResponse(Object responseObject, HttpStatus responseStatus) { | ||
response = responseObject; | ||
statusCode = responseStatus; | ||
} | ||
|
||
protected void setResponse(Object responseObject) { | ||
response = responseObject; | ||
statusCode = HttpStatus.OK; | ||
} | ||
|
||
protected FlightContext getContext() { | ||
return context; | ||
} | ||
|
||
protected String getFlightId() { | ||
return context.getFlightId(); | ||
} | ||
|
||
@VisibleForTesting | ||
public Object getResponse() { | ||
return response; | ||
} | ||
|
||
@VisibleForTesting | ||
public HttpStatus getStatusCode() { | ||
return statusCode; | ||
} | ||
} |
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package bio.terra.common; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.FIELD) | ||
public @interface StepInput { | ||
String USE_DEFAULT_NAME = ""; | ||
|
||
String value() default USE_DEFAULT_NAME; | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package bio.terra.common; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.FIELD) | ||
public @interface StepOutput { | ||
String USE_DEFAULT_NAME = ""; | ||
|
||
String value() default USE_DEFAULT_NAME; | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package bio.terra.common; | ||
|
||
import bio.terra.stairway.FlightContext; | ||
import bio.terra.stairway.FlightMap; | ||
import bio.terra.stairway.Step; | ||
import java.lang.reflect.Field; | ||
|
||
// Suppress sonar warnings about reflection API usage. Reflection APIs must be used to read and | ||
// write fields in a Step. | ||
@SuppressWarnings({"java:S3011"}) | ||
public class StepUtils { | ||
|
||
public static class MissingStepInputException extends RuntimeException { | ||
public MissingStepInputException(String key) { | ||
super("No flight value found for StepInput key '" + key + "'"); | ||
} | ||
} | ||
|
||
public static class IllegalSetException extends RuntimeException { | ||
public IllegalSetException(Throwable cause) { | ||
super(cause); | ||
} | ||
} | ||
|
||
public static class IllegalGetException extends RuntimeException { | ||
public IllegalGetException(Throwable cause) { | ||
super(cause); | ||
} | ||
} | ||
|
||
private StepUtils() {} | ||
|
||
public static String keyFromField(Field field) { | ||
var input = field.getAnnotation(StepInput.class); | ||
if (input != null && !input.value().isEmpty()) { | ||
return input.value(); | ||
} | ||
var output = field.getAnnotation(StepOutput.class); | ||
if (output != null && !output.value().isEmpty()) { | ||
return output.value(); | ||
} | ||
return field.getName(); | ||
} | ||
|
||
public static void readInputs(Step step, FlightContext context) throws MissingStepInputException { | ||
for (Class<?> clazz = step.getClass(); clazz != null; clazz = clazz.getSuperclass()) { | ||
for (Field field : clazz.getDeclaredFields()) { | ||
if (field.isAnnotationPresent(StepInput.class)) { | ||
String key = keyFromField(field); | ||
if (context.getInputParameters().containsKey(key)) { | ||
setField(step, context.getInputParameters(), field, key); | ||
} else if (context.getWorkingMap().containsKey(key)) { | ||
setField(step, context.getWorkingMap(), field, key); | ||
} else if (!field.isAnnotationPresent(StepOutput.class)) { | ||
// If the field is only used as an input, report an error if there's no value for it. | ||
throw new MissingStepInputException(key); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private static void setField(Step step, FlightMap map, Field field, String key) { | ||
field.setAccessible(true); | ||
try { | ||
field.set(step, map.get(key, field.getType())); | ||
} catch (IllegalAccessException e) { | ||
throw new IllegalSetException(e); | ||
} | ||
} | ||
|
||
public static void writeOutputs(Step step, FlightContext context) { | ||
for (Class<?> clazz = step.getClass(); clazz != null; clazz = clazz.getSuperclass()) { | ||
for (Field field : clazz.getDeclaredFields()) { | ||
if (field.isAnnotationPresent(StepOutput.class)) { | ||
field.setAccessible(true); | ||
final Object value; | ||
try { | ||
value = field.get(step); | ||
} catch (IllegalAccessException e) { | ||
throw new IllegalGetException(e); | ||
} | ||
if (value == null) { | ||
// An unset output can occur if an exception is thrown inside the run() operation. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thinking about this again, maybe instead the code shouldn't write outputs to the map if an exception occurs. Stairway might store flight context data even on error, but that doesn't seem like something we should ever count on. |
||
continue; | ||
} | ||
context.getWorkingMap().put(keyFromField(field), value); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains 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
12 changes: 4 additions & 8 deletions
12
src/main/java/bio/terra/service/common/ResourceLockSetResponseStep.java
This file contains 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 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 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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I kept this around in case it's needed but in general steps that use this API shouldn't need to access the context directly.