Skip to content

Commit

Permalink
Adds changes for optional/required fields in Java code generation (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
desaikd authored Oct 18, 2024
1 parent d710bd8 commit da69354
Show file tree
Hide file tree
Showing 13 changed files with 350 additions and 89 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// simple struct with all valid fields
{
A: "hello",
B: 12,
// C: ("foo" "bar" "baz"), // since `C` is a required field, this is an invalid struct
D: 10e2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// nested struct with some optional fields that are not provided
{
A: "hello",
// B: 12, // since `B` is optional field, this is a valid struct
C: {
D: false,
E: [1, 2, 3]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// simple struct with all valid fields
{
A: "hello",
B: 12,
C: ("foo" "bar" "baz"),
// D: 10e2, // since `D` is optional field, this is a valid struct
}
28 changes: 28 additions & 0 deletions code-gen-projects/rust/code-gen-demo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,28 @@ mod tests {
use ion_rs::ReaderBuilder;
use ion_rs::TextWriterBuilder;
use std::fs;
use std::path::MAIN_SEPARATOR_STR as PATH_SEPARATOR;
use test_generator::test_resources;

include!(concat!(env!("OUT_DIR"), "/ion_generated_code.rs"));

/// Determines if the given file name is in the ROUNDTRIP_TESTS_SKIP_LIST list. This deals with platform
/// path separator differences from '/' separators in the path list.
#[inline]
pub fn skip_list_contains_path(file_name: &str) -> bool {
ROUNDTRIP_TESTS_SKIP_LIST
.iter()
// TODO construct the paths in a not so hacky way
.map(|p| p.replace('/', PATH_SEPARATOR))
.any(|p| p == file_name)
}

pub const ROUNDTRIP_TESTS_SKIP_LIST: &[&str] = &[
"../../input/good/nested_struct/valid_optional_fields.ion",
"../../input/good/struct_with_fields/valid_optional_fields.ion",
"../../input/bad/struct_with_fields/missing_required_fields.ion",
];

#[test]
fn it_works() {
let result = add(2, 2);
Expand All @@ -22,6 +40,10 @@ mod tests {

#[test_resources("../../input/good/struct_with_fields/**/*.ion")]
fn roundtrip_good_test_generated_code_structs_with_fields(file_name: &str) -> SerdeResult<()> {
// if file name is under the ROUNDTRIP_TESTS_SKIP_LIST then do nothing.
if skip_list_contains_path(&file_name) {
return Ok(());
}
let ion_string = fs::read_to_string(file_name).unwrap();
let mut reader = ReaderBuilder::new().build(ion_string.clone())?;
let mut buffer = Vec::new();
Expand All @@ -43,6 +65,9 @@ mod tests {

#[test_resources("../../input/bad/struct_with_fields/**/*.ion")]
fn roundtrip_bad_test_generated_code_structs_with_fields(file_name: &str) -> SerdeResult<()> {
if skip_list_contains_path(&file_name) {
return Ok(());
}
let ion_string = fs::read_to_string(file_name).unwrap();
let mut reader = ReaderBuilder::new().build(ion_string.clone())?;
// read given Ion value using Ion reader
Expand All @@ -55,6 +80,9 @@ mod tests {

#[test_resources("../../input/good/nested_struct/**/*.ion")]
fn roundtrip_good_test_generated_code_nested_structs(file_name: &str) -> SerdeResult<()> {
if skip_list_contains_path(&file_name) {
return Ok(());
}
let ion_string = fs::read_to_string(file_name).unwrap();
let mut reader = ReaderBuilder::new().build(ion_string.clone())?;
let mut buffer = Vec::new();
Expand Down
2 changes: 1 addition & 1 deletion code-gen-projects/schema/struct_with_fields.isl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ type::{
fields: {
A: string,
B: int,
C: { element: string, type: sexp },
C: { element: string, type: sexp, occurs: required },
D: float,
}
}
Expand Down
Loading

0 comments on commit da69354

Please sign in to comment.