Skip to content

Commit

Permalink
task: updated source gen templates
Browse files Browse the repository at this point in the history
  • Loading branch information
dtanglr committed Jan 22, 2024
1 parent 494f9fc commit 0edb8ac
Show file tree
Hide file tree
Showing 20 changed files with 49 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Primitively" Version="1.4.15-rc.1" PrivateAssets="all" />
<PackageReference Include="Primitively" Version="1.4.15-rc.2" PrivateAssets="all" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
<ItemGroup>
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.6" />
<PackageReference Include="MongoDB.Driver" Version="2.23.1" />
<PackageReference Include="Primitively.AspNetCore.Mvc" Version="1.4.15-rc.1" />
<PackageReference Include="Primitively.AspNetCore.SwaggerGen" Version="1.4.15-rc.1" />
<PackageReference Include="Primitively.MongoDB.Bson" Version="1.4.15-rc.1" />
<PackageReference Include="Primitively.AspNetCore.Mvc" Version="1.4.15-rc.2" />
<PackageReference Include="Primitively.AspNetCore.SwaggerGen" Version="1.4.15-rc.2" />
<PackageReference Include="Primitively.MongoDB.Bson" Version="1.4.15-rc.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

<PropertyGroup Condition="'$(Configuration)' == 'Debug'">
<PackageOutputPath>$(MSBuildThisFileDirectory)..\artifacts</PackageOutputPath>
<version>1.4.15-rc.1</version>
<version>1.4.15-rc.2</version>
</PropertyGroup>

<ItemGroup>
Expand Down
5 changes: 1 addition & 4 deletions src/Primitively.Abstractions/ByteAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using System.Diagnostics;

namespace Primitively;
namespace Primitively;

/// <summary>
/// Make a readonly record struct that encapsulates a Unsigned 8-bit integer primitive value
/// with a default range of: 0 to 255
/// </summary>
[AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)]
[Conditional(Constants.ConditionalCompilationSymbol)]
public sealed class ByteAttribute : Attribute, IIntegerAttribute<byte>
{
/// <inheritdoc/>
Expand Down
8 changes: 0 additions & 8 deletions src/Primitively.Abstractions/Constants.cs

This file was deleted.

5 changes: 1 addition & 4 deletions src/Primitively.Abstractions/DateOnlyAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using System.Diagnostics;

namespace Primitively;
namespace Primitively;

/// <summary>
/// Make a readonly record struct that encapsulates a DateOnly primitive value
/// with default Iso8601 format of yyyy-MM-dd
/// </summary>
[AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)]
[Conditional(Constants.ConditionalCompilationSymbol)]
public sealed class DateOnlyAttribute : Attribute, IPimitivelyAttribute
{
/// <inheritdoc/>
Expand Down
5 changes: 1 addition & 4 deletions src/Primitively.Abstractions/GuidAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
using System.Diagnostics;

namespace Primitively;
namespace Primitively;

/// <summary>
/// Make a readonly record struct that encapsulates a GUID primitive value
/// </summary>
[AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)]
[Conditional(Constants.ConditionalCompilationSymbol)]
public sealed class GuidAttribute : Attribute, IPimitivelyAttribute
{
public GuidAttribute()
Expand Down
11 changes: 7 additions & 4 deletions src/Primitively.Abstractions/IntAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
using System.Diagnostics;

namespace Primitively;
namespace Primitively;

/// <summary>
/// Make a readonly record struct that encapsulates a Signed 32-bit integer primitive value
/// with a default range of: -2,147,483,648 to 2,147,483,647
/// </summary>
[AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)]
[Conditional(Constants.ConditionalCompilationSymbol)]
public sealed class IntAttribute : Attribute, IIntegerAttribute<int>
{
/// <inheritdoc/>
public bool ImplementIValidatableObject { get; set; }

/// <inheritdoc/>
public int Minimum { get; set; } = int.MinValue;

/// <inheritdoc/>
public int Maximum { get; set; } = int.MaxValue;

object IIntegerAttribute.Minimum { get => Minimum; }

object IIntegerAttribute.Maximum { get => Maximum; }
}
5 changes: 1 addition & 4 deletions src/Primitively.Abstractions/LongAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using System.Diagnostics;

