beam-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bchamb...@apache.org
Subject [5/9] incubator-beam git commit: Exclude JsonIgnore PipelineOptions from DisplayData
Date Wed, 11 May 2016 16:31:12 GMT
Exclude JsonIgnore PipelineOptions from DisplayData


Project: http://git-wip-us.apache.org/repos/asf/incubator-beam/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-beam/commit/43085f7e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-beam/tree/43085f7e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-beam/diff/43085f7e

Branch: refs/heads/master
Commit: 43085f7e53e693202ab91768a7f3688fc54f24ee
Parents: 0dab643
Author: Scott Wegner <swegner@google.com>
Authored: Tue May 3 14:10:59 2016 -0700
Committer: bchambers <bchambers@google.com>
Committed: Tue May 10 16:21:27 2016 -0700

----------------------------------------------------------------------
 .../beam/sdk/options/PipelineOptionSpec.java    | 61 +++++---------------
 .../sdk/options/ProxyInvocationHandler.java     | 10 ++++
 .../options/PipelineOptionsReflectorTest.java   | 32 ++++++++++
 .../sdk/options/ProxyInvocationHandlerTest.java |  9 +++
 4 files changed, 65 insertions(+), 47 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-beam/blob/43085f7e/sdks/java/core/src/main/java/org/apache/beam/sdk/options/PipelineOptionSpec.java
----------------------------------------------------------------------
diff --git a/sdks/java/core/src/main/java/org/apache/beam/sdk/options/PipelineOptionSpec.java
b/sdks/java/core/src/main/java/org/apache/beam/sdk/options/PipelineOptionSpec.java
index 71f9d46..9a88f70 100644
--- a/sdks/java/core/src/main/java/org/apache/beam/sdk/options/PipelineOptionSpec.java
+++ b/sdks/java/core/src/main/java/org/apache/beam/sdk/options/PipelineOptionSpec.java
@@ -17,73 +17,40 @@
  */
 package org.apache.beam.sdk.options;
 
-import com.google.common.base.MoreObjects;
-import com.google.common.base.Objects;
+import com.google.auto.value.AutoValue;
+
+import com.fasterxml.jackson.annotation.JsonIgnore;
 
 import java.lang.reflect.Method;
 
 /**
  * For internal use. Specification for an option defined in a {@link PipelineOptions} interface.
  */
