You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2021/07/12 16:05:55 UTC

[sling-whiteboard] 02/02: SLING-10551 - providers ordering

This is an automated email from the ASF dual-hosted git repository.

bdelacretaz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git

commit 37d580a2c2c34652cca7f2a3e17c299739218d7d
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Mon Jul 12 16:32:57 2021 +0200

    SLING-10551 - providers ordering
---
 .../aggregator/impl/DefaultSchemaAggregator.java    | 21 ++++++++++++++-------
 .../impl/DefaultSchemaAggregatorTest.java           | 13 +++++++++++++
 .../src/test/resources/several-providers-output.txt | 20 ++++++++++----------
 3 files changed, 37 insertions(+), 17 deletions(-)

diff --git a/sling-org-apache-sling-graphql-schema/src/main/java/org/apache/sling/graphql/schema/aggregator/impl/DefaultSchemaAggregator.java b/sling-org-apache-sling-graphql-schema/src/main/java/org/apache/sling/graphql/schema/aggregator/impl/DefaultSchemaAggregator.java
index 4add860..5f5ef7c 100644
--- a/sling-org-apache-sling-graphql-schema/src/main/java/org/apache/sling/graphql/schema/aggregator/impl/DefaultSchemaAggregator.java
+++ b/sling-org-apache-sling-graphql-schema/src/main/java/org/apache/sling/graphql/schema/aggregator/impl/DefaultSchemaAggregator.java
@@ -24,11 +24,10 @@ import java.io.Writer;
 import java.util.Map;
 import java.util.Optional;
 import java.util.Set;
-import java.util.SortedSet;
-import java.util.TreeSet;
 import java.util.regex.Pattern;
 import java.util.Arrays;
 import java.util.HashSet;
+import java.util.LinkedHashSet;
 
 import org.apache.commons.io.IOUtils;
 import org.apache.sling.graphql.schema.aggregator.api.SchemaAggregator;
@@ -99,7 +98,7 @@ public class DefaultSchemaAggregator implements SchemaAggregator {
             log.debug("Aggregating schemas, request={}, providers={}", Arrays.asList(providerNamesOrRegexp), providers.keySet());
         }
         final Set<String> missing = new HashSet<>();
