[ 
https://issues.apache.org/jira/browse/BEAM-10139?focusedWorklogId=511439&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-511439
 ]

ASF GitHub Bot logged work on BEAM-10139:
-----------------------------------------

                Author: ASF GitHub Bot
            Created on: 13/Nov/20 17:08
            Start Date: 13/Nov/20 17:08
    Worklog Time Spent: 10m 
      Work Description: TheNeuralBit commented on a change in pull request 
#12611:
URL: https://github.com/apache/beam/pull/12611#discussion_r523092073



##########
File path: 
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/spanner/StructUtils.java
##########
@@ -0,0 +1,380 @@
+/*
+ * 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.beam.sdk.io.gcp.spanner;
+
+import static java.util.stream.Collectors.toList;
+
+import com.google.cloud.ByteArray;
+import com.google.cloud.Timestamp;
+import com.google.cloud.spanner.Struct;
+import com.google.cloud.spanner.Type;
+import java.math.BigDecimal;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.StreamSupport;
+import org.apache.beam.sdk.schemas.Schema;
+import org.apache.beam.sdk.values.Row;
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.joda.time.DateTime;
+import org.joda.time.Instant;
+import org.joda.time.ReadableDateTime;
+
+final class StructUtils {
+  public static Row structToBeamRow(Struct struct, Schema schema) {
+    Map<String, Object> structValues =
+        schema.getFields().stream()
+            .collect(
+                HashMap::new,
+                (map, field) -> {
+                  @Nullable Object structValue = getStructValue(struct, field);
+                  if (structValue == null) {
+                    throw new NullPointerException("Null struct value at field 
" + field.getName());
+                  }
+                  map.put(field.getName(), structValue);
+                },
+                Map::putAll);
+    return Row.withSchema(schema).withFieldValues(structValues).build();
+  }
+
+  public static Struct beamRowToStruct(Row row) {
+    Struct.Builder structBuilder = Struct.newBuilder();
+    List<Schema.Field> fields = row.getSchema().getFields();
+    fields.forEach(
+        field -> {
+          String column = field.getName();
+          switch (field.getType().getTypeName()) {
+            case ROW:
+              @Nullable Row subRow = row.getRow(column);
+              if (subRow == null) {
+                throw new NullPointerException(String.format("Null subRow at 
'%s' column", column));
+              }

Review comment:
       Let's make these null checks use 
`org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkNotNull`,
 that will be a bit more concise, and it avoids throwing an NPE 




----------------------------------------------------------------
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.

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


Issue Time Tracking
-------------------

    Worklog Id:     (was: 511439)
    Time Spent: 33h  (was: 32h 50m)

> Add cross-language wrapper for Java's SpannerIO Write
> -----------------------------------------------------
>
>                 Key: BEAM-10139
>                 URL: https://issues.apache.org/jira/browse/BEAM-10139
>             Project: Beam
>          Issue Type: Sub-task
>          Components: cross-language, io-java-gcp, io-py-gcp
>    Affects Versions: Not applicable
>            Reporter: Piotr Szuberski
>            Assignee: Piotr Szuberski
>            Priority: P2
>              Labels: portability
>             Fix For: Not applicable
>
>          Time Spent: 33h
>  Remaining Estimate: 0h
>
> Add cross-language wrapper for Java's SpannerIO Write



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to