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

gyfora pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/flink-kubernetes-operator.git

commit 0f829d9b15807f3a39b708c5c48fba0b9917827c
Author: Gyula Fora <[email protected]>
AuthorDate: Thu Jul 13 16:49:42 2023 +0200

    [hotfix] Simplify diffbuilder array checks and add test
---
 .../operator/reconciler/diff/DiffBuilder.java      | 121 +++------------------
 .../operator/reconciler/diff/SpecDiffTest.java     |  64 +++++++++++
 2 files changed, 79 insertions(+), 106 deletions(-)

diff --git 
a/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/reconciler/diff/DiffBuilder.java
 
b/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/reconciler/diff/DiffBuilder.java
index cba89389..c34a1c43 100644
--- 
a/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/reconciler/diff/DiffBuilder.java
+++ 
b/flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/reconciler/diff/DiffBuilder.java
@@ -28,6 +28,7 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Objects;
+import java.util.function.BiFunction;
 
 /**
  * Assists in implementing comparison of {@link Diffable} instances.
@@ -53,108 +54,16 @@ public class DiffBuilder<T> implements 
Builder<DiffResult<?>> {
         this.triviallyEqual = before == after || before.equals(after);
     }
 
-    public DiffBuilder<T> append(
+    public <S> DiffBuilder<T> append(
             @NonNull final String fieldName,
-            final boolean[] left,
-            final boolean[] right,
+            S left,
+            S right,
+            BiFunction<S, S, Boolean> equals,
             DiffType type) {
         if (triviallyEqual) {
             return this;
         }
-        if (!Arrays.equals(left, right)) {
-            diffs.add(new Diff<>(fieldName, left, right, type));
-        }
-        return this;
-    }
-
-    public DiffBuilder<T> append(
-            @NonNull final String fieldName, final byte[] left, final byte[] 
right, DiffType type) {
-
-        if (triviallyEqual) {
-            return this;
-        }
-        if (!Arrays.equals(left, right)) {
-            diffs.add(new Diff<>(fieldName, left, right, type));
-        }
-        return this;
-    }
-
-    public DiffBuilder<T> append(
-            @NonNull final String fieldName, final char[] left, final char[] 
right, DiffType type) {
-
-        if (triviallyEqual) {
-            return this;
-        }
-        if (!Arrays.equals(left, right)) {
-            diffs.add(new Diff<>(fieldName, left, right, type));
-        }
-        return this;
-    }
-
-    public DiffBuilder<T> append(
-            @NonNull final String fieldName,
-            final double[] left,
-            final double[] right,
-            DiffType type) {
-
-        if (triviallyEqual) {
-            return this;
-        }
-        if (!Arrays.equals(left, right)) {
-            diffs.add(new Diff<>(fieldName, left, right, type));
-        }
-        return this;
-    }
-
-    public DiffBuilder<T> append(
-            @NonNull final String fieldName,
-            final float[] left,
-            final float[] right,
-            DiffType type) {
-
-        if (triviallyEqual) {
-            return this;
-        }
-        if (!Arrays.equals(left, right)) {
-            diffs.add(new Diff<>(fieldName, left, right, type));
-        }
-        return this;
-    }
-
-    public DiffBuilder<T> append(
-            @NonNull final String fieldName, final int[] left, final int[] 
right, DiffType type) {
-
-        if (triviallyEqual) {
-            return this;
-        }
-        if (!Arrays.equals(left, right)) {
-            diffs.add(new Diff<>(fieldName, left, right, type));
-        }
-        return this;
-    }
-
-    public DiffBuilder<T> append(
-            @NonNull final String fieldName, final long[] left, final long[] 
right, DiffType type) {
-
-        if (triviallyEqual) {
-            return this;
-        }
-        if (!Arrays.equals(left, right)) {
-            diffs.add(new Diff<>(fieldName, left, right, type));
-        }
-        return this;
-    }
-
-    public DiffBuilder<T> append(
-            @NonNull final String fieldName,
-            final short[] left,
-            final short[] right,
-            DiffType type) {
-
-        if (triviallyEqual) {
-            return this;
-        }
-        if (!Arrays.equals(left, right)) {
+        if (!equals.apply(left, right)) {
             diffs.add(new Diff<>(fieldName, left, right, type));
         }
         return this;
@@ -174,31 +83,31 @@ public class DiffBuilder<T> implements 
Builder<DiffResult<?>> {
 
         if (objectToTest.getClass().isArray()) {
             if (objectToTest instanceof boolean[]) {
-                return append(fieldName, (boolean[]) left, (boolean[]) right, 
type);
+                return append(fieldName, (boolean[]) left, (boolean[]) right, 
Arrays::equals, type);
             }
             if (objectToTest instanceof byte[]) {
-                return append(fieldName, (byte[]) left, (byte[]) right, type);
+                return append(fieldName, (byte[]) left, (byte[]) right, 
Arrays::equals, type);
             }
             if (objectToTest instanceof char[]) {
-                return append(fieldName, (char[]) left, (char[]) right, type);
+                return append(fieldName, (char[]) left, (char[]) right, 
Arrays::equals, type);
             }
             if (objectToTest instanceof double[]) {
-                return append(fieldName, (double[]) left, (double[]) right, 
type);
+                return append(fieldName, (double[]) left, (double[]) right, 
Arrays::equals, type);
             }
             if (objectToTest instanceof float[]) {
-                return append(fieldName, (float[]) left, (float[]) right, 
type);
+                return append(fieldName, (float[]) left, (float[]) right, 
Arrays::equals, type);
             }
             if (objectToTest instanceof int[]) {
-                return append(fieldName, (int[]) left, (int[]) right, type);
+                return append(fieldName, (int[]) left, (int[]) right, 
Arrays::equals, type);
             }
             if (objectToTest instanceof long[]) {
-                return append(fieldName, (long[]) left, (long[]) right, type);
+                return append(fieldName, (long[]) left, (long[]) right, 
Arrays::equals, type);
             }
             if (objectToTest instanceof short[]) {
-                return append(fieldName, (short[]) left, (short[]) right, 
type);
+                return append(fieldName, (short[]) left, (short[]) right, 
Arrays::equals, type);
             }
 
-            return append(fieldName, (Object[]) left, (Object[]) right, type);
+            return append(fieldName, (Object[]) left, (Object[]) right, 
Arrays::equals, type);
         }
 
         if (left != null && left.equals(right)) {
diff --git 
a/flink-kubernetes-operator/src/test/java/org/apache/flink/kubernetes/operator/reconciler/diff/SpecDiffTest.java
 
b/flink-kubernetes-operator/src/test/java/org/apache/flink/kubernetes/operator/reconciler/diff/SpecDiffTest.java
index c813aed3..254e6001 100644
--- 
a/flink-kubernetes-operator/src/test/java/org/apache/flink/kubernetes/operator/reconciler/diff/SpecDiffTest.java
+++ 
b/flink-kubernetes-operator/src/test/java/org/apache/flink/kubernetes/operator/reconciler/diff/SpecDiffTest.java
@@ -33,14 +33,17 @@ import 
org.apache.flink.kubernetes.operator.api.utils.SpecUtils;
 import 
org.apache.flink.kubernetes.operator.config.KubernetesOperatorConfigOptions;
 
 import io.fabric8.kubernetes.api.model.HostAlias;
+import lombok.Value;
 import org.junit.jupiter.api.Test;
 
 import java.util.List;
 import java.util.Map;
+import java.util.stream.Collectors;
 
 import static 
org.apache.flink.kubernetes.operator.config.KubernetesOperatorConfigOptions.OPERATOR_RECONCILE_INTERVAL;
 import static 
org.apache.flink.kubernetes.operator.metrics.KubernetesOperatorMetricOptions.SCOPE_NAMING_KUBERNETES_OPERATOR;
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 /** Spec diff test. */
 public class SpecDiffTest {
@@ -251,4 +254,65 @@ public class SpecDiffTest {
                         + "restartNonce : null -> 1]",
                 diff.toString());
     }
