damondouglas commented on code in PR #24271:
URL: https://github.com/apache/beam/pull/24271#discussion_r1028571793


##########
sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/utils/JsonUtils.java:
##########
@@ -73,6 +78,87 @@ public String apply(Row input) {
     };
   }
 
+  public static Schema beamSchemaFromJsonSchema(String jsonSchemaStr) {
+    org.everit.json.schema.ObjectSchema jsonSchema = 
jsonSchemaFromString(jsonSchemaStr);
+    return beamSchemaFromJsonSchema(jsonSchema);
+  }
+
+  private static Schema 
beamSchemaFromJsonSchema(org.everit.json.schema.ObjectSchema jsonSchema) {
+    Schema.Builder beamSchemaBuilder = Schema.builder();
+    for (String propertyName : jsonSchema.getPropertySchemas().keySet()) {
+      org.everit.json.schema.Schema propertySchema =
+          jsonSchema.getPropertySchemas().get(propertyName);
+      if (propertySchema == null) {
+        throw new IllegalArgumentException("Unable to parse schema " + 
jsonSchema.toString());
+      }
+      if (propertySchema instanceof org.everit.json.schema.ObjectSchema) {

Review Comment:
   Is it possible to add the following support methods?  I advocate that 
removing the embedded checks in this method allows these to be independently 
tested.  First, the following shows how `beamSchemaFromJsonSchema` might look 
with the proposed overloaded support methods.  Then, some of the support 
methods follow.  To avoid the instanceof checks, I tried to find in the 
libraries 
[Javadoc](https://erosb.github.io/everit-json-schema/javadoc/1.12.2/), whether 
the base 
[Schema](https://erosb.github.io/everit-json-schema/javadoc/1.12.2/org/everit/json/schema/Schema.html)
 had a `getType()` getter but it seems not.  Perhaps we could have a 
`Map<Class<T extends org.everit.json.schema.Schema>, Function<T extends 
org.everit.json.schema.Schema, Schema.Field>>` that maps a particular json 
Schema class to the Java function that converts to the Schema.Field.
   
   ```
   Schema.Builder beamSchemaBuilder = Schema.builder()
   for (String propertyName : jsonSchema.getPropertySchemas().keySet()) {
       org.everit.json.schema.Schema propertySchema = 
jsonSchema.getPropertySchemas().get(propertyName);
       if (propertySchema == null) {
           throw new IllegalArgumentException("Unable to parse schema " + 
jsonSchema.toString());
       }
       
       Schema.Field field = beamFieldFromJsonField(propertySchema);
       builder = builder.addField(field);
   }
   
   return builder.build();
   ```
   
   ```java
   static Schema.Field beamFieldFromJsonField(org.everit.json.schema.Schema 
propertySchema) {
       if (propertySchema instanceof org.everit.json.schema.ObjectSchema) {
           return beamFieldFromJsonField((org.everit.json.schema.ObjectSchema) 
propertySchema);
       }
       if (propertySchema instanceof org.everit.json.schema.ArraySchema) {
           return beamFieldFromJsonField((org.everit.json.schema.ObjectSchema) 
propertySchema);
       }
       // etc etc to all the various types.
   }
   
   static Schema.Field 
beamFieldFromJsonField(org.everit.json.schema.ObjectSchema objectSchema) {
      // do some magic
   }
   
   static Schema.Field 
beamFieldFromJsonField(org.everit.json.schema.ArraySchema arraySchema) {
      // do some more magic
   }
   
   static Schema.Field 
beamFieldFromJsonField(org.everit.json.schema.BooleanField booleanSchema) {
     // do even some more magical magic
   }
   ```



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