Skip to content

Commit

Permalink
Always use renamed property on subtype value position
Browse files Browse the repository at this point in the history
  • Loading branch information
dandro committed Jul 23, 2024
1 parent 44e2191 commit f45e2a6
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 50 deletions.
6 changes: 3 additions & 3 deletions core/data/tests/anonymous_struct_with_rename/output.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public class AnonymousStructWithRenameKebabCaseInner {
}

[JsonConverter(typeof(JsonSubtypes), "type")]
[JsonSubtypes.KnownSubType(typeof(List), "List")]
[JsonSubtypes.KnownSubType(typeof(LongFieldNames), "LongFieldNames")]
[JsonSubtypes.KnownSubType(typeof(KebabCase), "KebabCase")]
[JsonSubtypes.KnownSubType(typeof(List), "list")]
[JsonSubtypes.KnownSubType(typeof(LongFieldNames), "longFieldNames")]
[JsonSubtypes.KnownSubType(typeof(KebabCase), "kebabCase")]
public abstract record AnonymousStructWithRename
{
public record list(AnonymousStructWithRenameListInner Content): AnonymousStructWithRename();
Expand Down
8 changes: 4 additions & 4 deletions core/data/tests/can_generate_algebraic_enum/output.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ public record ReallyCoolType(ItemDetailsFieldValue Content) : AdvancedColors();


[JsonConverter(typeof(JsonSubtypes), "type")]
[JsonSubtypes.KnownSubType(typeof(String), "String")]
[JsonSubtypes.KnownSubType(typeof(Number), "Number")]
[JsonSubtypes.KnownSubType(typeof(NumberArray), "NumberArray")]
[JsonSubtypes.KnownSubType(typeof(ReallyCoolType), "ReallyCoolType")]
[JsonSubtypes.KnownSubType(typeof(String), "string")]
[JsonSubtypes.KnownSubType(typeof(Number), "number")]
[JsonSubtypes.KnownSubType(typeof(NumberArray), "number-array")]
[JsonSubtypes.KnownSubType(typeof(ReallyCoolType), "really-cool-type")]
public abstract record AdvancedColors2
{
/** This is a case comment */
Expand Down
6 changes: 3 additions & 3 deletions core/data/tests/csharp_without_naming_convention/output.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public enum DimensionFitValue
}

[JsonConverter(typeof(JsonSubtypes), "type")]
[JsonSubtypes.KnownSubType(typeof(fixed-size), "fixed-size")]
[JsonSubtypes.KnownSubType(typeof(percentage), "percentage")]
[JsonSubtypes.KnownSubType(typeof(fit), "fit")]
[JsonSubtypes.KnownSubType(typeof(FixedSize), "fixed-size")]
[JsonSubtypes.KnownSubType(typeof(Percentage), "percentage")]
[JsonSubtypes.KnownSubType(typeof(Fit), "fit")]
public abstract record DimensionValue
{
public record FixedSize(float Value) : DimensionValue();
Expand Down
16 changes: 8 additions & 8 deletions core/data/tests/recursive_enum_decorator/output.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
using System.Runtime.Serialization;

[JsonConverter(typeof(JsonSubtypes), "type")]
[JsonSubtypes.KnownSubType(typeof(Red), "Red")]
[JsonSubtypes.KnownSubType(typeof(Banana), "Banana")]
[JsonSubtypes.KnownSubType(typeof(Vermont), "Vermont")]
[JsonSubtypes.KnownSubType(typeof(Red), "red")]
[JsonSubtypes.KnownSubType(typeof(Banana), "banana")]
[JsonSubtypes.KnownSubType(typeof(Vermont), "vermont")]
public abstract record Options
{
public record Red(bool Content) : Options();
Expand All @@ -30,14 +30,14 @@ public class MoreOptionsBuiltInner {
}

[JsonConverter(typeof(JsonSubtypes), "type")]
[JsonSubtypes.KnownSubType(typeof(News), "News")]
[JsonSubtypes.KnownSubType(typeof(Exactly), "Exactly")]
[JsonSubtypes.KnownSubType(typeof(Built), "Built")]
[JsonSubtypes.KnownSubType(typeof(News), "news")]
[JsonSubtypes.KnownSubType(typeof(Exactly), "exactly")]
[JsonSubtypes.KnownSubType(typeof(Built), "built")]
public abstract record MoreOptions
{
public record News(bool Content) : MoreOptions();
public record exactly(MoreOptionsExactlyInner Content): MoreOptions();
public record built(MoreOptionsBuiltInner Content): MoreOptions();
public record Exactly(MoreOptionsExactlyInner Content): MoreOptions();
public record Built(MoreOptionsBuiltInner Content): MoreOptions();
}


Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ public class ItemDetailsFieldValue {
}

[JsonConverter(typeof(JsonSubtypes), "type")]
[JsonSubtypes.KnownSubType(typeof(String), "String")]
[JsonSubtypes.KnownSubType(typeof(Number), "Number")]
[JsonSubtypes.KnownSubType(typeof(NumberArray), "NumberArray")]
[JsonSubtypes.KnownSubType(typeof(ReallyCoolType), "ReallyCoolType")]
[JsonSubtypes.KnownSubType(typeof(String), "string")]
[JsonSubtypes.KnownSubType(typeof(Number), "number")]
[JsonSubtypes.KnownSubType(typeof(NumberArray), "number-array")]
[JsonSubtypes.KnownSubType(typeof(ReallyCoolType), "reallyCoolType")]
public abstract record AdvancedColors
{
public record String(string Content) : AdvancedColors();
Expand Down
43 changes: 15 additions & 28 deletions core/src/language/csharp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,7 @@ impl Language for CSharp {
writeln!(w, "\n}}\n")
}
RustEnum::Algebraic { shared, .. } => {
write_discriminated_union_json_attributes(
w,
e,
self.without_csharp_naming_convention,
)?;
write_discriminated_union_json_attributes(w, e)?;
write!(
w,
"public abstract record {}{} \n{{",
Expand All @@ -207,11 +203,7 @@ impl Language for CSharp {
}
}

fn write_discriminated_union_json_attributes(
w: &mut dyn Write,
e: &RustEnum,
with_rename: bool,
) -> io::Result<()> {
fn write_discriminated_union_json_attributes(w: &mut dyn Write, e: &RustEnum) -> io::Result<()> {
match e {
RustEnum::Algebraic {
tag_key,
Expand All @@ -220,16 +212,18 @@ fn write_discriminated_union_json_attributes(
} => {
writeln!(w, "[JsonConverter(typeof(JsonSubtypes), \"{}\")]", tag_key)?;
let case_attributes = shared.variants.iter().map(|v| {
let case_name = match v {
let (case_name, renamed) = match v {
RustEnumVariant::AnonymousStruct { shared, .. } => {
get_property_name(with_rename, shared)
(&shared.id.original, &shared.id.renamed)
}
RustEnumVariant::Unit(shared) => (&shared.id.original, &shared.id.renamed),
RustEnumVariant::Tuple { shared, .. } => {
(&shared.id.original, &shared.id.renamed)
}
RustEnumVariant::Unit(shared) => get_property_name(with_rename, shared),
RustEnumVariant::Tuple { shared, .. } => get_property_name(with_rename, shared),
};
format!(
"[JsonSubtypes.KnownSubType(typeof({0}), \"{0}\")]",
case_name,
"[JsonSubtypes.KnownSubType(typeof({0}), \"{1}\")]",
case_name, renamed
)
});

Expand All @@ -239,17 +233,6 @@ fn write_discriminated_union_json_attributes(
}
}

fn get_property_name(
with_rename: bool,
shared: &crate::rust_types::RustEnumVariantShared,
) -> &String {
if with_rename {
&shared.id.renamed
} else {
&shared.id.original
}
}

impl CSharp {
fn write_enum_variants(&mut self, w: &mut dyn Write, e: &RustEnum) -> io::Result<()> {
match e {
Expand Down Expand Up @@ -331,7 +314,11 @@ impl CSharp {
write!(
w,
"\tpublic record {}({}{}Inner{} {}): {}{}();",
shared.id.renamed,
if self.without_csharp_naming_convention {
&shared.id.renamed
} else {
&shared.id.original
},
e.shared().id.original,
shared.id.original,
generics,
Expand Down

0 comments on commit f45e2a6

Please sign in to comment.