Skip to content

Commit

Permalink
Merge pull request #189 from hculea/hculea/format-variant-type-explic…
Browse files Browse the repository at this point in the history
…itly

Explicitly format variant types in Go
  • Loading branch information
seanaye authored Aug 9, 2024
2 parents 4a73bd4 + 0870aae commit 980d8d9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
17 changes: 9 additions & 8 deletions core/src/language/go.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,6 @@ impl Go {
));

if let Some(variant_type) = variant_type {
let variant_type = self.acronyms_to_uppercase(&variant_type);
let (variant_pointer, variant_deref, variant_ref) =
match (v, custom_structs.contains(&variant_type.as_str())) {
(RustEnumVariant::AnonymousStruct { .. }, ..) | (.., true) => {
Expand All @@ -287,17 +286,19 @@ impl Go {
_ => ("", "*", "&"),
};

let formatted_variant_type = self.acronyms_to_uppercase(&variant_type);

decoding_cases.push(format!(
"\t\tvar res {variant_type}
"\t\tvar res {formatted_variant_type}
\t\t{short_name}.{content_field} = &res
",
variant_type = variant_type,
formatted_variant_type = formatted_variant_type,
short_name = struct_short_name,
content_field = content_field,
));
variant_accessors.push(format!(
r#"func ({short_name} {full_name}) {variant_name}() {variant_pointer}{variant_type} {{
res, _ := {short_name}.{content_field}.(*{variant_type})
r#"func ({short_name} {full_name}) {variant_name}() {variant_pointer}{formatted_variant_type} {{
res, _ := {short_name}.{content_field}.(*{formatted_variant_type})
return {variant_deref}res
}}
"#,
Expand All @@ -306,11 +307,11 @@ impl Go {
variant_name = variant_name,
variant_pointer = variant_pointer,
variant_deref = variant_deref,
variant_type = variant_type,
formatted_variant_type = formatted_variant_type,
content_field = content_field,
));
variant_constructors.push(format!(
r#"func New{variant_type_const}(content {variant_pointer}{variant_type}) {struct_name} {{
r#"func New{variant_type_const}(content {variant_pointer}{formatted_variant_type}) {struct_name} {{
return {struct_name}{{
{tag_field}: {variant_type_const},
{content_field}: {variant_ref}content,
Expand All @@ -321,7 +322,7 @@ impl Go {
tag_field = tag_field,
variant_type_const = variant_type_const,
variant_pointer = variant_pointer,
variant_type = variant_type,
formatted_variant_type = formatted_variant_type,
variant_ref = variant_ref,
content_field = content_field,
));
Expand Down
1 change: 1 addition & 0 deletions core/src/rust_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ pub enum RustType {
/// - `SomeStruct<String>`
/// - `SomeEnum<u32>`
/// - `SomeTypeAlias<(), &str>`
///
/// However, there are some generic types that are considered to be _special_. These
/// include `Vec<T>` `HashMap<K, V>`, and `Option<T>`, which are part of `SpecialRustType` instead
/// of `RustType::Generic`.
Expand Down

0 comments on commit 980d8d9

Please sign in to comment.