Skip to content

Commit

Permalink
Merge pull request #301 from ignatandrei/300-dusharp
Browse files Browse the repository at this point in the history
dusharp
  • Loading branch information
ignatandrei authored Sep 18, 2024
2 parents 61c7c7a + f27e3fe commit d50dab7
Show file tree
Hide file tree
Showing 43 changed files with 1,481 additions and 234 deletions.
127 changes: 71 additions & 56 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion v2/Generator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//if (x1.res == 2)
//{
// Console.WriteLine("asdasd");
// return;
// return;
//}
try
{
Expand Down
1 change: 1 addition & 0 deletions v2/Generator/all.csv
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,4 @@ Nr,Key,Source,Category
157,Sera.Union, https://github.com/sera-net/Sera.Union,FunctionalProgramming
158,Enhanced.GetTypes, https://github.com/duskembayev/Enhanced.GetTypes,EnhancementClass
159,LightweightObjectMapper, https://github.com/stratosblue/LightweightObjectMapper,Mapper
160,Dusharp, https://github.com/kolebynov/Dusharp,FunctionalProgramming
6 changes: 6 additions & 0 deletions v2/RSCGExamplesData/GeneratorDataRec.json
Original file line number Diff line number Diff line change
Expand Up @@ -954,5 +954,11 @@
"Category": 6,
"dtStart": "2024-09-18T00:00:00",
"show": true
},
{
"ID":"Dusharp",
"Category": 10,
"dtStart": "2024-09-19T00:00:00",
"show": true
}
]
10 changes: 2 additions & 8 deletions v2/RSCGExamplesData/NoExample.json
Original file line number Diff line number Diff line change
Expand Up @@ -844,13 +844,7 @@
"name":"Strings.ResourceGenerator https://github.com/biggik/Strings.ResourceGenerator",
"why":"old ISourceGenerator"
}
,
{
"ID":174,
"name":"Dusharp https://github.com/kolebynov/Dusharp",
"why":"later"
}
,
,
{
"ID":175,
"name":"laker https://github.com/Lakerfield/Lakerfield.Rpc",
Expand All @@ -860,7 +854,7 @@
{
"ID":176,
"name":"embed https://github.com/datacute/EmbeddedResourcePropertyGenerator",
"why":"later"
"why":"issue opened"
}

]
85 changes: 85 additions & 0 deletions v2/book/examples/Dusharp.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@

<h1>RSCG nr 160 : Dusharp</h1>

<h2>Info</h2>
Nuget : <a href="https://www.nuget.org/packages/Dusharp/" target="_blank">https://www.nuget.org/packages/Dusharp/</a>

<p>You can find more details at : <a href="https://github.com/kolebynov/Dusharp" target="_blank"> https://github.com/kolebynov/Dusharp</a></p>

<p>Author :Vitali</p>

<p>Source : <a href="https://github.com/kolebynov/Dusharp" target="_blank">https://github.com/kolebynov/Dusharp</a> </p>

<h2>About</h2>

Generate tagged union

<h2>
How to use
</h2>
<h3>
Add reference to the <a href="https://www.nuget.org/packages/Dusharp/" target="_blank">Dusharp</a> in the csproj
</h3>
<img src="images/Dusharp/UnionTypesDemo.csproj.png" width="580" height="580" />

<h3>This was for me the <b>starting</b> code</h3>

<br />
I have <b>coded</b> the file Program.cs
<br />
<img src="images/Dusharp/csFiles/Program.cs.png" width="580" height="580" />
<hr />

<br />
I have <b>coded</b> the file ResultSave.cs
<br />
<img src="images/Dusharp/csFiles/ResultSave.cs.png" width="580" height="580" />
<hr />

<br />
I have <b>coded</b> the file SaveToDatabase.cs
<br />
<img src="images/Dusharp/csFiles/SaveToDatabase.cs.png" width="580" height="580" />
<hr />
<h3>And here are the <i>generated</i> files</h3>

<br />
The file <i>generated</i> is Dusharp.EmbeddedCode.ExceptionUtils.cs
<br />
<img src="images/Dusharp/generated/Dusharp.EmbeddedCode.ExceptionUtils.cs.png" width="580" height="580" />

<br />
The file <i>generated</i> is Dusharp.EmbeddedCode.UnionAttribute.cs
<br />
<img src="images/Dusharp/generated/Dusharp.EmbeddedCode.UnionAttribute.cs.png" width="580" height="580" />

<br />
The file <i>generated</i> is Dusharp.EmbeddedCode.UnionCaseAttribute.cs
<br />
<img src="images/Dusharp/generated/Dusharp.EmbeddedCode.UnionCaseAttribute.cs.png" width="580" height="580" />

<br />
The file <i>generated</i> is UnionTypesDemo.ResultSave.Dusharp.g.cs
<br />
<img src="images/Dusharp/generated/UnionTypesDemo.ResultSave.Dusharp.g.cs.png" width="580" height="580" />

