Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Money generic #24

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>
<PropertyGroup Label="Common Settings">
<LangVersion>10.0</LangVersion>
<LangVersion>11.0</LangVersion>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<ImplicitUsings>enable</ImplicitUsings>
<IsPackable>false</IsPackable>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
<PropertyGroup Label="Shared NuGet Metadata">
<Authors>Polyadic</Authors>
Expand Down
22 changes: 22 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>
<ItemGroup Label="Runtime Dependencies">
<PackageVersion Include="Funcky" Version="[3.0.0, 4.0.0)" />
</ItemGroup>
<ItemGroup Label="Build Dependencies">
<PackageVersion Include="Funcky.DiscriminatedUnion" Version="1.1.0" />
<PackageVersion Include="Messerli.CodeStyle" Version="2.3.0 " />
<PackageVersion Include="IsExternalInit" Version="[1.0.3, 2.0.0)" />
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.7.0" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="1.1.1" />
</ItemGroup>
<ItemGroup Label="Test Dependencies">
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.7.1" />
<PackageVersion Include="xunit" Version="2.4.2" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.4.2" />
<PackageVersion Include="Funcky.Xunit" Version="2.0.1" />
<PackageVersion Include="FsCheck.Xunit" Version="2.16.6" />
<PackageVersion Include="coverlet.collector" Version="[1.0.0, 2.0.0)" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk; Microsoft.Build.CentralPackageVersions">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Funcky" GeneratePathProperty="true" PrivateAssets="all" />
Expand Down
47 changes: 23 additions & 24 deletions Funcky.Money.SourceGenerator/Iso4217RecordGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Funcky.Monads;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
using static System.Environment;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;

namespace Funcky.Money.SourceGenerator;
Expand Down Expand Up @@ -34,18 +33,18 @@ private static void GenerateSource(SourceProductionContext context, ImmutableArr
}

private static string GenerateCurrencyClass(IReadOnlyList<Iso4217Record> records)
=> $"using System.Collections.Generic;{NewLine}" +
$"using System.Collections.Immutable;{NewLine}" +
$"using Funcky.Monads;{NewLine}" +
$"namespace {RootNamespace}{NewLine}" +
$"{{{NewLine}" +
$"{Indent}public partial record Currency{NewLine}" +
$"{Indent}{{{NewLine}" +
=> $"using System.Collections.Generic;\n" +
$"using System.Collections.Immutable;\n" +
$"using Funcky.Monads;\n" +
$"namespace {RootNamespace}\n" +
$"{{\n" +
$"{Indent}public partial record Currency\n" +
$"{Indent}{{\n" +
$"{GenerateCurrencyProperties(records)}" +
$"{GenerateAllCurrenciesProperty(records)}" +
$"{GenerateParseMethod(records)}" +
$"{Indent}}}{NewLine}" +
$"}}{NewLine}";
$"{Indent}}}\n" +
$"}}\n";

private static string GenerateParseMethod(IEnumerable<Iso4217Record> records)
{
Expand All @@ -58,11 +57,11 @@ private static string GenerateParseMethod(IEnumerable<Iso4217Record> records)

switchCases.AppendLine($"{Indent}{Indent}{Indent} _ => Option<Currency>.None,");

return $"{Indent}{Indent}public static partial Option<Currency> ParseOrNone(string input){NewLine}" +
$"{Indent}{Indent} => input switch{NewLine}" +
$"{Indent}{Indent} {{{NewLine}" +
return $"{Indent}{Indent}public static partial Option<Currency> ParseOrNone(string input)\n" +
$"{Indent}{Indent} => input switch\n" +
$"{Indent}{Indent} {{\n" +
switchCases +
$"{Indent}{Indent} }};{NewLine}";
$"{Indent}{Indent} }};\n";
}

private static string GenerateCurrencyProperties(IEnumerable<Iso4217Record> records)
Expand Down Expand Up @@ -101,14 +100,14 @@ private static string GenerateAllCurrenciesProperty(IReadOnlyCollection<Iso4217R
}

