Skip to content

Commit

Permalink
Fix for #577: Some code suggestion from CodeGuru (#984)
Browse files Browse the repository at this point in the history
* avoid repeated get()

* %s to %d for integer

* change put() to putIfAbsent()

* Update powertools-parameters/src/main/java/software/amazon/lambda/powertools/parameters/cache/DataStore.java

Co-authored-by: Michael Brewer <[email protected]>

* Fix logic for caching json schema.

Co-authored-by: liuxinyu <[email protected]>
Co-authored-by: Pankaj Agrawal <[email protected]>
Co-authored-by: Michael Brewer <[email protected]>
  • Loading branch information
4 people authored Nov 15, 2022
1 parent d6405f4 commit 7a4a896
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public void remove(String Key){
}

public Object get(String key) {
return store.containsKey(key)?store.get(key).value:null;
ValueNode node = store.get(key);
return node != null ? node.value : null;
}

public boolean hasExpired(String key, Instant now) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private <T> void processFailedMessages(List<T> successReturns,
map(SQSMessage::getMessageId)
.collect(toList());

LOG.debug(format("[%s] records failed processing, but exceptions are suppressed. " +
LOG.debug(format("[%d] records failed processing, but exceptions are suppressed. " +
"Failed messages %s", failedMessages.size(), messageIds));
} else {
throw new SQSBatchProcessingException(exceptions, failedMessages, successReturns);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,17 @@ public static JsonSchema getJsonSchema(String schema) {
* @return the loaded json schema
*/
public static JsonSchema getJsonSchema(String schema, boolean validateSchema) {
JsonSchema jsonSchema = schemas.get(schema);
JsonSchema jsonSchema = schemas.computeIfAbsent(schema, ValidationUtils::createJsonSchema);

if (jsonSchema != null) {
return jsonSchema;
if (validateSchema) {
validateSchema(schema, jsonSchema);
}

return jsonSchema;
}

private static JsonSchema createJsonSchema(String schema) {
JsonSchema jsonSchema;
if (schema.startsWith(CLASSPATH)) {
String filePath = schema.substring(CLASSPATH.length());
try (InputStream schemaStream = ValidationAspect.class.getResourceAsStream(filePath)) {
Expand All @@ -260,21 +265,19 @@ public static JsonSchema getJsonSchema(String schema, boolean validateSchema) {
jsonSchema = ValidationConfig.get().getFactory().getSchema(schema);
}

if (validateSchema) {
String version = ValidationConfig.get().getSchemaVersion().toString();
try {
validate(jsonSchema.getSchemaNode(),
getJsonSchema("classpath:/schemas/meta_schema_" + version));
} catch (ValidationException ve) {
throw new IllegalArgumentException("The schema " + schema + " is not valid, it does not respect the specification " + version, ve);
}
}

schemas.put(schema, jsonSchema);

return jsonSchema;
}

private static void validateSchema(String schema, JsonSchema jsonSchema) {
String version = ValidationConfig.get().getSchemaVersion().toString();
try {
validate(jsonSchema.getSchemaNode(),
getJsonSchema("classpath:/schemas/meta_schema_" + version));
} catch (ValidationException ve) {
throw new IllegalArgumentException("The schema " + schema + " is not valid, it does not respect the specification " + version, ve);
}
}

/**
*
*/
Expand Down

0 comments on commit 7a4a896

Please sign in to comment.