phet commented on a change in pull request #3421:
URL: https://github.com/apache/gobblin/pull/3421#discussion_r740665535



##########
File path: 
gobblin-runtime/src/main/java/org/apache/gobblin/runtime/spec_serde/JobSpecDeserializer.java
##########
@@ -0,0 +1,83 @@
+/*
+ * 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.gobblin.runtime.spec_serde;
+
+import com.google.gson.JsonDeserializationContext;
+import com.google.gson.JsonDeserializer;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParseException;
+import java.io.IOException;
+import java.io.StringReader;
+import java.lang.reflect.Type;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.Optional;
+import java.util.Properties;
+import org.apache.gobblin.runtime.api.JobSpec;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+public class JobSpecDeserializer implements JsonDeserializer<JobSpec> {
+
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(JobSpecDeserializer.class);
+
+  @Override
+  public JobSpec deserialize(JsonElement json, Type typeOfT, 
JsonDeserializationContext context) {
+    JsonObject jsonObject = json.getAsJsonObject();
+
+    String uri = 
jsonObject.get(JobSpecSerializer.JOB_SPEC_URI_KEY).getAsString();
+    String version = 
jsonObject.get(JobSpecSerializer.JOB_SPEC_VERSION_KEY).getAsString();
+    String description = 
jsonObject.get(JobSpecSerializer.JOB_SPEC_DESCRIPTION_KEY).getAsString();
+    Optional<URI> templateURI;
+    try {
+      templateURI = 
Optional.ofNullable(jsonObject.get(JobSpecSerializer.JOB_SPEC_TEMPLATE_URI_KEY).getAsString()).map(s
 -> {
+        try {
+          return new URI(s);
+        } catch (URISyntaxException e) {
+          LOGGER.warn(String.format("error deserializing '%s' as a URI",
+              
jsonObject.get(JobSpecSerializer.JOB_SPEC_TEMPLATE_URI_KEY).getAsString()), e);
+          throw new RuntimeException(e);
+        }
+      });
+    } catch (RuntimeException e) {
+      templateURI = Optional.empty();
+    }

Review comment:
       first off, I agree it's unsatisfying.  (I welcome any tips toward 
finessing java's checked exceptions!)
   
   the transformation you suggest doesn't protect against passing `null` to the 
`URI` ctor.  that's why I was working (via `map`) *within* the `Optional`.  
still, you did helped spot a NPE bug from the parens closed in the wrong place. 
 we need:
   ```
   Optional.ofNullable(jsonObj.get(...)).map
   ```
   not the potentially explosive:
   ```
   Optional.ofNullable(jsonObj.get(...).getAsString()).map
   ```
   in addition, I'll use `Optional.flatMap` to eliminate the second level of 
exception handling.  I'll push those corrections, and you please tell me if you 
see any other potential improvement.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to