From 86f3084ff061a2a2dd68de3da6de0128e7489ffa Mon Sep 17 00:00:00 2001 From: Priamo Germosen <6233424+pgermosen@users.noreply.github.com> Date: Fri, 18 Sep 2026 21:17:44 -0400 Subject: [PATCH] refactor(s3): dedupe XmlElement child-count helper (#3844) S3AnalyticsConfiguration and S3InventoryConfiguration each carried an identical private count(XmlElement, String) helper (#3843), a follow-up from review of #3835/#3836. That review comment also suggested the two share a second Filter grammar of their own, but checked against botocore: AnalyticsFilter supports Prefix/Tag/And (the same shape #3835 covers) while InventoryFilter is Prefix-only, so there's no real Filter-grammar duplication between these two to extract, only this helper. Turned it into an instance method on XmlElement itself in XmlParser, alongside the existing child() method, rather than a second static utility: it belongs on the type both files were already calling .child()/.children() on. Updated both call sites from count(x, "Y") to x.count("Y") and removed each file's own copy. Also looked at the Destination/S3BucketDestination builders both files have; left them separate since the actual fields differ (AccountId vs BucketAccountId, different supported Format lists, Encryption only on Inventory's side), and forcing a shared shape across those differences would be a worse abstraction than three similar lines. No behavior change: verified against both configuration types' unit and integration test suites plus XmlParserTest. --- .../hectorvent/floci/core/common/XmlParser.java | 4 ++++ .../services/s3/S3AnalyticsConfiguration.java | 16 ++++++---------- .../services/s3/S3InventoryConfiguration.java | 14 +++++--------- 3 files changed, 15 insertions(+), 19 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/core/common/XmlParser.java b/src/main/java/io/github/hectorvent/floci/core/common/XmlParser.java index 3bdc586d8c..1453138374 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/XmlParser.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/XmlParser.java @@ -468,6 +468,10 @@ public final class XmlParser { .findFirst() .orElse(null); } + + public long count(String childName) { + return children.stream().filter(child -> childName.equals(child.name())).count(); + } } /** diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3AnalyticsConfiguration.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3AnalyticsConfiguration.java index d48cdc1729..54005fe070 100644 --- a/src/main/java/io/github/hectorvent/floci/services/s3/S3AnalyticsConfiguration.java +++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3AnalyticsConfiguration.java @@ -40,9 +40,9 @@ record S3AnalyticsConfiguration(String id, String innerXml) { // The configuration is one Id, at most one Filter and one StorageClassAnalysis, so // anything else under the root is a body AWS would not have accepted. - long ids = count(root, "Id"); - long filters = count(root, "Filter"); - long analyses = count(root, "StorageClassAnalysis"); + long ids = root.count("Id"); + long filters = root.count("Filter"); + long analyses = root.count("StorageClassAnalysis"); if (ids != 1 || filters > 1 || analyses != 1 || root.children().size() != ids + filters + analyses) { throw malformed(); @@ -78,8 +78,8 @@ record S3AnalyticsConfiguration(String id, String innerXml) { case "Tag" -> tagXml(predicate); case "And" -> { List conjuncts = predicate.children(); - long prefixes = count(predicate, "Prefix"); - long tags = count(predicate, "Tag"); + long prefixes = predicate.count("Prefix"); + long tags = predicate.count("Tag"); // Every conjunct has to be one floci understands, or the filter it stores would // not be the filter that was sent. if (conjuncts.size() < 2 || prefixes > 1 || conjuncts.size() != prefixes + tags) { @@ -112,7 +112,7 @@ record S3AnalyticsConfiguration(String id, String innerXml) { * the output schema version and a destination bucket, with the account id and prefix optional. */ private static String storageClassAnalysisXml(XmlElement analysis) { - long exports = count(analysis, "DataExport"); + long exports = analysis.count("DataExport"); if (exports > 1 || analysis.children().size() != exports) { throw malformed(); } @@ -156,8 +156,4 @@ record S3AnalyticsConfiguration(String id, String innerXml) { } return out.end("S3BucketDestination").end("Destination").end("DataExport").build(); } - - private static long count(XmlElement parent, String childName) { - return parent.children().stream().filter(child -> childName.equals(child.name())).count(); - } } diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3InventoryConfiguration.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3InventoryConfiguration.java index fba9c40919..bf41b53d84 100644 --- a/src/main/java/io/github/hectorvent/floci/services/s3/S3InventoryConfiguration.java +++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3InventoryConfiguration.java @@ -48,11 +48,11 @@ record S3InventoryConfiguration(String id, String innerXml) { // The configuration is one each of Id, IsEnabled, IncludedObjectVersions, Schedule and // Destination, with Filter and OptionalFields at most once, so anything else under the // root is a body AWS would not have accepted. - long filters = count(root, "Filter"); - long optionalFields = count(root, "OptionalFields"); - if (count(root, "Id") != 1 || count(root, "IsEnabled") != 1 - || count(root, "IncludedObjectVersions") != 1 || count(root, "Schedule") != 1 - || count(root, "Destination") != 1 || filters > 1 || optionalFields > 1 + long filters = root.count("Filter"); + long optionalFields = root.count("OptionalFields"); + if (root.count("Id") != 1 || root.count("IsEnabled") != 1 + || root.count("IncludedObjectVersions") != 1 || root.count("Schedule") != 1 + || root.count("Destination") != 1 || filters > 1 || optionalFields > 1 || root.children().size() != 5 + filters + optionalFields) { throw malformed(); } @@ -169,8 +169,4 @@ record S3InventoryConfiguration(String id, String innerXml) { } return out.build(); } - - private static long count(XmlElement parent, String childName) { - return parent.children().stream().filter(child -> childName.equals(child.name())).count(); - } }