From a905fc1e90105713458e25035c3652a247c4afe2 Mon Sep 17 00:00:00 2001 From: Mohammed Omar Miraj <103403052+MOmarMiraj@users.noreply.github.com> Date: Wed, 28 Aug 2024 11:20:21 -0400 Subject: [PATCH] Remove requirement of -go-package from typeshare with go feature (#184) * Remove requirement of -go-package from typeshare with go feature Add check to see if no package flag is passed or in typeshare.toml Fmt fix clippy fix Cleanup logic and make it cleaner fix logic fix return value cargo clippy fix Cleanup function fix logic of go_package function update logic for error handling fix logic to correctly return package name Capitalize all variant names when configuration specifies so in Go Signed-off-by: Omar Miraj Add support for Kotlin inline value classes Signed-off-by: Omar Miraj Introduce redacted typeshare parameter Signed-off-by: Omar Miraj keep the file output deterministic Signed-off-by: Omar Miraj Deterministic output I want to use a parallel iterator while walking the input folders however in order to maintain a deterministic output I'll have to keep the crate to parsed data sorted along with the types within each parsed data. Consequently the tests have been updated since they did not have this sorting before. Signed-off-by: Omar Miraj Add determinism ensurance tests Signed-off-by: Omar Miraj chore: update changelog and bump version numbers for release v1.10.0-beta.7 Signed-off-by: Omar Miraj Explicitly format variant types in Go Signed-off-by: Omar Miraj Remove dead code and switch if else to match Signed-off-by: Omar Miraj Add check to see if no package flag is passed or in typeshare.toml Fmt fix Signed-off-by: Omar Miraj Cleanup logic and make it cleaner update logic for error handling fix logic to correctly return package name Capitalize all variant names when configuration specifies so in Go Signed-off-by: Omar Miraj keep the file output deterministic Deterministic output I want to use a parallel iterator while walking the input folders however in order to maintain a deterministic output I'll have to keep the crate to parsed data sorted along with the types within each parsed data. Consequently the tests have been updated since they did not have this sorting before. Signed-off-by: Omar Miraj Explicitly format variant types in Go Signed-off-by: Omar Miraj Remove dead code and switch if else to match Signed-off-by: Omar Miraj * make the package check more idiomatic * fix merge conflict regression --------- Signed-off-by: Omar Miraj --- cli/data/tests/go_config.toml | 2 ++ cli/src/config.rs | 8 ++++++++ cli/src/main.rs | 28 ++++++++++++++++++++-------- core/src/rust_types.rs | 7 +++---- 4 files changed, 33 insertions(+), 12 deletions(-) create mode 100644 cli/data/tests/go_config.toml diff --git a/cli/data/tests/go_config.toml b/cli/data/tests/go_config.toml new file mode 100644 index 00000000..7fca3676 --- /dev/null +++ b/cli/data/tests/go_config.toml @@ -0,0 +1,2 @@ +[go] +package="testPackage" \ No newline at end of file diff --git a/cli/src/config.rs b/cli/src/config.rs index d8a526b1..d0608a7c 100644 --- a/cli/src/config.rs +++ b/cli/src/config.rs @@ -180,4 +180,12 @@ mod test { assert_eq!(config.swift.prefix, "test"); } + #[test] + #[cfg(feature = "go")] + fn go_package_test() { + let path = config_file_path("go_config.toml"); + let config = load_config(Some(path)).unwrap(); + + assert_eq!(config.go.package, "testPackage"); + } } diff --git a/cli/src/main.rs b/cli/src/main.rs index fccdcd3c..3cd34a8a 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -46,7 +46,7 @@ fn main() -> anyhow::Result<()> { .long("go-package") .help("Go package name") .takes_value(true) - .required_if(ARG_TYPE, "go"), + .required(false), ); } @@ -63,10 +63,10 @@ fn main() -> anyhow::Result<()> { let config_file = options.value_of(ARG_CONFIG_FILE_NAME); let config = config::load_config(config_file).context("Unable to read configuration file")?; - let config = override_configuration(config, &options); + let config = override_configuration(config, &options)?; if options.is_present(ARG_GENERATE_CONFIG) { - let config = override_configuration(Config::default(), &options); + let config = override_configuration(Config::default(), &options)?; let file_path = options.value_of(ARG_CONFIG_FILE_NAME); config::store_config(&config, file_path).context("Failed to create new config file")?; } @@ -196,7 +196,7 @@ fn language( } /// Overrides any configuration values with provided arguments -fn override_configuration(mut config: Config, options: &ArgMatches) -> Config { +fn override_configuration(mut config: Config, options: &ArgMatches) -> anyhow::Result { if let Some(swift_prefix) = options.value_of(ARG_SWIFT_PREFIX) { config.swift.prefix = swift_prefix.to_string(); } @@ -222,15 +222,17 @@ fn override_configuration(mut config: Config, options: &ArgMatches) -> Config { } #[cfg(feature = "go")] - if let Some(go_package) = options.value_of(args::ARG_GO_PACKAGE) { - config.go.package = go_package.to_string(); + { + if let Some(go_package) = options.value_of(args::ARG_GO_PACKAGE) { + config.go.package = go_package.to_string(); + } + assert_go_package_present(&config)?; } - config.target_os = options .get_many::(ARG_TARGET_OS) .map(|arg| arg.into_iter().map(ToString::to_string).collect::>()) .unwrap_or_default(); - config + Ok(config) } /// Prints out all parsing errors if any and returns Err. @@ -255,3 +257,13 @@ fn check_parse_errors(parsed_crates: &BTreeMap) -> anyhow Ok(()) } } + +#[cfg(feature = "go")] +fn assert_go_package_present(config: &Config) -> anyhow::Result<()> { + if config.go.package.is_empty() { + return Err(anyhow!( + "Please provide a package name in the typeshare.toml or using --go-package " + )); + } + Ok(()) +} diff --git a/core/src/rust_types.rs b/core/src/rust_types.rs index d0d87308..4585b05f 100644 --- a/core/src/rust_types.rs +++ b/core/src/rust_types.rs @@ -156,10 +156,9 @@ pub enum RustType { /// - `SomeStruct` /// - `SomeEnum` /// - `SomeTypeAlias<(), &str>` - /// - /// However, there are some generic types that are considered to be _special_. These - /// include `Vec` `HashMap`, and `Option`, which are part of `SpecialRustType` instead - /// of `RustType::Generic`. + /// However, there are some generic types that are considered to be _special_. These + /// include `Vec` `HashMap`, and `Option`, which are part of `SpecialRustType` instead + /// of `RustType::Generic`. /// /// If a generic type is type-mapped via `typeshare.toml`, the generic parameters will be dropped automatically. Generic {