<br />
The file <i>generated</i> is UnionTypesDemo.ResultSave.union.g.cs
<br />
<img src="images/Dusharp/generated/UnionTypesDemo.ResultSave.union.g.cs.png" width="580" height="580" />

<p>
You can download the code and this page as pdf from
<a target="_blank" href='https://ignatandrei.github.io/RSCG_Examples/v2/docs/Dusharp'>
https://ignatandrei.github.io/RSCG_Examples/v2/docs/Dusharp
</a>
</p>


<p>
You can see the whole list at
<a target="_blank" href='https://ignatandrei.github.io/RSCG_Examples/v2/docs/List-of-RSCG'>
https://ignatandrei.github.io/RSCG_Examples/v2/docs/List-of-RSCG
</a>
</p>

12 changes: 12 additions & 0 deletions v2/book/examples/Sera.Union.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ <h3>This was for me the <b>starting</b> code</h3>
I have <b>coded</b> the file Program.cs
<br />
<img src="images/Sera.Union/csFiles/Program.cs.png" width="580" height="580" />
<hr />

<br />
I have <b>coded</b> the file ResultSave.cs
<br />
<img src="images/Sera.Union/csFiles/ResultSave.cs.png" width="580" height="580" />
<hr />

<br />
I have <b>coded</b> the file SaveToDatabase.cs
<br />
<img src="images/Sera.Union/csFiles/SaveToDatabase.cs.png" width="580" height="580" />
<hr />
<h3>And here are the <i>generated</i> files</h3>

Expand Down
6 changes: 5 additions & 1 deletion v2/book/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</head>
<body>
<h1>
This is the list of 159 RSCG with examples =>
This is the list of 160 RSCG with examples =>
</h1>

<table >
Expand Down Expand Up @@ -662,6 +662,10 @@ <h1>
<td>159</td>
<td><a href="examples/LightweightObjectMapper.html">LightweightObjectMapper</a></td>
</tr>
<tr>
<td>160</td>
<td><a href="examples/Dusharp.html">Dusharp</a></td>
</tr>
</table>


Expand Down
1 change: 1 addition & 0 deletions v2/book/pandocHTML.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ input-files:
- examples/Sera.Union.html
- examples/Enhanced.GetTypes.html
- examples/LightweightObjectMapper.html
- examples/Dusharp.html

# or you may use input-file: with a single value
# defaults:
Expand Down
22 changes: 22 additions & 0 deletions v2/rscg_examples/Dusharp/description.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"generator":{
"name":"Dusharp",
"nuget":[
"https://www.nuget.org/packages/Dusharp/"
],
"link":"https://github.com/kolebynov/Dusharp",
"author":"Vitali",
"source":"https://github.com/kolebynov/Dusharp"
},
"data":{
"goodFor":["Generate tagged union"],
"csprojDemo":"UnionTypesDemo.csproj",
"csFiles":["Program.cs","ResultSave.cs","SaveToDatabase.cs"],
"excludeDirectoryGenerated":[""],
"includeAdditionalFiles":[""]
},
"links":{
"blog":"",
"video":""
}
}
1 change: 1 addition & 0 deletions v2/rscg_examples/Dusharp/nuget.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Dusharp is a C# source generator for creating discriminated unions.
89 changes: 89 additions & 0 deletions v2/rscg_examples/Dusharp/readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Dusharp

