Skip to content

Commit 7a4a896

Browse files
kozubSxinyuliupankajagrawal16Michael Brewer
authored
Fix for #577: Some code suggestion from CodeGuru (#984)
* 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]>
1 parent d6405f4 commit 7a4a896

File tree

3 files changed

+21
-17
lines changed
  • powertools-parameters/src/main/java/software/amazon/lambda/powertools/parameters/cache
  • powertools-sqs/src/main/java/software/amazon/lambda/powertools/sqs/internal
  • powertools-validation/src/main/java/software/amazon/lambda/powertools/validation

3 files changed

+21
-17
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ public void remove(String Key){
4646
}
4747

4848
public Object get(String key) {
49-
return store.containsKey(key)?store.get(key).value:null;
49+
ValueNode node = store.get(key);
50+
return node != null ? node.value : null;
5051
}
5152

5253
public boolean hasExpired(String key, Instant now) {

powertools-sqs/src/main/java/software/amazon/lambda/powertools/sqs/internal/BatchContext.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ private <T> void processFailedMessages(List<T> successReturns,
114114
map(SQSMessage::getMessageId)
115115
.collect(toList());
116116

117-
LOG.debug(format("[%s] records failed processing, but exceptions are suppressed. " +
117+
LOG.debug(format("[%d] records failed processing, but exceptions are suppressed. " +
118118
"Failed messages %s", failedMessages.size(), messageIds));
119119
} else {
120120
throw new SQSBatchProcessingException(exceptions, failedMessages, successReturns);

powertools-validation/src/main/java/software/amazon/lambda/powertools/validation/ValidationUtils.java

+18-15
Original file line numberDiff line numberDiff line change
@@ -239,12 +239,17 @@ public static JsonSchema getJsonSchema(String schema) {
239239
* @return the loaded json schema
240240
*/
241241
public static JsonSchema getJsonSchema(String schema, boolean validateSchema) {
242-
JsonSchema jsonSchema = schemas.get(schema);
242+
JsonSchema jsonSchema = schemas.computeIfAbsent(schema, ValidationUtils::createJsonSchema);
243243

244-
if (jsonSchema != null) {
245-
return jsonSchema;
244+
if (validateSchema) {
245+
validateSchema(schema, jsonSchema);
246246
}
247247

248+
return jsonSchema;
249+
}
250+
251+
private static JsonSchema createJsonSchema(String schema) {
252+
JsonSchema jsonSchema;
248253
if (schema.startsWith(CLASSPATH)) {
249254
String filePath = schema.substring(CLASSPATH.length());
250255
try (InputStream schemaStream = ValidationAspect.class.getResourceAsStream(filePath)) {
@@ -260,21 +265,19 @@ public static JsonSchema getJsonSchema(String schema, boolean validateSchema) {
260265
jsonSchema = ValidationConfig.get().getFactory().getSchema(schema);
261266
}
262267

263-
if (validateSchema) {
264-
String version = ValidationConfig.get().getSchemaVersion().toString();
265-
try {
266-
validate(jsonSchema.getSchemaNode(),
267-
getJsonSchema("classpath:/schemas/meta_schema_" + version));
268-
} catch (ValidationException ve) {
269-
throw new IllegalArgumentException("The schema " + schema + " is not valid, it does not respect the specification " + version, ve);
270-
}
271-
}
272-
273-
schemas.put(schema, jsonSchema);
274-
275268
return jsonSchema;
276269
}
277270

271+
private static void validateSchema(String schema, JsonSchema jsonSchema) {
272+
String version = ValidationConfig.get().getSchemaVersion().toString();
273+
try {
274+
validate(jsonSchema.getSchemaNode(),
275+
getJsonSchema("classpath:/schemas/meta_schema_" + version));
276+
} catch (ValidationException ve) {
277+
throw new IllegalArgumentException("The schema " + schema + " is not valid, it does not respect the specification " + version, ve);
278+
}
279+
}
280+
278281
/**
279282
*
280283
*/

0 commit comments

Comments
 (0)