-class PipelineOptionSpec {
-  private final Class<? extends PipelineOptions> clazz;
-  private final String name;
-  private final Method getter;
-
+@AutoValue
+abstract class PipelineOptionSpec {
   static PipelineOptionSpec of(Class<? extends PipelineOptions> clazz, String name,
Method getter) {
-    return new PipelineOptionSpec(clazz, name, getter);
-  }
-
-  private PipelineOptionSpec(Class<? extends PipelineOptions> clazz, String name, Method
getter) {
-    this.clazz = clazz;
-    this.name = name;
-    this.getter = getter;
+    return new AutoValue_PipelineOptionSpec(clazz, name, getter);
   }
 
   /**
    * The {@link PipelineOptions} interface which defines this {@link PipelineOptionSpec}.
    */
-  Class<? extends PipelineOptions> getDefiningInterface() {
-    return clazz;
-  }
+  abstract Class<? extends PipelineOptions> getDefiningInterface();
 
   /**
    * Name of the property.
    */
-  String getName() {
-    return name;
-  }
+  abstract String getName();
 
   /**
    * The getter method for this property.
    */
-  Method getGetterMethod() {
-    return getter;
-  }
+  abstract Method getGetterMethod();
 
-  @Override
-  public String toString() {
-    return MoreObjects.toStringHelper(this)
-        .add("definingInterface", getDefiningInterface())
-        .add("name", getName())
-        .add("getterMethod", getGetterMethod())
-        .toString();
-  }
-
-  @Override
-  public int hashCode() {
-    return Objects.hashCode(getDefiningInterface(), getName(), getGetterMethod());
-  }
-
-  @Override
-  public boolean equals(Object obj) {
-    if (!(obj instanceof PipelineOptionSpec)) {
-      return false;
-    }
-
-    PipelineOptionSpec that = (PipelineOptionSpec) obj;
-    return Objects.equal(this.getDefiningInterface(), that.getDefiningInterface())
-        && Objects.equal(this.getName(), that.getName())
-        && Objects.equal(this.getGetterMethod(), that.getGetterMethod());
+  /**
+   * Whether the option should be serialized. Uses the {@link JsonIgnore} annotation.
+   */
+  boolean shouldSerialize() {
+    return !getGetterMethod().isAnnotationPresent(JsonIgnore.class);
   }
 }

http://git-wip-us.apache.org/repos/asf/incubator-beam/blob/43085f7e/sdks/java/core/src/main/java/org/apache/beam/sdk/options/ProxyInvocationHandler.java
----------------------------------------------------------------------
diff --git a/sdks/java/core/src/main/java/org/apache/beam/sdk/options/ProxyInvocationHandler.java
b/sdks/java/core/src/main/java/org/apache/beam/sdk/options/ProxyInvocationHandler.java
index 745549f..159eb5b 100644
--- a/sdks/java/core/src/main/java/org/apache/beam/sdk/options/ProxyInvocationHandler.java
+++ b/sdks/java/core/src/main/java/org/apache/beam/sdk/options/ProxyInvocationHandler.java
@@ -281,6 +281,12 @@ class ProxyInvocationHandler implements InvocationHandler {
       HashSet<PipelineOptionSpec> specs = new HashSet<>(optionsMap.get(option.getKey()));
 
       for (PipelineOptionSpec optionSpec : specs) {
+        if (!optionSpec.shouldSerialize()) {
+          // Options that are excluded for serialization (i.e. those with @JsonIgnore) are
also
+          // excluded from display data. These options are generally not useful for display.
+          continue;
+        }
+
         Class<?> pipelineInterface = optionSpec.getDefiningInterface();
         if (type != null) {
           builder.add(DisplayData.item(option.getKey(), type, value)
@@ -304,6 +310,10 @@ class ProxyInvocationHandler implements InvocationHandler {
           .withNamespace(UnknownPipelineOptions.class));
       } else {
         for (PipelineOptionSpec spec : specs) {
+          if (!spec.shouldSerialize()) {
+            continue;
+          }
+
           Object value = getValueFromJson(jsonOption.getKey(), spec.getGetterMethod());
           DisplayData.Type type = DisplayData.inferType(value);
           if (type != null) {

http://git-wip-us.apache.org/repos/asf/incubator-beam/blob/43085f7e/sdks/java/core/src/test/java/org/apache/beam/sdk/options/PipelineOptionsReflectorTest.java
----------------------------------------------------------------------
diff --git a/sdks/java/core/src/test/java/org/apache/beam/sdk/options/PipelineOptionsReflectorTest.java
b/sdks/java/core/src/test/java/org/apache/beam/sdk/options/PipelineOptionsReflectorTest.java
index 82f0329..8f801c7 100644
--- a/sdks/java/core/src/test/java/org/apache/beam/sdk/options/PipelineOptionsReflectorTest.java
+++ b/sdks/java/core/src/test/java/org/apache/beam/sdk/options/PipelineOptionsReflectorTest.java
@@ -20,12 +20,15 @@ package org.apache.beam.sdk.options;
 
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.allOf;
+import static org.hamcrest.Matchers.equalTo;
 import static org.hamcrest.Matchers.hasItem;
 import static org.hamcrest.Matchers.is;
 import static org.hamcrest.Matchers.isOneOf;
 import static org.hamcrest.Matchers.not;
 
 import com.google.common.collect.ImmutableSet;
+
+import com.fasterxml.jackson.annotation.JsonIgnore;
 import org.hamcrest.FeatureMatcher;
 import org.hamcrest.Matcher;
 import org.hamcrest.Matchers;
@@ -130,6 +133,24 @@ public class PipelineOptionsReflectorTest {
   }
 
   @Test
+  public void testShouldSerialize() {
+    Set<PipelineOptionSpec> properties =
+        PipelineOptionsReflector.getOptionSpecs(JsonIgnoreOptions.class);
+
+    assertThat(properties, hasItem(allOf(hasName("notIgnored"), shouldSerialize())));
+    assertThat(properties, hasItem(allOf(hasName("ignored"), not(shouldSerialize()))));
+  }
+
+  interface JsonIgnoreOptions extends PipelineOptions {
+    String getNotIgnored();
+    void setNotIgnored(String value);
+
+    @JsonIgnore
+    String getIgnored();
+    void setIgnored(String value);
+  }
+
+  @Test
   public void testMultipleInputInterfaces() {
     Set<Class<? extends PipelineOptions>> interfaces =
         ImmutableSet.<Class<? extends PipelineOptions>>of(
@@ -193,4 +214,15 @@ public class PipelineOptionsReflectorTest {
       }
     };
   }
+
+  private static Matcher<PipelineOptionSpec> shouldSerialize() {
+    return new FeatureMatcher<PipelineOptionSpec, Boolean>(equalTo(true),
+        "should serialize", "shouldSerialize") {
+
+      @Override
+      protected Boolean featureValueOf(PipelineOptionSpec actual) {
+        return actual.shouldSerialize();
+      }
+    };
+  }
 }

http://git-wip-us.apache.org/repos/asf/incubator-beam/blob/43085f7e/sdks/java/core/src/test/java/org/apache/beam/sdk/options/ProxyInvocationHandlerTest.java
----------------------------------------------------------------------
diff --git a/sdks/java/core/src/test/java/org/apache/beam/sdk/options/ProxyInvocationHandlerTest.java
b/sdks/java/core/src/test/java/org/apache/beam/sdk/options/ProxyInvocationHandlerTest.java
index b53000d..228a6ba 100644
--- a/sdks/java/core/src/test/java/org/apache/beam/sdk/options/ProxyInvocationHandlerTest.java
+++ b/sdks/java/core/src/test/java/org/apache/beam/sdk/options/ProxyInvocationHandlerTest.java
@@ -951,4 +951,13 @@ public class ProxyInvocationHandlerTest {
     String value = MAPPER.writeValueAsString(options);
     return MAPPER.readValue(value, PipelineOptions.class).as(kls);
   }
+
+  @Test
+  public void testDisplayDataExcludesJsonIgnoreOptions() {
+    IgnoredProperty options = PipelineOptionsFactory.as(IgnoredProperty.class);
+    options.setValue("foobar");
+
+    DisplayData data = DisplayData.from(options);
+    assertThat(data, not(hasDisplayItem(hasKey("value"))));
+  }
 }


Mime
View raw message