[![NuGet](https://img.shields.io/nuget/v/Dusharp)](https://www.nuget.org/packages/Dusharp/)

**Dusharp** is a C# source generator library for creating **discriminated unions**. This library allows you to define union types with ease, using attributes and partial methods. It is inspired by functional languages but built for C# developers.

## Features

- ✅ **Create unions**: Define discriminated unions using attributes.
- ✅ **Match method**: Pattern match on union cases in a type-safe way.
- ✅ **Equality**: Automatic equality comparison for unions.
- ✅ **Generics**: Generics support for union types.
- ✅ **Pretty print**: Using overloaded `ToString()`.
- ❌ **JSON serialization/deserialization**: Support for unions with `System.Text.Json` (coming soon).
- ❌ **Struct unions**: With efficient memory layout for unions as structs (coming soon).

## Installation

Dusharp is available as a NuGet package. You can install it using the NuGet package manager:

```bash
dotnet add package Dusharp
```

## Usage

`Dusharp` uses attributes to generate discriminated unions and case methods. Here's how to get started:

### 1. Define a Union
To define a union, annotate a class with the `[Dusharp.UnionAttribute]` attribute.

```csharp
using Dusharp;

[Union]
public partial class Shape<T>
where T : struct, INumber<T>
{
}
```

### 2. Define Union Cases
Define union cases by creating public static partial methods and marking them with the `[Dusharp.UnionCaseAttribute]` attribute. The method body will be automatically generated.

```csharp
using Dusharp;

[Union]
public partial class Shape<T>
where T : struct, INumber<T>
{
[UnionCase]
public static partial Shape<T> Circle(T radius);

[UnionCase]
public static partial Shape<T> Rectangle(T width, T height);
}
```

### 3. Match on Union
You can easily perform pattern matching on a union using the `Match` method. The source generator will create the `Match` method based on the defined union cases.

```csharp
Shape<double> shape = Shape<double>.Circle(5.0);

string result = shape.Match(
radius => $"Circle with radius {radius}",
(width, height) => $"Rectangle with width {width} and height {height}");

Console.WriteLine(result); // Output: Circle with radius 5.0
```

### 4. Compare Unions
Union cases can be compared for equality using the auto-generated equality methods. This allows for checking if two unions are the same.

```csharp
Shape<double> shape1 = Shape<double>.Circle(5.0);
Shape<double> shape2 = Shape<double>.Circle(5.0);

Console.WriteLine(shape1.Equals(shape2)); // True
Console.WriteLine(shape1 == shape2); // True
```

## Upcoming Features
- **JSON serialization/deserialization**: Support for JSON (de)serialization via System.Text.Json.
- **Struct unions**: More efficient unions using structs with effective data layout.

## License
This project is licensed under the MIT License - see the LICENSE file for details.
66 changes: 66 additions & 0 deletions v2/rscg_examples/Dusharp/src/.tours/Dusharp.tour
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@

{
"$schema": "https://aka.ms/codetour-schema",
"title": "Dusharp",
"steps":
[
{
"file": "UnionTypesDemo/UnionTypesDemo.csproj",
"description": "First, we add Nuget [Dusharp](https://www.nuget.org/packages/Dusharp/) in csproj ",
"pattern": "Dusharp"
}

,{
"file": "UnionTypesDemo/SaveToDatabase.cs",
"description": "File SaveToDatabase.cs ",
"pattern": "this is the code"
}

,{
"file": "UnionTypesDemo/ResultSave.cs",
"description": "File ResultSave.cs ",
"pattern": "this is the code"
}

,{
"file": "UnionTypesDemo/Program.cs",
"description": "File Program.cs \r\n>> dotnet run --project UnionTypesDemo/UnionTypesDemo.csproj ",
"pattern": "this is the code"
}


,{
"file": "UnionTypesDemo/obj/GX/Sera.TaggedUnion.Analyzers/Sera.TaggedUnion.Analyzers.Generators.UnionGenerator/UnionTypesDemo.ResultSave.union.g.cs",
"description": "Generated File 5 from 5 : UnionTypesDemo.ResultSave.union.g.cs ",
"line": 1
}

,{
"file": "UnionTypesDemo/obj/GX/Dusharp/Dusharp.UnionSourceGenerator/UnionTypesDemo.ResultSave.Dusharp.g.cs",
"description": "Generated File 4 from 5 : UnionTypesDemo.ResultSave.Dusharp.g.cs ",
"line": 1
}

,{
"file": "UnionTypesDemo/obj/GX/Dusharp/Dusharp.UnionSourceGenerator/Dusharp.EmbeddedCode.UnionCaseAttribute.cs",
"description": "Generated File 3 from 5 : Dusharp.EmbeddedCode.UnionCaseAttribute.cs ",
"line": 1
}

,{
"file": "UnionTypesDemo/obj/GX/Dusharp/Dusharp.UnionSourceGenerator/Dusharp.EmbeddedCode.UnionAttribute.cs",
"description": "Generated File 2 from 5 : Dusharp.EmbeddedCode.UnionAttribute.cs ",
"line": 1
}

,{
"file": "UnionTypesDemo/obj/GX/Dusharp/Dusharp.UnionSourceGenerator/Dusharp.EmbeddedCode.ExceptionUtils.cs",
"description": "Generated File 1 from 5 : Dusharp.EmbeddedCode.ExceptionUtils.cs ",
"line": 1
}

],

"ref": "main"

}
25 changes: 25 additions & 0 deletions v2/rscg_examples/Dusharp/src/UnionTypesDemo.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.7.34221.43
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnionTypesDemo", "UnionTypesDemo\UnionTypesDemo.csproj", "{B3F52D30-D0FB-4DF2-B494-6B903B16613D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B3F52D30-D0FB-4DF2-B494-6B903B16613D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B3F52D30-D0FB-4DF2-B494-6B903B16613D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B3F52D30-D0FB-4DF2-B494-6B903B16613D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B3F52D30-D0FB-4DF2-B494-6B903B16613D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7C63CD9F-20A2-4EAB-9766-127A7184E23B}
EndGlobalSection
EndGlobal
Loading

0 comments on commit d50dab7

Please sign in to comment.