namespace Primitively;
namespace Primitively;

/// <summary>
/// Make a readonly record struct that encapsulates a Signed 64-bit integer primitive value
/// with a default range of: -9,223,372,036,854,775,807 to 9,223,372,036,854,775,807
/// </summary>
[AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)]
[Conditional(Constants.ConditionalCompilationSymbol)]
public sealed class LongAttribute : Attribute, IIntegerAttribute<long>
{
/// <inheritdoc/>
Expand Down
5 changes: 1 addition & 4 deletions src/Primitively.Abstractions/SByteAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using System.Diagnostics;

namespace Primitively;
namespace Primitively;

/// <summary>
/// Make a readonly record struct that encapsulates a Signed 8-bit integer primitive value
/// with a default range of: -128 to 127
/// </summary>
[AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)]
[Conditional(Constants.ConditionalCompilationSymbol)]
public sealed class SByteAttribute : Attribute, IIntegerAttribute<sbyte>
{
/// <inheritdoc/>
Expand Down
5 changes: 1 addition & 4 deletions src/Primitively.Abstractions/ShortAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using System.Diagnostics;

namespace Primitively;
namespace Primitively;

/// <summary>
/// Make a readonly record struct that encapsulates a Signed 16-bit integer primitive value
/// with a default range of: -32,768 to 32,767
/// </summary>
[AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)]
[Conditional(Constants.ConditionalCompilationSymbol)]
public sealed class ShortAttribute : Attribute, IIntegerAttribute<short>
{
/// <inheritdoc/>
Expand Down
5 changes: 1 addition & 4 deletions src/Primitively.Abstractions/StringAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
using System.Diagnostics;

#nullable enable
#nullable enable

namespace Primitively;

/// <summary>
/// Make a readonly record struct that encapsulates a String primitive value
/// </summary>
[AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)]
[Conditional(Constants.ConditionalCompilationSymbol)]
public sealed class StringAttribute : Attribute, IPimitivelyAttribute
{
/// <summary>
Expand Down
5 changes: 1 addition & 4 deletions src/Primitively.Abstractions/UIntAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using System.Diagnostics;

namespace Primitively;
namespace Primitively;

/// <summary>
/// Make a readonly record struct that encapsulates an Unsigned 32-bit integer primitive value
/// with a default range of: 0 to 4,294,967,295
/// </summary>
[AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)]
[Conditional(Constants.ConditionalCompilationSymbol)]
public sealed class UIntAttribute : Attribute, IIntegerAttribute<uint>
{
/// <inheritdoc/>
Expand Down
5 changes: 1 addition & 4 deletions src/Primitively.Abstractions/ULongAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using System.Diagnostics;

namespace Primitively;
namespace Primitively;

/// <summary>
/// Make a readonly record struct that encapsulates an Unsigned 64-bit integer primitive value
/// with a default range of: 0 to 18,446,744,073,709,551,615
/// </summary>
[AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)]
[Conditional(Constants.ConditionalCompilationSymbol)]
public sealed class ULongAttribute : Attribute, IIntegerAttribute<ulong>
{
/// <inheritdoc/>
Expand Down
5 changes: 1 addition & 4 deletions src/Primitively.Abstractions/UShortAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using System.Diagnostics;

namespace Primitively;
namespace Primitively;

/// <summary>
/// Make a readonly record struct that encapsulates an Unsigned 16-bit integer primitive value
/// with a default range of: 0 to 65,535
/// </summary>
[AttributeUsage(AttributeTargets.Struct, Inherited = false, AllowMultiple = false)]
[Conditional(Constants.ConditionalCompilationSymbol)]
public sealed class UShortAttribute : Attribute, IIntegerAttribute<ushort>
{
/// <inheritdoc/>
Expand Down
4 changes: 3 additions & 1 deletion src/Primitively/EmbeddedResources/PrimitiveFactory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
public partial class PrimitiveFactory : Primitively.IPrimitiveFactory
namespace PRIMITIVE_NAMESPACE;

public partial class PrimitiveFactory : Primitively.IPrimitiveFactory
{
#nullable enable
public Primitively.IPrimitive? Create(System.Type type, string? value)
Expand Down
8 changes: 3 additions & 5 deletions src/Primitively/EmbeddedResources/PrimitiveLibrary.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using Primitively;

namespace PRIMITIVE_NAMESPACE;
namespace PRIMITIVE_NAMESPACE;

public readonly record struct PrimitiveLibrary
{
public static IPrimitiveRepository Respository { get; } = new PrimitiveRepository();
public static IPrimitiveFactory Factory { get; } = new PrimitiveFactory();
public static Primitively.IPrimitiveRepository Respository { get; } = new PrimitiveRepository();
public static Primitively.IPrimitiveFactory Factory { get; } = new PrimitiveFactory();
public static bool HasTypes => PRIMITIVE_LIBRARY_HAS_TYPES;
}
22 changes: 8 additions & 14 deletions src/Primitively/EmbeddedResources/PrimitiveRepository.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Primitively;
namespace PRIMITIVE_NAMESPACE;

namespace PRIMITIVE_NAMESPACE;

public partial class PrimitiveRepository : IPrimitiveRepository
public partial class PrimitiveRepository : Primitively.IPrimitiveRepository
{
private static readonly Lazy<IEnumerable<PrimitiveInfo>> _types = new(GetAll, LazyThreadSafetyMode.ExecutionAndPublication);
private static readonly System.Lazy<System.Collections.Generic.IEnumerable<Primitively.PrimitiveInfo>> _types = new(GetAll, System.Threading.LazyThreadSafetyMode.ExecutionAndPublication);

public PrimitiveInfo GetType(Type type) => _types.Value.Single(t => t.Type.Equals(type));
public Primitively.PrimitiveInfo GetType(System.Type type) => _types.Value.Single(t => t.Type.Equals(type));
#nullable enable
public bool TryGetType(Type type, out PrimitiveInfo? result)
public bool TryGetType(System.Type type, out Primitively.PrimitiveInfo? result)
#nullable disable
{
result = _types.Value.SingleOrDefault(t => t.Type.Equals(type));

return result is not null;
}

public IReadOnlyCollection<PrimitiveInfo> GetTypes() => _types.Value.ToList();
public System.Collections.Generic.IReadOnlyCollection<Primitively.PrimitiveInfo> GetTypes() => _types.Value.ToList();

public IReadOnlyCollection<T> GetTypes<T>() where T : PrimitiveInfo => _types.Value.OfType<T>().ToList();
public System.Collections.Generic.IReadOnlyCollection<T> GetTypes<T>() where T : Primitively.PrimitiveInfo => _types.Value.OfType<T>().ToList();

private static IEnumerable<PrimitiveInfo> GetAll()
private static System.Collections.Generic.IEnumerable<Primitively.PrimitiveInfo> GetAll()
{
PRIMITIVE_REPOSITORY_YIELD_STATEMENTS
}
Expand Down
2 changes: 0 additions & 2 deletions src/Primitively/MetaData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

internal readonly struct MetaData
{
public const string ConditionalCompilationSymbol = "PRIMITIVELY_USAGES";

public readonly struct DateOnly
{
public static readonly string Interface = typeof(IDateOnly).FullName;
Expand Down
27 changes: 13 additions & 14 deletions src/Primitively/Structs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,10 @@ private static void GenerateFactoryFile(SourceProductionContext context, List<Re
var sb = new StringBuilder();
sb.Append(EmbeddedResources.AutoGeneratedHeader);
sb.AppendLine();
sb.AppendLine($"namespace {compilation.AssemblyName};"); // TODO: Get Root Namespace from the Compilation class
sb.AppendLine();
sb.Append(EmbeddedResources.PrimitiveFactory);

// Replace variable names with values
sb.Replace("PRIMITIVE_NAMESPACE", compilation.AssemblyName); // TODO: Get Root Namespace from the Compilation class
sb.Replace("PRIMITIVE_FACTORY_CASE_STATEMENTS", string.Join("\r\n", caseStatements));

// Construct source file text from string
Expand All @@ -179,7 +178,7 @@ private static void GenerateRepositoryFile(SourceProductionContext context, List

if (!dataTypes.Any())
{
yieldStatements.Add($"{Padding}return Enumerable.Empty<PrimitiveInfo>();");
yieldStatements.Add($"{Padding}return System.Linq.Enumerable.Empty<Primitively.IPrimitiveInfo>();");
}
else
{
Expand All @@ -191,17 +190,17 @@ private static void GenerateRepositoryFile(SourceProductionContext context, List
.OrderBy(rs => rs.Name)
.Select(rs => rs.DataType switch
{
DataType.DateOnly => $"{Padding}yield return new DateOnlyInfo(typeof({rs.NameSpace}.{rs.Name}), typeof(DateOnly), \"{rs.Example}\", (value) => ({rs.NameSpace}.{rs.Name})value, \"{rs.Format}\", {rs.Length});",
DataType.Guid => $"{Padding}yield return new GuidInfo(typeof({rs.NameSpace}.{rs.Name}), typeof(Guid), \"{rs.Example}\", (value) => ({rs.NameSpace}.{rs.Name})value, {nameof(rs.Specifier)}.{rs.Specifier}, {rs.Length});",
DataType.Byte => $"{Padding}yield return new IntegerInfo({nameof(DataType)}.{nameof(DataType.Byte)}, typeof({rs.NameSpace}.{rs.Name}), typeof(byte), \"{rs.Example}\", (value) => ({rs.NameSpace}.{rs.Name})value, {rs.Minimum ?? 0}, {rs.Maximum ?? 0});",
DataType.SByte => $"{Padding}yield return new IntegerInfo({nameof(DataType)}.{nameof(DataType.SByte)}, typeof({rs.NameSpace}.{rs.Name}), typeof(sbyte), \"{rs.Example}\", (value) => ({rs.NameSpace}.{rs.Name})value, {rs.Minimum ?? 0}, {rs.Maximum ?? 0});",
DataType.Short => $"{Padding}yield return new IntegerInfo({nameof(DataType)}.{nameof(DataType.Short)}, typeof({rs.NameSpace}.{rs.Name}), typeof(short), \"{rs.Example}\", (value) => ({rs.NameSpace}.{rs.Name})value, {rs.Minimum ?? 0}, {rs.Maximum ?? 0});",
DataType.UShort => $"{Padding}yield return new IntegerInfo({nameof(DataType)}.{nameof(DataType.UShort)}, typeof({rs.NameSpace}.{rs.Name}), typeof(ushort), \"{rs.Example}\", (value) => ({rs.NameSpace}.{rs.Name})value, {rs.Minimum ?? 0}, {rs.Maximum ?? 0});",
DataType.Int => $"{Padding}yield return new IntegerInfo({nameof(DataType)}.{nameof(DataType.Int)}, typeof({rs.NameSpace}.{rs.Name}), typeof(int), \"{rs.Example}\", (value) => ({rs.NameSpace}.{rs.Name})value, {rs.Minimum ?? 0}, {rs.Maximum ?? 0});",
DataType.UInt => $"{Padding}yield return new IntegerInfo({nameof(DataType)}.{nameof(DataType.UInt)}, typeof({rs.NameSpace}.{rs.Name}), typeof(uint), \"{rs.Example}\", (value) => ({rs.NameSpace}.{rs.Name})value, {rs.Minimum ?? 0}, {rs.Maximum ?? 0});",
DataType.Long => $"{Padding}yield return new IntegerInfo({nameof(DataType)}.{nameof(DataType.Long)}, typeof({rs.NameSpace}.{rs.Name}), typeof(long), \"{rs.Example}\", (value) => ({rs.NameSpace}.{rs.Name})value, {rs.Minimum ?? 0}, {rs.Maximum ?? 0});",
DataType.ULong => $"{Padding}yield return new IntegerInfo({nameof(DataType)}.{nameof(DataType.ULong)}, typeof({rs.NameSpace}.{rs.Name}), typeof(ulong), \"{rs.Example}\", (value) => ({rs.NameSpace}.{rs.Name})value, {rs.Minimum ?? 0}, {rs.Maximum ?? 0});",
DataType.String => $"{Padding}yield return new StringInfo(typeof({rs.NameSpace}.{rs.Name}), typeof(string), \"{rs.Example}\", (value) => ({rs.NameSpace}.{rs.Name})value, \"{rs.Format}\", \"{rs.Pattern}\", {rs.MinLength}, {rs.MaxLength});",
DataType.DateOnly => $"{Padding}yield return new Primitively.DateOnlyInfo(typeof({rs.NameSpace}.{rs.Name}), typeof(System.DateOnly), \"{rs.Example}\", (value) => ({rs.NameSpace}.{rs.Name})value, \"{rs.Format}\", {rs.Length});",
DataType.Guid => $"{Padding}yield return new Primitively.GuidInfo(typeof({rs.NameSpace}.{rs.Name}), typeof(System.Guid), \"{rs.Example}\", (value) => ({rs.NameSpace}.{rs.Name})value, {nameof(rs.Specifier)}.{rs.Specifier}, {rs.Length});",
DataType.Byte => $"{Padding}yield return new Primitively.IntegerInfo({nameof(DataType)}.{nameof(DataType.Byte)}, typeof({rs.NameSpace}.{rs.Name}), typeof(byte), \"{rs.Example}\", (value) => ({rs.NameSpace}.{rs.Name})value, {rs.Minimum ?? 0}, {rs.Maximum ?? 0});",
DataType.SByte => $"{Padding}yield return new Primitively.IntegerInfo({nameof(DataType)}.{nameof(DataType.SByte)}, typeof({rs.NameSpace}.{rs.Name}), typeof(sbyte), \"{rs.Example}\", (value) => ({rs.NameSpace}.{rs.Name})value, {rs.Minimum ?? 0}, {rs.Maximum ?? 0});",
DataType.Short => $"{Padding}yield return new Primitively.IntegerInfo({nameof(DataType)}.{nameof(DataType.Short)}, typeof({rs.NameSpace}.{rs.Name}), typeof(short), \"{rs.Example}\", (value) => ({rs.NameSpace}.{rs.Name})value, {rs.Minimum ?? 0}, {rs.Maximum ?? 0});",
DataType.UShort => $"{Padding}yield return new Primitively.IntegerInfo({nameof(DataType)}.{nameof(DataType.UShort)}, typeof({rs.NameSpace}.{rs.Name}), typeof(ushort), \"{rs.Example}\", (value) => ({rs.NameSpace}.{rs.Name})value, {rs.Minimum ?? 0}, {rs.Maximum ?? 0});",
DataType.Int => $"{Padding}yield return new Primitively.IntegerInfo({nameof(DataType)}.{nameof(DataType.Int)}, typeof({rs.NameSpace}.{rs.Name}), typeof(int), \"{rs.Example}\", (value) => ({rs.NameSpace}.{rs.Name})value, {rs.Minimum ?? 0}, {rs.Maximum ?? 0});",
DataType.UInt => $"{Padding}yield return new Primitively.IntegerInfo({nameof(DataType)}.{nameof(DataType.UInt)}, typeof({rs.NameSpace}.{rs.Name}), typeof(uint), \"{rs.Example}\", (value) => ({rs.NameSpace}.{rs.Name})value, {rs.Minimum ?? 0}, {rs.Maximum ?? 0});",
DataType.Long => $"{Padding}yield return new Primitively.IntegerInfo({nameof(DataType)}.{nameof(DataType.Long)}, typeof({rs.NameSpace}.{rs.Name}), typeof(long), \"{rs.Example}\", (value) => ({rs.NameSpace}.{rs.Name})value, {rs.Minimum ?? 0}, {rs.Maximum ?? 0});",
DataType.ULong => $"{Padding}yield return new Primitively.IntegerInfo({nameof(DataType)}.{nameof(DataType.ULong)}, typeof({rs.NameSpace}.{rs.Name}), typeof(ulong), \"{rs.Example}\", (value) => ({rs.NameSpace}.{rs.Name})value, {rs.Minimum ?? 0}, {rs.Maximum ?? 0});",
DataType.String => $"{Padding}yield return new Primitively.StringInfo(typeof({rs.NameSpace}.{rs.Name}), typeof(string), \"{rs.Example}\", (value) => ({rs.NameSpace}.{rs.Name})value, \"{rs.Format}\", \"{rs.Pattern}\", {rs.MinLength}, {rs.MaxLength});",
_ => throw new NotImplementedException()
});

Expand Down

0 comments on commit 0edb8ac

Please sign in to comment.