Skip to content

Commit

Permalink
feat: Use Discriminator mapping values for names AnyOf property names.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Oct 26, 2024
1 parent 10c2a9f commit 6a575e9
Show file tree
Hide file tree
Showing 132 changed files with 5,759 additions and 5,786 deletions.
89 changes: 47 additions & 42 deletions src/libs/AutoSDK/Models/AnyOfData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public readonly record struct AnyOfData(
int Count,
TypeData? DiscriminatorType,
string? DiscriminatorPropertyName,
//EquatableArray<(string, string)>? DiscriminatorMapping,
bool IsTrimming,
string Namespace,
string Name,
Expand All @@ -19,6 +18,8 @@ public readonly record struct AnyOfData(
Settings Settings
)
{
public bool IsNamed => !string.IsNullOrWhiteSpace(Name);

public static AnyOfData FromSchemaContext(SchemaContext context)
{
context = context ?? throw new ArgumentNullException(nameof(context));
Expand All @@ -33,26 +34,55 @@ public static AnyOfData FromSchemaContext(SchemaContext context)
var className = context.Id.ToClassName();
TypeData? discriminatorType = null;
string? discriminatorPropertyName = null;
//IDictionary<string, string>? discriminatorMapping = null;

if (context.Schema.Discriminator != null &&
context.Schema.Discriminator.Mapping.Count != 0)
{
discriminatorType = context.Children.FirstOrDefault(x => x.Hint == Hint.Discriminator)?.TypeData;
discriminatorPropertyName = context.Schema.Discriminator.PropertyName.ToPropertyName();

// if (context.Schema.Discriminator.Mapping.Count == 0)
// {
// if (children.All(x =>
// x.Children.FirstOrDefault(y => y.PropertyName == discriminatorPropertyName)?.GetDefaultValue() != null &&
// x.ClassName != null))
// {
// context.Schema.Discriminator.Mapping = children
// .ToDictionary(
// x => x.GetDefaultValue()!,
// x => x.ClassName);
// }
// }
}

var count = context.IsAnyOf
? context.Schema.AnyOf.Count
: context.IsOneOf
? context.Schema.OneOf.Count
: context.Schema.AllOf.Count;
var properties = context.IsNamedAnyOfLike
? children.Select((x, i) => PropertyData.Default with
{
Type = x.TypeData,
Name = SmartNamedAnyOfNames.ComputePropertyName(children, className, i),
Summary = x.Schema.GetSummary(),
DiscriminatorValue = context.Schema.Discriminator?.Mapping?
.FirstOrDefault(y =>
y.Value.Contains(x.Id) ||
(x.Schema.Properties.ContainsKey(context.Schema.Discriminator.PropertyName) &&
x.Schema.Properties[context.Schema.Discriminator.PropertyName].Enum.Count == 1 &&
x.Schema.Properties[context.Schema.Discriminator.PropertyName].Enum.FirstOrDefault()
?.GetString() == y.Key))
.Key?.ToEnumValue(string.Empty, context.Settings).Name ?? string.Empty,
}).ToImmutableArray().AsEquatableArray()
: Enumerable
.Range(1, count)
.Select(i => PropertyData.Default with
{
Name = $"Value{i}",
Type = TypeData.Default with
{
CSharpTypeRaw = $"T{i}",
},
})
.ToImmutableArray().AsEquatableArray();
if (context.IsNamedAnyOfLike &&
!properties.IsEmpty &&
properties.All(x => !string.IsNullOrWhiteSpace(x.DiscriminatorValue)))
{
properties = properties
.Select(x => x with
{
Name = x.DiscriminatorValue,
})
.ToImmutableArray().AsEquatableArray();
}

return new AnyOfData(
Expand All @@ -61,16 +91,9 @@ public static AnyOfData FromSchemaContext(SchemaContext context)
: context.IsOneOf
? "OneOf"
: "AllOf",
Count: context.IsAnyOf
? context.Schema.AnyOf.Count
: context.IsOneOf
? context.Schema.OneOf.Count
: context.Schema.AllOf.Count,
Count: count,
DiscriminatorType: discriminatorType,
DiscriminatorPropertyName: discriminatorPropertyName,
//DiscriminatorMapping: discriminatorMapping?
// .Select(x => (x.Key, x.Value))
// .ToImmutableArray(),
IsTrimming:
context.Settings.JsonSerializerType == JsonSerializerType.SystemTextJson &&
(!string.IsNullOrWhiteSpace(context.Settings.JsonSerializerContext) ||
Expand All @@ -82,25 +105,7 @@ public static AnyOfData FromSchemaContext(SchemaContext context)
Summary: context.IsNamedAnyOfLike
? context.Schema.GetSummary()
: string.Empty,
Properties: context.IsNamedAnyOfLike
? children.Select((x, i) => PropertyData.Default with
{
Type = x.TypeData,
Name = SmartNamedAnyOfNames.ShouldUseSmartName(children, className)
? SmartNamedAnyOfNames.ComputeSmartName(
x.TypeData,
className)
: $"Value{i + 1}",
Summary = x.Schema.GetSummary(),
DiscriminatorValue = context.Schema.Discriminator?.Mapping?
.FirstOrDefault(y =>
y.Value.Contains(x.Id) ||
(x.Schema.Properties.ContainsKey(context.Schema.Discriminator.PropertyName) &&
x.Schema.Properties[context.Schema.Discriminator.PropertyName].Enum.Count == 1 &&
x.Schema.Properties[context.Schema.Discriminator.PropertyName].Enum.FirstOrDefault()?.GetString() == y.Key))
.Key?.ToEnumValue(string.Empty, context.Settings).Name ?? string.Empty,
}).ToImmutableArray()
: ImmutableArray<PropertyData>.Empty,
Properties: properties,
Settings: context.Settings);
}
}
13 changes: 0 additions & 13 deletions src/libs/AutoSDK/Models/PropertyData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,17 +240,4 @@ internal static string HandleWordSeparators(string name)
.ReplaceIfEquals("void", "@void")
.ReplaceIfEquals("volatile", "@volatile")
.ReplaceIfEquals("while", "@while");

public string ArgumentName
{
get
{
if (Type.EnumValues.Length != 0 && Settings.JsonSerializerType == JsonSerializerType.NewtonsoftJson)
{
return ParameterName + "Value";
}

return ParameterName;
}
}
}
9 changes: 9 additions & 0 deletions src/libs/AutoSDK/Naming/AnyOfs/SmartNamedAnyOfNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ namespace AutoSDK.Naming.AnyOfs;

public static class SmartNamedAnyOfNames
{
public static string ComputePropertyName(IList<SchemaContext> children, string className, int i)
{
return ShouldUseSmartName(children, className)
? ComputeSmartName(
children.ElementAt(i).TypeData,
className)
: $"Value{i + 1}";
}

public static bool ShouldUseSmartName(IList<SchemaContext> children, string className)
{
return children.All(x =>
Expand Down
66 changes: 26 additions & 40 deletions src/libs/AutoSDK/Sources/Sources.AnyOf.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Collections.Immutable;
using AutoSDK.Extensions;
using AutoSDK.Models;

Expand All @@ -11,82 +10,69 @@ public static string GenerateAnyOf(
CancellationToken cancellationToken = default)
{
var types = $"<{string.Join(", ", Enumerable.Range(1, anyOfData.Count).Select(x => $"T{x}"))}>";
var classNameWithoutTypes = string.IsNullOrWhiteSpace(anyOfData.Name)
var classNameWithoutTypes = !anyOfData.IsNamed
? $"{anyOfData.SubType}"
: anyOfData.Name;
var className = string.IsNullOrWhiteSpace(anyOfData.Name)
var className = !anyOfData.IsNamed
? $"{anyOfData.SubType}{types}"
: anyOfData.Name;
var allTypes = anyOfData.Properties.IsEmpty
? Enumerable
.Range(1, anyOfData.Count)
.Select(i => PropertyData.Default with
{
Name = $"Value{i}",
Type = TypeData.Default with
{
CSharpTypeRaw = $"T{i}",
},
})
.ToImmutableArray().AsEquatableArray()
: anyOfData.Properties;
var validation = anyOfData.SubType switch
{
"AnyOf" => string.Join(" || ", allTypes.Select(x => $"Is{x.Name}")),
"OneOf" => string.Join(" || ", allTypes.Select((x, xi) =>
string.Join(" && ", allTypes.Select((y, yi) => $"{(yi == xi ? "" : "!")}Is{y.Name}")))),
"AllOf" => string.Join(" && ", allTypes.Select(x => $"Is{x.Name}")),
"AnyOf" => string.Join(" || ", anyOfData.Properties.Select(x => $"Is{x.Name}")),
"OneOf" => string.Join(" || ", anyOfData.Properties.Select((x, xi) =>
string.Join(" && ", anyOfData.Properties.Select((y, yi) => $"{(yi == xi ? "" : "!")}Is{y.Name}")))),
"AllOf" => string.Join(" && ", anyOfData.Properties.Select(x => $"Is{x.Name}")),
_ => throw new NotImplementedException(),
};
var constructorWithAllValues =
anyOfData.Count > 1 ||
(!string.IsNullOrWhiteSpace(anyOfData.Name) &&
(anyOfData.IsNamed &&
anyOfData.DiscriminatorType != null &&
anyOfData.DiscriminatorPropertyName != null &&
allTypes.All(x => !string.IsNullOrWhiteSpace(x.DiscriminatorValue))) ? $@"
anyOfData.Properties.All(x => !string.IsNullOrWhiteSpace(x.DiscriminatorValue))) ? $@"
{string.Empty.ToXmlDocumentationSummary(level: 8)}
public {classNameWithoutTypes}(
{(string.IsNullOrWhiteSpace(anyOfData.Name) ||
{(!anyOfData.IsNamed ||
anyOfData.DiscriminatorType == null ||
anyOfData.DiscriminatorPropertyName == null ||
allTypes.Any(x => string.IsNullOrWhiteSpace(x.DiscriminatorValue)) ? " " : $@"
anyOfData.Properties.Any(x => string.IsNullOrWhiteSpace(x.DiscriminatorValue)) ? " " : $@"
{anyOfData.DiscriminatorType.Value.CSharpTypeWithoutNullability}{anyOfData.DiscriminatorPropertyName}? {anyOfData.DiscriminatorPropertyName.ToParameterName()},
")}
{allTypes.Select(x => $@"
{anyOfData.Properties.Select(x => $@"
{x.Type.CSharpTypeWithNullability} {x.ParameterName},
").Inject().TrimEnd(',', '\n')}
)
{{
{(string.IsNullOrWhiteSpace(anyOfData.Name) ||
{(!anyOfData.IsNamed ||
anyOfData.DiscriminatorType == null ||
anyOfData.DiscriminatorPropertyName == null ||
allTypes.Any(x => string.IsNullOrWhiteSpace(x.DiscriminatorValue)) ? " " : $@"
anyOfData.Properties.Any(x => string.IsNullOrWhiteSpace(x.DiscriminatorValue)) ? " " : $@"
{anyOfData.DiscriminatorPropertyName} = {anyOfData.DiscriminatorPropertyName.ToParameterName()};
")}
{allTypes.Select(x => $@"
{anyOfData.Properties.Select(x => $@"
{x.Name} = {x.ParameterName};
").Inject()}
}}" : " ";
var json = GenerateFromToJsonMethods(anyOfData.Namespace, className, anyOfData.Settings, isValueType: true, cancellationToken);

return $@"using System.Linq;
{(anyOfData.Properties.IsEmpty ? "" : @"#pragma warning disable CS0618 // Type or member is obsolete
")}
{(anyOfData.IsNamed ? @"#pragma warning disable CS0618 // Type or member is obsolete
" : "")}
#nullable enable
namespace {anyOfData.Namespace}
{{
{anyOfData.Summary.ToXmlDocumentationSummary(level: 4)}
public readonly partial struct {className} : global::System.IEquatable<{className}>
{{
{(string.IsNullOrWhiteSpace(anyOfData.Name) ||
{(!anyOfData.IsNamed ||
anyOfData.DiscriminatorType == null ||
anyOfData.DiscriminatorPropertyName == null ||
allTypes.Any(x => string.IsNullOrWhiteSpace(x.DiscriminatorValue)) ? " " : $@"
anyOfData.Properties.Any(x => string.IsNullOrWhiteSpace(x.DiscriminatorValue)) ? " " : $@"
{string.Empty.ToXmlDocumentationSummary(level: 8)}
public {anyOfData.DiscriminatorType.Value.CSharpTypeWithoutNullability}{anyOfData.DiscriminatorPropertyName}? {anyOfData.DiscriminatorPropertyName} {{ get; }}
")}
{allTypes.Select(x => $@"
{anyOfData.Properties.Select(x => $@"
{x.Summary.ToXmlDocumentationSummary(level: 8)}
#if NET6_0_OR_GREATER
public {x.Type.CSharpTypeWithNullability} {x.Name} {{ get; init; }}
Expand Down Expand Up @@ -117,7 +103,7 @@ namespace {anyOfData.Namespace}
{string.Empty.ToXmlDocumentationSummary(level: 8)}
public object? Object =>
{allTypes.Reverse().Select(x => $@"
{anyOfData.Properties.Reverse().Select(x => $@"
{x.Name} as object ??
").Inject().TrimEnd('?', '\n')}
;
Expand All @@ -130,7 +116,7 @@ public bool Validate()
{string.Empty.ToXmlDocumentationSummary(level: 8)}
public TResult? Match<TResult>(
{allTypes.Select(x => $@"
{anyOfData.Properties.Select(x => $@"
global::System.Func<{x.Type.CSharpType}, TResult>? {x.ParameterName} = null,
").Inject()}
bool validate = true)
Expand All @@ -140,7 +126,7 @@ public bool Validate()
Validate();
}}
{allTypes.Select((x, i) => $@"
{anyOfData.Properties.Select((x, i) => $@"
{(i > 0 ? "else " : "")}if (Is{x.Name} && {x.ParameterName} != null)
{{
return {x.ParameterName}({x.Name}!);
Expand All @@ -151,7 +137,7 @@ public bool Validate()
{string.Empty.ToXmlDocumentationSummary(level: 8)}
public void Match(
{allTypes.Select(x => $@"
{anyOfData.Properties.Select(x => $@"
global::System.Action<{x.Type.CSharpType}>? {x.ParameterName} = null,
").Inject()}
bool validate = true)
Expand All @@ -161,7 +147,7 @@ public void Match(
Validate();
}}
{allTypes.Select((x, i) => $@"
{anyOfData.Properties.Select((x, i) => $@"
{(i > 0 ? "else " : "")}if (Is{x.Name})
{{
{x.ParameterName}?.Invoke({x.Name}!);
Expand All @@ -173,7 +159,7 @@ public override int GetHashCode()
{{
var fields = new object?[]
{{
{allTypes.Select(x => $@"
{anyOfData.Properties.Select(x => $@"
{x.Name},
typeof({x.Type.CSharpTypeWithoutNullability}),
").Inject()}
Expand All @@ -193,7 +179,7 @@ static int HashCodeAggregator(int hashCode, object? value) => value == null
public bool Equals({className} other)
{{
return
{allTypes.Select(x => $@"
{anyOfData.Properties.Select(x => $@"
global::System.Collections.Generic.EqualityComparer<{x.Type.CSharpTypeWithNullability}>.Default.Equals({x.Name}, other.{x.Name}) &&
").Inject().TrimEnd('&', '\n')}
;
Expand Down
Loading

0 comments on commit 6a575e9

Please sign in to comment.