From 7a4a896c04133b5cc043c700ee10c13e0358deb2 Mon Sep 17 00:00:00 2001 From: Grzegorz Kozub Date: Tue, 15 Nov 2022 22:59:18 +0100 Subject: [PATCH] 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 * Fix logic for caching json schema. Co-authored-by: liuxinyu Co-authored-by: Pankaj Agrawal Co-authored-by: Michael Brewer --- .../parameters/cache/DataStore.java | 3 +- .../powertools/sqs/internal/BatchContext.java | 2 +- .../validation/ValidationUtils.java | 33 ++++++++++--------- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/powertools-parameters/src/main/java/software/amazon/lambda/powertools/parameters/cache/DataStore.java b/powertools-parameters/src/main/java/software/amazon/lambda/powertools/parameters/cache/DataStore.java index 351ba054d..9ad8df12c 100644 --- a/powertools-parameters/src/main/java/software/amazon/lambda/powertools/parameters/cache/DataStore.java +++ b/powertools-parameters/src/main/java/software/amazon/lambda/powertools/parameters/cache/DataStore.java @@ -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) { diff --git a/powertools-sqs/src/main/java/software/amazon/lambda/powertools/sqs/internal/BatchContext.java b/powertools-sqs/src/main/java/software/amazon/lambda/powertools/sqs/internal/BatchContext.java index 93ad2abc4..1e4eff3bf 100644 --- a/powertools-sqs/src/main/java/software/amazon/lambda/powertools/sqs/internal/BatchContext.java +++ b/powertools-sqs/src/main/java/software/amazon/lambda/powertools/sqs/internal/BatchContext.java @@ -114,7 +114,7 @@ private void processFailedMessages(List 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); diff --git a/powertools-validation/src/main/java/software/amazon/lambda/powertools/validation/ValidationUtils.java b/powertools-validation/src/main/java/software/amazon/lambda/powertools/validation/ValidationUtils.java index 12c51c632..9b73806a5 100644 --- a/powertools-validation/src/main/java/software/amazon/lambda/powertools/validation/ValidationUtils.java +++ b/powertools-validation/src/main/java/software/amazon/lambda/powertools/validation/ValidationUtils.java @@ -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)) { @@ -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); + } + } + /** * */