amazon-ion / ion-cli

Apache License 2.0
31 stars 15 forks source link

Removes generating a separate class/struct for nested sequence and scalar type #111

Closed desaikd closed 2 months ago

desaikd commented 3 months ago

Issue #110:

Description of changes:

This PR works on generating nested sequence and scalar types in the same class/struct.

What is the change?

Previously, code generator would generate nested sequence or scalar types as a separate nested class or struct in a nested module. But this new change does not create a new class rather maps these nested sequence or scalar type in the same class/struct that uses it.

e.g. For a schema as below:

type::{
   name: struct_type,
   fields:  {
      foo: { type: list, element: int }, // nested sequence type
      bar: { type: string } // nested scalar type
   }
}

Previously generated code:

class StructType {
  private NestedType1 foo;
  private NestedType2 bar; 
   ...

   class NestedType1 {
      private ArrayList<Integer> value;
      ...
   }

   class NestedType1 {
      private String value;
      ...
   }
}

Code generated with this PR changes:

class StructType {
  private ArrayList<Integer> foo;
  private String bar; 
   ...
}

(Note: For example purpose I have only used java code but same is the case with Rust. Also, top level sequence/scalar top would still generate a separate class/struct. For more information on complete newly generated code see here)

Note for reviewer:

This PR also adds a new data model that can be used by code generator to render templates. The current approach can not generate nested sequence types like {type: list, element: {type: sexp, element: int} }. This will be resolved with the new data model as well as the new data model makes it easier to store all the information used by templates in a single place. This Pr just introduces the model but doesn't use it in implementation yet. The change for usage will be added in a separate PR.

Generated code:

Generated code for Java can be found here. Generated code for Rust can be found here. (You can find corresponding ISL files and Ion files in code-gen-projects directory)

List of changes:

Test:

Added new test schema and input files for this change.


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

desaikd commented 3 months ago

As per offline discussion, I am removing model.rs from this PR and will be added as a separate PR.