+
+    @Test
+    public void testArrayDiffs() {
+        var left =
+                new TestClass(
+                        new boolean[] {true},
+                        new byte[] {0},
+                        new char[] {'a'},
+                        new double[] {0.},
+                        new float[] {0f},
+                        new int[] {0},
+                        new long[] {0L},
+                        new short[] {2},
+                        new Object[] {"a"});
+        var right =
+                new TestClass(
+                        new boolean[] {true},
+                        new byte[] {0},
+                        new char[] {'a'},
+                        new double[] {0.},
+                        new float[] {0f},
+                        new int[] {0},
+                        new long[] {0L},
+                        new short[] {2},
+                        new Object[] {"a"});
+
+        var diff =
+                new ReflectiveDiffBuilder<>(KubernetesDeploymentMode.NATIVE, 
left, right).build();
+        assertTrue(diff.getDiffList().isEmpty());
+
+        right =
+                new TestClass(
+                        new boolean[] {false},
+                        new byte[] {0},
+                        new char[] {'a'},
+                        new double[] {0.},
+                        new float[] {0f},
+                        new int[] {0},
+                        new long[] {0L},
+                        new short[] {2},
+                        new Object[] {"b"});
+        diff = new ReflectiveDiffBuilder<>(KubernetesDeploymentMode.NATIVE, 
left, right).build();
+        assertEquals(2, diff.getNumDiffs());
+        assertEquals(
+                Map.of("f0", DiffType.UPGRADE, "f8", DiffType.UPGRADE),
+                diff.getDiffList().stream()
+                        .collect(Collectors.toMap(Diff::getFieldName, 
Diff::getType)));
+    }
+
+    @Value
+    private static class TestClass {
+        boolean[] f0;
+        byte[] f1;
+        char[] f2;
+        double[] f3;
+        float[] f4;
+        int[] f5;
+        long[] f6;
+        short[] f7;
+        Object[] f8;
+    }
 }

Reply via email to