-        final SortedSet<Partial> selected = selectProviders(providers, missing, providerNamesOrRegexp);
+        final Set<Partial> selected = selectProviders(providers, missing, providerNamesOrRegexp);
 
         if(!missing.isEmpty()) {
             log.debug("Requested providers {} not found in {}", missing, providers.keySet());
@@ -112,17 +111,25 @@ public class DefaultSchemaAggregator implements SchemaAggregator {
         copySection(selected, S_MUTATION, true, target);
         copySection(selected, S_TYPES, false, target);
 
-        target.write(String.format("%n# End of Schema aggregated by %s", getClass().getSimpleName()));
+        final StringBuilder partialNames = new StringBuilder();
+        selected.forEach(p -> {
+            if(partialNames.length() > 0) {
+                partialNames.append(",");
+            }
+            partialNames.append(p.getName());
+        });
+        target.write(String.format("%n# End of Schema aggregated from [%s] by %s", partialNames, getClass().getSimpleName()));
     }
 
-    SortedSet<Partial> selectProviders(Map<String, Partial> providers, Set<String> missing, String ... providerNamesOrRegexp) {
-        final SortedSet<Partial> result= new TreeSet<>();
+    Set<Partial> selectProviders(Map<String, Partial> providers, Set<String> missing, String ... providerNamesOrRegexp) {
+        final Set<Partial> result= new LinkedHashSet<>();
         for(String str : providerNamesOrRegexp) {
             final Pattern p = toRegexp(str);
             if(p != null) {
                 log.debug("Selecting providers matching {}", p);
                 providers.entrySet().stream()
                     .filter(e -> p.matcher(e.getKey()).matches())
+                    .sorted((e, other) -> e.getValue().getName().compareTo(other.getValue().getName()))
                     .forEach(e -> addWithRequirements(providers, result, missing, e.getValue(), 0))
                 ;
             } else {
@@ -138,7 +145,7 @@ public class DefaultSchemaAggregator implements SchemaAggregator {
         return result;
     }
 
-    private void addWithRequirements(Map<String, Partial> providers, SortedSet<Partial> addTo, Set<String> missing, Partial p, int recursionLevel) {
+    private void addWithRequirements(Map<String, Partial> providers, Set<Partial> addTo, Set<String> missing, Partial p, int recursionLevel) {
 
         // simplistic cycle detection
         if(recursionLevel > MAX_REQUIREMENTS_RECURSION_LEVEL) {
diff --git a/sling-org-apache-sling-graphql-schema/src/test/java/org/apache/sling/graphql/schema/aggregator/impl/DefaultSchemaAggregatorTest.java b/sling-org-apache-sling-graphql-schema/src/test/java/org/apache/sling/graphql/schema/aggregator/impl/DefaultSchemaAggregatorTest.java
index 0c9bef8..498b8c3 100644
--- a/sling-org-apache-sling-graphql-schema/src/test/java/org/apache/sling/graphql/schema/aggregator/impl/DefaultSchemaAggregatorTest.java
+++ b/sling-org-apache-sling-graphql-schema/src/test/java/org/apache/sling/graphql/schema/aggregator/impl/DefaultSchemaAggregatorTest.java
@@ -151,4 +151,17 @@ public class DefaultSchemaAggregatorTest {
             assertTrue(String.format("Expecting message to contain %s: %s",  s, rex.getMessage()), rex.getMessage().contains(s));
         }));
     }
+
+    @Test
+    public void providersOrdering() throws Exception {
+        final StringWriter target = new StringWriter();
+        tracker.addingBundle(U.mockProviderBundle("ordering", 1, "Aprov.txt", "Cprov.txt", "Z_test.txt", "A_test.txt", "Zprov.txt", "Z_test.txt", "Bprov.txt", "C_test.txt"), null);
+        dsa.aggregate(target, "Aprov", "Zprov", "/[A-Z]_test/", "A_test", "Cprov");
+        final String sdl = target.toString();
+
+        // The order of named partials is kept, regexp selected ones are ordered by name
+        // And A_test has already been used so it's not used again when called explicitly after regexp
+        final String expected = "End of Schema aggregated from [Aprov,Zprov,A_test,C_test,Z_test,Cprov] by DefaultSchemaAggregator";
+        assertTrue(String.format("Expecting schema to contain [%s]: %s", expected, sdl), sdl.contains(expected));
+   }
 }
\ No newline at end of file
diff --git a/sling-org-apache-sling-graphql-schema/src/test/resources/several-providers-output.txt b/sling-org-apache-sling-graphql-schema/src/test/resources/several-providers-output.txt
index b7ae536..0ecd6a8 100644
--- a/sling-org-apache-sling-graphql-schema/src/test/resources/several-providers-output.txt
+++ b/sling-org-apache-sling-graphql-schema/src/test/resources/several-providers-output.txt
@@ -1,16 +1,12 @@
 # Schema aggregated by DefaultSchemaAggregator
 
-# DefaultSchemaAggregator.source=2.z
-
 # DefaultSchemaAggregator.source=B1a
 
 # DefaultSchemaAggregator.source=B2
 
-type QUERY {
-
 # DefaultSchemaAggregator.source=2.z
-Fake query for 2.z.w
 
+type QUERY {
 
 # DefaultSchemaAggregator.source=B1a
 Fake query for B1a.txt
@@ -20,22 +16,26 @@ Fake query for B1a.txt
 Fake query for B2.xy
 
 
+# DefaultSchemaAggregator.source=2.z
+Fake query for 2.z.w
+
+
 }
 
 type MUTATION {
 
-# DefaultSchemaAggregator.source=2.z
-
 # DefaultSchemaAggregator.source=B1a
 
 # DefaultSchemaAggregator.source=B2
 
-}
-
 # DefaultSchemaAggregator.source=2.z
 
+}
+
 # DefaultSchemaAggregator.source=B1a
 
 # DefaultSchemaAggregator.source=B2
 
-# End of Schema aggregated by DefaultSchemaAggregator
\ No newline at end of file
+# DefaultSchemaAggregator.source=2.z
+
+# End of Schema aggregated from [B1a,B2,2.z] by DefaultSchemaAggregator
\ No newline at end of file