Skip to content

Commit

Permalink
Resolve AllOff.
Browse files Browse the repository at this point in the history
  • Loading branch information
Corniel committed Aug 21, 2023
1 parent a3e7847 commit 373e8de
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 7 deletions.
3 changes: 2 additions & 1 deletion specs/Qowaiv.CodeGeneration.Specs/Syntax/Literal_specs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ internal class Suports
[TestCase(null, "null")]
[TestCase(true, "true")]
[TestCase(false, "false")]
[TestCase(@"Qowaiv ""quoted""", @"""Qowaiv \""quoted\""""")]
[TestCase(@"Qowaiv ""quoted""", @"@""Qowaiv ""quoted""""")]
[TestCase(@"\d{9}", @"@""\d{9}""")]
[TestCase(typeof(Guid), "typeof(System.Guid)")]
[TestCase(123d, "123")]
[TestCase(123.17, "123.17")]
Expand Down
6 changes: 6 additions & 0 deletions src/Qowaiv.CodeGeneration.OpenApi/Extensions/System.Linq.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace System.Linq;

internal static class LinqExtensions
{
public static bool HasMultiple<T>(this IEnumerable<T> enumerable) => enumerable.Skip(1).Any();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ partial class OpenApiTypeResolver
private static readonly IReadOnlyDictionary<string, Type> Formats = new Dictionary<string, Type>
{
["BIC"] = typeof(Qowaiv.Financial.BusinessIdentifierCode),
["BYTE"] = typeof(System.BinaryData),
["COUNTRY"] = typeof(Qowaiv.Globalization.Country),
["CURRENCY"] = typeof(Qowaiv.Financial.Currency),
["DATE"] = typeof(System.DateOnly),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Qowaiv.CodeGeneration.Syntax;
using System.Linq;

namespace Qowaiv.CodeGeneration.OpenApi;

Expand Down Expand Up @@ -39,7 +40,8 @@ public partial class OpenApiTypeResolver
OpenApiType.@string => ResolveString(schema),
OpenApiType.array => ResolveArray(schema),
OpenApiType.@object => ResolveObject(schema),
_ when schema.OneOf.Skip(1).Any() => ResolveOneOf(schema),
_ when schema.OneOf.HasMultiple() => ResolveOneOf(schema),
_ when schema.AllOf.Any() => ResolveAllOf(schema),
_ => ResolveOther(schema),
};

Expand Down Expand Up @@ -101,6 +103,7 @@ static bool IsNone(EnumerationField field)
"INT64" => typeof(long),
"AMOUNT" => typeof(Qowaiv.Financial.Amount),
"ELO" => typeof(Qowaiv.Statistics.Elo),
"PERCENTAGE" => typeof(Qowaiv.Percentage),
_ => typeof(decimal),
};

Expand Down Expand Up @@ -196,6 +199,21 @@ static bool IsNone(EnumerationField field)
else return null;
}

/// <summary>
/// Resolves the <see cref="Type"/> when there is at least one <see cref="ResolveOpenApiSchema.AllOf"/>'s.
/// </summary>
[Pure]
protected virtual Type? ResolveAllOf(ResolveOpenApiSchema schema)
{
return Resolve(schema.With(schema.AllOf[0]));

//if (schema.AllOf.Count == 1)
//{
// return Resolve(schema.With(schema.AllOf[0]));
//}
//else throw new NotSupportedException($"Schema with type multiple all-off is not supported.");
}

/// <summary>Resolves the <see cref="Type"/> for <see cref="OpenApiType.None"/>.</summary>
[Pure]
protected virtual Type? ResolveOther(ResolveOpenApiSchema schema)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<PackageReference Include="Microsoft.OpenApi" Version="1.6.6" />
<PackageReference Include="Microsoft.OpenApi.Readers" Version="1.6.6" />
<PackageReference Include="Qowaiv.Validation.Abstractions" Version="0.3.0" />
<PackageReference Include="System.Memory.Data" Version="7.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Qowaiv.CodeGeneration/Constant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{
public readonly string? Name;

public Constant(string name) => Name = Guard.NotNullOrEmpty(name, nameof(name));
public Constant(string name) => Name = Guard.NotNullOrEmpty(name);

/// <inheritdoc />
[Pure]
Expand Down
6 changes: 4 additions & 2 deletions src/Qowaiv.CodeGeneration/NamingStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ public static string Enum(string name)
{
name = '_' + name;
}
return name
name = name
.Replace("+", "_pls")
.Replace(" ", "_")
.Replace("-", "_")
.Replace(";", "_")
.Replace("(", string.Empty)
.Replace(")", string.Empty);

return EscapeKeywords(name);
}

[Pure]
Expand All @@ -47,5 +49,5 @@ public static string EscapeKeywords(this string name)
? "@" + name
: name;

private static readonly string[] keywords = new[] { "default" };
private static readonly string[] keywords = new[] { "default", "new", };
}
15 changes: 13 additions & 2 deletions src/Qowaiv.CodeGeneration/Syntax/Literal.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Qowaiv.CodeGeneration.Syntax;
using System.IO;

namespace Qowaiv.CodeGeneration.Syntax;

/// <summary>Represents a code literal.</summary>
public sealed class Literal : Code
Expand All @@ -20,8 +22,9 @@ public void WriteTo(CSharpWriter writer)
else if (Value is int int32) writer.Write(int32.ToString(CultureInfo.InvariantCulture));
else if (Value is double dbl) writer.Write(Double(dbl));
else if (Value is decimal dec) writer.Write(dec.ToString(CultureInfo.InvariantCulture)).Write('m');
else if (Value is string str) writer.Write('"').Write(str.Replace(@"""", @"\""")).Write('"');
else if (Value is string str) writer.Write(String(str));
else throw new NotSupportedException($"Literals of type {Value.GetType()} are not supported");

}

[Pure]
Expand All @@ -35,6 +38,14 @@ private static string Double(double dbl)
else return dbl.ToString(CultureInfo.InvariantCulture);
}

[Pure]
private static string String(string str)
{
return str.Contains('\\') || str.Contains('"')
? $@"@""{str}"""
: $@"""{str}""";
}

/// <inheritdoc />
[Pure]
public override string ToString() => this.Stringify();
Expand Down

0 comments on commit 373e8de

Please sign in to comment.