From 0d2e336c08f413b65eb66c3f70866c6999c906fb Mon Sep 17 00:00:00 2001 From: Luca Burgazzoli Date: Mon, 11 Jan 2021 12:45:14 +0100 Subject: [PATCH] Catalog json keys should be sorted #576 --- .../camel/k/catalog/model/Artifact.java | 34 ++++++++++++- .../camel/k/catalog/model/CamelArtifact.java | 34 +++++++------ .../k/catalog/model/CamelCapability.java | 10 ++-- .../camel/k/catalog/model/CamelLoader.java | 22 ++++---- .../camel/k/catalog/model/CamelScheme.java | 17 +++++-- .../k/catalog/model/CamelScopedArtifact.java | 14 +++--- .../camel/k/catalog/model/MavenArtifact.java | 50 ------------------- .../camel/k/catalog/model/k8s/ObjectMeta.java | 7 +-- .../model/k8s/crd/CamelCatalogSpec.java | 19 +++---- .../k/catalog/model/k8s/crd/RuntimeSpec.java | 24 +++++---- .../k/tooling/maven/GenerateCatalogMojo.java | 1 - .../maven/processors/CatalogProcessor3x.java | 18 +++++-- 12 files changed, 131 insertions(+), 119 deletions(-) delete mode 100644 support/camel-k-catalog-model/src/main/java/org/apache/camel/k/catalog/model/MavenArtifact.java diff --git a/support/camel-k-catalog-model/src/main/java/org/apache/camel/k/catalog/model/Artifact.java b/support/camel-k-catalog-model/src/main/java/org/apache/camel/k/catalog/model/Artifact.java index 334acd9f2..8f3dc7223 100644 --- a/support/camel-k-catalog-model/src/main/java/org/apache/camel/k/catalog/model/Artifact.java +++ b/support/camel-k-catalog-model/src/main/java/org/apache/camel/k/catalog/model/Artifact.java @@ -16,10 +16,42 @@ */ package org.apache.camel.k.catalog.model; +import java.util.Comparator; import java.util.Optional; -public interface Artifact { +import com.fasterxml.jackson.annotation.JsonPropertyOrder; + +@JsonPropertyOrder({"groupId", "artifactId", "version"}) +public interface Artifact extends Comparable { String getGroupId();; String getArtifactId(); Optional getVersion(); + + @Override + default int compareTo(Artifact o) { + return Comparator + .comparing(Artifact::getGroupId) + .thenComparing(Artifact::getArtifactId) + .thenComparing(Artifact::getVersion, Comparator.comparing(c -> c.orElse(""))) + .compare(this, o); + } + + static Artifact from(String groupId, String artifactId) { + return new Artifact() { + @Override + public String getGroupId() { + return groupId; + } + + @Override + public String getArtifactId() { + return artifactId; + } + + @Override + public Optional getVersion() { + return Optional.empty(); + } + }; + } } diff --git a/support/camel-k-catalog-model/src/main/java/org/apache/camel/k/catalog/model/CamelArtifact.java b/support/camel-k-catalog-model/src/main/java/org/apache/camel/k/catalog/model/CamelArtifact.java index 7cce93284..40928d39d 100644 --- a/support/camel-k-catalog-model/src/main/java/org/apache/camel/k/catalog/model/CamelArtifact.java +++ b/support/camel-k-catalog-model/src/main/java/org/apache/camel/k/catalog/model/CamelArtifact.java @@ -17,7 +17,7 @@ package org.apache.camel.k.catalog.model; import java.util.Collections; -import java.util.Set; +import java.util.SortedSet; import com.fasterxml.jackson.annotation.JsonPropertyOrder; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; @@ -30,38 +30,44 @@ public interface CamelArtifact extends Artifact { @Value.Auxiliary @Value.Default - default Set getSchemes() { - return Collections.emptySet(); + @Value.NaturalOrder + default SortedSet getSchemes() { + return Collections.emptySortedSet(); } @Value.Auxiliary @Value.Default - default Set getLanguages() { - return Collections.emptySet(); + @Value.NaturalOrder + default SortedSet getLanguages() { + return Collections.emptySortedSet(); } @Value.Auxiliary @Value.Default - default Set getDataformats() { - return Collections.emptySet(); + @Value.NaturalOrder + default SortedSet getDataformats() { + return Collections.emptySortedSet(); } @Value.Auxiliary @Value.Default - default Set getDependencies() { - return Collections.emptySet(); + @Value.NaturalOrder + default SortedSet getDependencies() { + return Collections.emptySortedSet(); } @Value.Auxiliary @Value.Default - default Set getExclusions() { - return Collections.emptySet(); + @Value.NaturalOrder + default SortedSet getExclusions() { + return Collections.emptySortedSet(); } @Value.Auxiliary @Value.Default - default Set getJavaTypes() { - return Collections.emptySet(); + @Value.NaturalOrder + default SortedSet getJavaTypes() { + return Collections.emptySortedSet(); } static Builder from(String groupId, String artifactId) { @@ -70,7 +76,7 @@ static Builder from(String groupId, String artifactId) { class Builder extends ImmutableCamelArtifact.Builder { public Builder addDependency(String groupId, String artifactId) { - return super.addDependencies(MavenArtifact.from(groupId, artifactId)); + return super.addDependencies(Artifact.from(groupId, artifactId)); } } } diff --git a/support/camel-k-catalog-model/src/main/java/org/apache/camel/k/catalog/model/CamelCapability.java b/support/camel-k-catalog-model/src/main/java/org/apache/camel/k/catalog/model/CamelCapability.java index ec18dbebd..0b6c6400c 100644 --- a/support/camel-k-catalog-model/src/main/java/org/apache/camel/k/catalog/model/CamelCapability.java +++ b/support/camel-k-catalog-model/src/main/java/org/apache/camel/k/catalog/model/CamelCapability.java @@ -17,7 +17,7 @@ package org.apache.camel.k.catalog.model; import java.util.Collections; -import java.util.Set; +import java.util.SortedSet; import com.fasterxml.jackson.annotation.JsonPropertyOrder; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; @@ -30,8 +30,9 @@ public interface CamelCapability { @Value.Auxiliary @Value.Default - default Set getDependencies() { - return Collections.emptySet(); + @Value.NaturalOrder + default SortedSet getDependencies() { + return Collections.emptySortedSet(); } static CamelCapability forArtifact(String groupId, String artifactId) { @@ -40,8 +41,7 @@ static CamelCapability forArtifact(String groupId, String artifactId) { class Builder extends ImmutableCamelCapability.Builder { public Builder addDependency(String groupId, String artifactId) { - addDependencies(MavenArtifact.from(groupId, artifactId)); - return this; + return super.addDependencies(Artifact.from(groupId, artifactId)); } } } diff --git a/support/camel-k-catalog-model/src/main/java/org/apache/camel/k/catalog/model/CamelLoader.java b/support/camel-k-catalog-model/src/main/java/org/apache/camel/k/catalog/model/CamelLoader.java index 2912508a3..83a56f5e0 100644 --- a/support/camel-k-catalog-model/src/main/java/org/apache/camel/k/catalog/model/CamelLoader.java +++ b/support/camel-k-catalog-model/src/main/java/org/apache/camel/k/catalog/model/CamelLoader.java @@ -17,8 +17,8 @@ package org.apache.camel.k.catalog.model; import java.util.Collections; -import java.util.Map; -import java.util.Set; +import java.util.SortedMap; +import java.util.SortedSet; import com.fasterxml.jackson.annotation.JsonPropertyOrder; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; @@ -31,20 +31,23 @@ public interface CamelLoader extends Artifact { @Value.Auxiliary @Value.Default - default Set getLanguages() { - return Collections.emptySet(); + @Value.NaturalOrder + default SortedSet getLanguages() { + return Collections.emptySortedSet(); } @Value.Auxiliary @Value.Default - default Set getDependencies() { - return Collections.emptySet(); + @Value.NaturalOrder + default SortedSet getDependencies() { + return Collections.emptySortedSet(); } @Value.Auxiliary @Value.Default - default Map getMetadata() { - return Collections.emptyMap(); + @Value.NaturalOrder + default SortedMap getMetadata() { + return Collections.emptySortedMap(); } static Builder fromArtifact(String groupId, String artifactId) { @@ -53,8 +56,7 @@ static Builder fromArtifact(String groupId, String artifactId) { class Builder extends ImmutableCamelLoader.Builder { public Builder addDependency(String groupId, String artifactId) { - addDependencies(MavenArtifact.from(groupId, artifactId)); - return this; + return super.addDependencies(Artifact.from(groupId, artifactId)); } } } diff --git a/support/camel-k-catalog-model/src/main/java/org/apache/camel/k/catalog/model/CamelScheme.java b/support/camel-k-catalog-model/src/main/java/org/apache/camel/k/catalog/model/CamelScheme.java index fea0a292a..14b68a74e 100644 --- a/support/camel-k-catalog-model/src/main/java/org/apache/camel/k/catalog/model/CamelScheme.java +++ b/support/camel-k-catalog-model/src/main/java/org/apache/camel/k/catalog/model/CamelScheme.java @@ -17,15 +17,16 @@ package org.apache.camel.k.catalog.model; import java.util.Collections; +import java.util.Comparator; import java.util.Optional; -import java.util.Set; +import java.util.SortedSet; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import org.immutables.value.Value; @Value.Immutable @JsonDeserialize(builder = CamelScheme.Builder.class) -public interface CamelScheme { +public interface CamelScheme extends Comparable { String getId(); @Value.Auxiliary @@ -42,8 +43,9 @@ default boolean passive() { @Value.Auxiliary @Value.Default - default Set getRequiredCapabilities() { - return Collections.emptySet(); + @Value.NaturalOrder + default SortedSet getRequiredCapabilities() { + return Collections.emptySortedSet(); } @Value.Auxiliary @@ -52,6 +54,13 @@ default Set getRequiredCapabilities() { @Value.Auxiliary Optional getConsumer(); + @Override + default int compareTo(CamelScheme o) { + return Comparator + .comparing(CamelScheme::getId) + .compare(this, o); + } + class Builder extends ImmutableCamelScheme.Builder { } } diff --git a/support/camel-k-catalog-model/src/main/java/org/apache/camel/k/catalog/model/CamelScopedArtifact.java b/support/camel-k-catalog-model/src/main/java/org/apache/camel/k/catalog/model/CamelScopedArtifact.java index a72429bc0..667dce04a 100644 --- a/support/camel-k-catalog-model/src/main/java/org/apache/camel/k/catalog/model/CamelScopedArtifact.java +++ b/support/camel-k-catalog-model/src/main/java/org/apache/camel/k/catalog/model/CamelScopedArtifact.java @@ -17,7 +17,7 @@ package org.apache.camel.k.catalog.model; import java.util.Collections; -import java.util.Set; +import java.util.SortedSet; import com.fasterxml.jackson.annotation.JsonPropertyOrder; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; @@ -30,19 +30,21 @@ public interface CamelScopedArtifact { @Value.Auxiliary @Value.Default - default Set getDependencies() { - return Collections.emptySet(); + @Value.NaturalOrder + default SortedSet getDependencies() { + return Collections.emptySortedSet(); } @Value.Auxiliary @Value.Default - default Set getRequiredCapabilities() { - return Collections.emptySet(); + @Value.NaturalOrder + default SortedSet getRequiredCapabilities() { + return Collections.emptySortedSet(); } class Builder extends ImmutableCamelScopedArtifact.Builder { public Builder addDependency(String groupId, String artifactId) { - return super.addDependencies(MavenArtifact.from(groupId, artifactId)); + return super.addDependencies(Artifact.from(groupId, artifactId)); } } } diff --git a/support/camel-k-catalog-model/src/main/java/org/apache/camel/k/catalog/model/MavenArtifact.java b/support/camel-k-catalog-model/src/main/java/org/apache/camel/k/catalog/model/MavenArtifact.java deleted file mode 100644 index d75faf17e..000000000 --- a/support/camel-k-catalog-model/src/main/java/org/apache/camel/k/catalog/model/MavenArtifact.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.camel.k.catalog.model; - -import java.util.Optional; - -import com.fasterxml.jackson.annotation.JsonPropertyOrder; -import com.fasterxml.jackson.databind.annotation.JsonDeserialize; -import org.immutables.value.Value; - -@Value.Immutable -@JsonDeserialize(builder = MavenArtifact.Builder.class) -@JsonPropertyOrder({"groupId", "artifactId", "version"}) -public interface MavenArtifact extends Artifact { - static MavenArtifact from(String groupId, String artifactId) { - return new MavenArtifact() { - @Override - public String getGroupId() { - return groupId; - } - - @Override - public String getArtifactId() { - return artifactId; - } - - @Override - public Optional getVersion() { - return Optional.empty(); - } - }; - } - - class Builder extends ImmutableMavenArtifact.Builder { - } -} diff --git a/support/camel-k-catalog-model/src/main/java/org/apache/camel/k/catalog/model/k8s/ObjectMeta.java b/support/camel-k-catalog-model/src/main/java/org/apache/camel/k/catalog/model/k8s/ObjectMeta.java index 60d7aff02..faa66f919 100644 --- a/support/camel-k-catalog-model/src/main/java/org/apache/camel/k/catalog/model/k8s/ObjectMeta.java +++ b/support/camel-k-catalog-model/src/main/java/org/apache/camel/k/catalog/model/k8s/ObjectMeta.java @@ -17,7 +17,7 @@ package org.apache.camel.k.catalog.model.k8s; import java.util.Collections; -import java.util.Map; +import java.util.SortedMap; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import org.immutables.value.Value; @@ -28,8 +28,9 @@ public interface ObjectMeta { String getName(); @Value.Default - default Map getLabels() { - return Collections.emptyMap(); + @Value.NaturalOrder + default SortedMap getLabels() { + return Collections.emptySortedMap(); } class Builder extends ImmutableObjectMeta.Builder { diff --git a/support/camel-k-catalog-model/src/main/java/org/apache/camel/k/catalog/model/k8s/crd/CamelCatalogSpec.java b/support/camel-k-catalog-model/src/main/java/org/apache/camel/k/catalog/model/k8s/crd/CamelCatalogSpec.java index 77360bbfe..d3b9cecdc 100644 --- a/support/camel-k-catalog-model/src/main/java/org/apache/camel/k/catalog/model/k8s/crd/CamelCatalogSpec.java +++ b/support/camel-k-catalog-model/src/main/java/org/apache/camel/k/catalog/model/k8s/crd/CamelCatalogSpec.java @@ -17,7 +17,7 @@ package org.apache.camel.k.catalog.model.k8s.crd; import java.util.Collections; -import java.util.Map; +import java.util.SortedMap; import com.fasterxml.jackson.annotation.JsonPropertyOrder; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; @@ -33,23 +33,24 @@ public interface CamelCatalogSpec { RuntimeSpec getRuntime(); @Value.Default - default Map getArtifacts() { - return Collections.emptyMap(); + @Value.NaturalOrder + default SortedMap getArtifacts() { + return Collections.emptySortedMap(); } @Value.Default - default Map getLoaders() { - return Collections.emptyMap(); + @Value.NaturalOrder + default SortedMap getLoaders() { + return Collections.emptySortedMap(); } class Builder extends ImmutableCamelCatalogSpec.Builder { public Builder putArtifact(CamelArtifact artifact) { - putArtifact(artifact.getArtifactId(), artifact); - return this; + return putArtifact(artifact.getArtifactId(), artifact); } + public Builder putArtifact(String groupId, String artifactId) { - putArtifact(new CamelArtifact.Builder().groupId(groupId).artifactId(artifactId).build()); - return this; + return putArtifact(new CamelArtifact.Builder().groupId(groupId).artifactId(artifactId).build()); } } } diff --git a/support/camel-k-catalog-model/src/main/java/org/apache/camel/k/catalog/model/k8s/crd/RuntimeSpec.java b/support/camel-k-catalog-model/src/main/java/org/apache/camel/k/catalog/model/k8s/crd/RuntimeSpec.java index a6b2d5aff..730dc95a9 100644 --- a/support/camel-k-catalog-model/src/main/java/org/apache/camel/k/catalog/model/k8s/crd/RuntimeSpec.java +++ b/support/camel-k-catalog-model/src/main/java/org/apache/camel/k/catalog/model/k8s/crd/RuntimeSpec.java @@ -17,13 +17,13 @@ package org.apache.camel.k.catalog.model.k8s.crd; import java.util.Collections; -import java.util.Map; -import java.util.Set; +import java.util.SortedMap; +import java.util.SortedSet; import com.fasterxml.jackson.annotation.JsonPropertyOrder; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import org.apache.camel.k.catalog.model.Artifact; import org.apache.camel.k.catalog.model.CamelCapability; -import org.apache.camel.k.catalog.model.MavenArtifact; import org.immutables.value.Value; @Value.Immutable @@ -36,24 +36,26 @@ public interface RuntimeSpec { String getApplicationClass(); @Value.Default - default Map getMetadata() { - return Collections.emptyMap(); + @Value.NaturalOrder + default SortedMap getMetadata() { + return Collections.emptySortedMap(); } @Value.Default - default Set getDependencies() { - return Collections.emptySet(); + @Value.NaturalOrder + default SortedSet getDependencies() { + return Collections.emptySortedSet(); } @Value.Default - default Map getCapabilities() { - return Collections.emptyMap(); + @Value.NaturalOrder + default SortedMap getCapabilities() { + return Collections.emptySortedMap(); } class Builder extends ImmutableRuntimeSpec.Builder { public Builder addDependency(String groupId, String artifactId) { - addDependencies(MavenArtifact.from(groupId, artifactId)); - return this; + return super.addDependencies(Artifact.from(groupId, artifactId)); } } } diff --git a/support/camel-k-maven-plugin/src/main/java/org/apache/camel/k/tooling/maven/GenerateCatalogMojo.java b/support/camel-k-maven-plugin/src/main/java/org/apache/camel/k/tooling/maven/GenerateCatalogMojo.java index 9590e379a..0279087a0 100644 --- a/support/camel-k-maven-plugin/src/main/java/org/apache/camel/k/tooling/maven/GenerateCatalogMojo.java +++ b/support/camel-k-maven-plugin/src/main/java/org/apache/camel/k/tooling/maven/GenerateCatalogMojo.java @@ -148,7 +148,6 @@ public void execute() throws MojoExecutionException, MojoFailureException { CamelCatalogSpec.Builder catalogSpec = new CamelCatalogSpec.Builder() .runtime(runtimeSpec.build()); - StreamSupport.stream(ServiceLoader.load(CatalogProcessor.class).spliterator(), false) .sorted(Comparator.comparingInt(CatalogProcessor::getOrder)) .filter(p -> p.accepts(catalog)) diff --git a/support/camel-k-maven-plugin/src/main/java/org/apache/camel/k/tooling/maven/processors/CatalogProcessor3x.java b/support/camel-k-maven-plugin/src/main/java/org/apache/camel/k/tooling/maven/processors/CatalogProcessor3x.java index 2c7bd3728..d4974e423 100644 --- a/support/camel-k-maven-plugin/src/main/java/org/apache/camel/k/tooling/maven/processors/CatalogProcessor3x.java +++ b/support/camel-k-maven-plugin/src/main/java/org/apache/camel/k/tooling/maven/processors/CatalogProcessor3x.java @@ -17,10 +17,12 @@ package org.apache.camel.k.tooling.maven.processors; import java.util.Arrays; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.Set; +import java.util.TreeMap; +import java.util.TreeSet; import com.vdurmont.semver4j.Semver; import org.apache.camel.catalog.CamelCatalog; @@ -93,7 +95,7 @@ public boolean accepts(CamelCatalog catalog) { @Override public void process(MavenProject project, CamelCatalog catalog, CamelCatalogSpec.Builder specBuilder) { - Map artifacts = new HashMap<>(); + Map artifacts = new TreeMap<>(); processComponents(catalog, artifacts); processLanguages(catalog, artifacts); @@ -221,7 +223,9 @@ private static void processLoaders(CamelCatalogSpec.Builder specBuilder) { } private static void processComponents(CamelCatalog catalog, Map artifacts) { - for (String name : catalog.findComponentNames()) { + final Set elements = new TreeSet<>(catalog.findComponentNames()); + + for (String name : elements) { String json = catalog.componentJSonSchema(name); CatalogComponentDefinition definition = CatalogSupport.unmarshallComponent(json); @@ -244,7 +248,9 @@ private static void processComponents(CamelCatalog catalog, Map artifacts) { - for (String name : catalog.findLanguageNames()) { + final Set elements = new TreeSet<>(catalog.findLanguageNames()); + + for (String name : elements) { String json = catalog.languageJSonSchema(name); CatalogLanguageDefinition definition = CatalogSupport.unmarshallLanguage(json); @@ -259,7 +265,9 @@ private static void processLanguages(CamelCatalog catalog, Map artifacts) { - for (String name : catalog.findDataFormatNames()) { + final Set elements = new TreeSet<>(catalog.findDataFormatNames()); + + for (String name : elements) { String json = catalog.dataFormatJSonSchema(name); CatalogDataFormatDefinition definition = CatalogSupport.unmarshallDataFormat(json);