private static string GenerateMoneyClass(IEnumerable<Iso4217Record> records)
=> $"using Funcky.Monads;{NewLine}" +
$"namespace {RootNamespace}{NewLine}" +
$"{{{NewLine}" +
$"{Indent}public partial record Money{NewLine}" +
$"{Indent}{{{NewLine}" +
=> $"using Funcky.Monads;\n" +
$"namespace {RootNamespace}\n" +
$"{{\n" +
$"{Indent}public partial record Money<TUnderlyingType>\n" +
$"{Indent}{{\n" +
$"{GenerateMoneyFactoryMethods(records)}" +
$"{Indent}}}{NewLine}" +
$"}}{NewLine}";
$"{Indent}}}\n" +
$"}}\n";

private static string GenerateMoneyFactoryMethods(IEnumerable<Iso4217Record> records)
=> records.Aggregate(new StringBuilder(), AppendCurrencyFactory).ToString();
Expand All @@ -120,9 +119,9 @@ private static string CreateCurrencyFactory(Iso4217Record record)
{
var identifier = Identifier(record.AlphabeticCurrencyCode);

return $"{Indent}{Indent}/// <summary>Creates a new <see cref=\"Money\" /> instance using the <see cref=\"Currency.{identifier}\" /> currency.</summary>{NewLine}" +
$"{Indent}{Indent}public static Money {identifier}(decimal amount){NewLine}" +
$"{Indent}{Indent} => new(amount, MoneyEvaluationContext.Builder.Default.WithTargetCurrency(Currency.{identifier}).Build());";
return $"{Indent}{Indent}/// <summary>Creates a new <see cref=\"Money{{TUnderlyingType}}\" /> instance using the <see cref=\"Currency.{identifier}\" /> currency.</summary>\n" +
$"{Indent}{Indent}public static Money<TUnderlyingType> {identifier}(TUnderlyingType amount)\n" +
$"{Indent}{Indent} => new(amount, MoneyEvaluationContext<TUnderlyingType>.Builder.Default.WithTargetCurrency(Currency.{identifier}).Build());";
}

private static IEnumerable<Iso4217Record> ReadIso4217RecordsFromAdditionalFiles(
Expand Down
2 changes: 0 additions & 2 deletions Funcky.Money.SourceGenerator/XmlNodeExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Xml;

namespace Funcky.Money.SourceGenerator;
Expand Down
9 changes: 6 additions & 3 deletions Funcky.Money.Test/Funcky.Money.Test.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk; Microsoft.Build.CentralPackageVersions">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;</TargetFrameworks>
<TargetFrameworks>net7.0</TargetFrameworks>
<RootNamespace>Funcky.Test</RootNamespace>
</PropertyGroup>

Expand All @@ -10,7 +10,10 @@
<PackageReference Include="Funcky.Xunit" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio" />
<PackageReference Include="xunit.runner.visualstudio">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
8 changes: 4 additions & 4 deletions Funcky.Money.Test/MoneyArbitraries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ internal class MoneyArbitraries
public static Arbitrary<Currency> ArbitraryCurrency()
=> Arb.From(Gen.Elements<Currency>(Currency.AllCurrencies));

public static Arbitrary<Money> ArbitraryMoney()
public static Arbitrary<Money<decimal>> ArbitraryMoney()
=> GenerateMoney().ToArbitrary();

public static Arbitrary<SwissMoney> ArbitrarySwissMoney()
=> GenerateSwissFranc().ToArbitrary();

private static Gen<Money> GenerateMoney()
private static Gen<Money<decimal>> GenerateMoney()
=> from currency in Arb.Generate<Currency>()
from amount in Arb.Generate<int>()
select new Money(Power.OfATenth(currency.MinorUnitDigits) * amount, currency);
select new Money<decimal>(Power<decimal>.OfATenth(currency.MinorUnitDigits) * amount, currency);

private static Gen<SwissMoney> GenerateSwissFranc()
=> from amount in Arb.Generate<int>()
select new SwissMoney(Money.CHF(SwissMoney.SmallestCoin * amount));
select new SwissMoney(Money<decimal>.CHF(SwissMoney.SmallestCoin * amount));
}
Loading
Loading