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.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-09-18 20:17:44 -05:00
1 parent df3ce4a198
commit 86f3084ff0
3 files changed
+15 -19

No files matched your search

@@ -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();
}
}
/**
@@ -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<XmlElement> 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();
}
}
@@ -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();
}
}