Skip to content

Commit

Permalink
Rename project and namespace roots to PolyType.
Browse files Browse the repository at this point in the history
  • Loading branch information
eiriktsarpalis committed Nov 8, 2024
1 parent 6ee0ffa commit 4df3a24
Show file tree
Hide file tree
Showing 229 changed files with 653 additions and 656 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ NUGET_SOURCE ?= "https://api.nuget.org/v3/index.json"
NUGET_API_KEY ?= ""
ADDITIONAL_ARGS ?= -p:ContinuousIntegrationBuild=true
CODECOV_ARGS ?= --collect:"XPlat Code Coverage" --results-directory $(ARTIFACT_PATH)
DOCKER_IMAGE_NAME ?= "typeshape-docker-build"
DOCKER_IMAGE_NAME ?= "polytype-docker-build"
DOCKER_CMD ?= make CONFIGURATION=$(CONFIGURATION)

clean:
Expand Down
20 changes: 10 additions & 10 deletions typeshape-csharp.sln → PolyType.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.12.35323.107
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TypeShape", "src\TypeShape\TypeShape.csproj", "{C1D31D26-9A35-4E88-8014-20D600B08306}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PolyType", "src\PolyType\PolyType.csproj", "{C1D31D26-9A35-4E88-8014-20D600B08306}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TypeShape.Examples", "src\TypeShape.Examples\TypeShape.Examples.csproj", "{62A5D47E-C7A8-43B1-8B11-7B964AE14504}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PolyType.Examples", "src\PolyType.Examples\PolyType.Examples.csproj", "{62A5D47E-C7A8-43B1-8B11-7B964AE14504}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SerializationApp.Reflection", "applications\SerializationApp.Reflection\SerializationApp.Reflection.csproj", "{FD0477BD-155D-4E06-9563-5D1F88ECE067}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TypeShape.Roslyn", "src\TypeShape.Roslyn\TypeShape.Roslyn.csproj", "{9B4627DE-9185-4050-82E2-EE8341003A4E}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PolyType.Roslyn", "src\PolyType.Roslyn\PolyType.Roslyn.csproj", "{9B4627DE-9185-4050-82E2-EE8341003A4E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TypeShape.SourceGenerator", "src\TypeShape.SourceGenerator\TypeShape.SourceGenerator.csproj", "{3BE34E78-92CC-4931-9C83-6BF2FF4B02EB}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PolyType.SourceGenerator", "src\PolyType.SourceGenerator\PolyType.SourceGenerator.csproj", "{3BE34E78-92CC-4931-9C83-6BF2FF4B02EB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfigurationBinder.AOT", "applications\ConfigurationBinder.AOT\ConfigurationBinder.AOT.csproj", "{6BDF614A-7D1B-4AA9-8E4E-BAA993771ED1}"
EndProject
Expand All @@ -23,11 +23,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SerializationApp.AOT", "app
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ValidationApp.AOT", "applications\ValidationApp.AOT\ValidationApp.AOT.csproj", "{74D6B54A-294D-4FA4-927A-DA3577393859}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TypeShape.Benchmarks", "tests\TypeShape.Benchmarks\TypeShape.Benchmarks.csproj", "{07EB616A-9393-4C5B-9E3F-E0607FC6009D}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PolyType.Benchmarks", "tests\PolyType.Benchmarks\PolyType.Benchmarks.csproj", "{07EB616A-9393-4C5B-9E3F-E0607FC6009D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TypeShape.SourceGenerator.UnitTests", "tests\TypeShape.SourceGenerator.UnitTests\TypeShape.SourceGenerator.UnitTests.csproj", "{DF05A2E3-4C68-46C9-8CBA-E263CC3F10F8}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PolyType.SourceGenerator.UnitTests", "tests\PolyType.SourceGenerator.UnitTests\PolyType.SourceGenerator.UnitTests.csproj", "{DF05A2E3-4C68-46C9-8CBA-E263CC3F10F8}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TypeShape.Tests", "tests\TypeShape.Tests\TypeShape.Tests.csproj", "{EAABC384-65E6-404B-AA30-12EF559A0545}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PolyType.Tests", "tests\PolyType.Tests\PolyType.Tests.csproj", "{EAABC384-65E6-404B-AA30-12EF559A0545}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".github", ".github", "{E712CC73-2B97-4F4C-840D-4C82F5C07936}"
EndProject
Expand Down Expand Up @@ -61,8 +61,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "docs", "docs", "{B036DAA9-D
docs\core-abstractions.md = docs\core-abstractions.md
docs\docfx.json = docs\docfx.json
docs\index.md = docs\index.md
docs\shape-providers.md = docs\shape-providers.md
docs\toc.yml = docs\toc.yml
docs\typeshape-providers.md = docs\typeshape-providers.md
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{F2C95E2E-AED7-46E4-A93E-831FA658BD2B}"
Expand All @@ -72,9 +72,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{F2C95E2E-AED
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{1EAD80C8-8E21-4173-9438-67CE9A7A3292}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TypeShape.TestCases", "src\TypeShape.TestCases\TypeShape.TestCases.csproj", "{9C5EE30D-663C-42FF-B0B0-A7C240FF0BEE}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PolyType.TestCases", "src\PolyType.TestCases\PolyType.TestCases.csproj", "{9C5EE30D-663C-42FF-B0B0-A7C240FF0BEE}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "TypeShape.TestCases.FSharp", "src\TypeShape.TestCases.FSharp\TypeShape.TestCases.FSharp.fsproj", "{7BC73596-FF6B-5933-D061-4E651B15B43E}"
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "PolyType.TestCases.FSharp", "src\PolyType.TestCases.FSharp\PolyType.TestCases.FSharp.fsproj", "{7BC73596-FF6B-5933-D061-4E651B15B43E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
72 changes: 36 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
# typeshape-csharp [![Build & Tests](https://github.com/eiriktsarpalis/typeshape-csharp/actions/workflows/build.yml/badge.svg)](https://github.com/eiriktsarpalis/typeshape-csharp/actions/workflows/build.yml) [![NuGet Badge](https://img.shields.io/nuget/dt/typeshape-csharp)](https://www.nuget.org/packages/typeshape-csharp/)
# PolyType [![Build & Tests](https://github.com/eiriktsarpalis/PolyType/actions/workflows/build.yml/badge.svg)](https://github.com/eiriktsarpalis/PolyType/actions/workflows/build.yml) [![NuGet Badge](https://img.shields.io/nuget/dt/PolyType)](https://www.nuget.org/packages/PolyType/)

`typeshape-csharp` is a practical datatype-generic programming library for .NET types. It is a port of the [TypeShape](https://github.com/eiriktsarpalis/TypeShape) F# library, adapted to patterns and idioms available in C#. See the [project website](https://eiriktsarpalis.github.io/typeshape-csharp) for additional background and [API documentation](https://eiriktsarpalis.github.io/typeshape-csharp/api/TypeShape.html).
`PolyType` is a practical datatype-generic programming library for .NET types. It is an adaptation of the [TypeShape](https://github.com/eiriktsarpalis/TypeShape) library for F#, adapted to patterns and idioms available in C#. See the [project website](https://eiriktsarpalis.github.io/PolyType) for additional background and [API documentation](https://eiriktsarpalis.github.io/PolyType/api/PolyType.html).

## Quick Start

You can try the library by installing the `typeshape-csharp` NuGet package:
You can try the library by installing the `PolyType` NuGet package:

```bash
$ dotnet add package typeshape-csharp
$ dotnet add package PolyType
```

which includes the core types and source generator for generating type shapes:

```C#
using TypeShape;
using PolyType;

[GenerateShape]
public partial record Person(string name, int age);
```

Doing this will augment `Person` with an implementation of the `IShapeable<Person>` interface. This suffices to make `Person` usable with any library that targets the TypeShape core abstractions. You can try this out by installing the built-in example libraries:
Doing this will augment `Person` with an implementation of the `IShapeable<Person>` interface. This suffices to make `Person` usable with any library that targets the PolyType core abstractions. You can try this out by installing the built-in example libraries:

```bash
$ dotnet add package TypeShape.Examples
$ dotnet add package PolyType.Examples
```

Here's how the same value can be serialized to three separate formats.

```csharp
using TypeShape.Examples.JsonSerializer;
using TypeShape.Examples.CborSerializer;
using TypeShape.Examples.XmlSerializer;
using PolyType.Examples.JsonSerializer;
using PolyType.Examples.CborSerializer;
using PolyType.Examples.XmlSerializer;

Person person = new("Pete", 70);
JsonSerializerTS.Serialize(person); // {"Name":"Pete","Age":70}
XmlSerializer.Serialize(person); // <value><Name>Pete</Name><Age>70</Age></value>
CborSerializer.EncodeToHex(person); // A2644E616D656450657465634167651846
```

Since the application uses a source generator to produce the shape for `Person`, it is fully compatible with Native AOT. See the [TypeShape providers](https://eiriktsarpalis.github.io/typeshape-csharp/typeshape-providers.html) article for more details on how to use the library with your types.
Since the application uses a source generator to produce the shape for `Person`, it is fully compatible with Native AOT. See the [shape providers](https://eiriktsarpalis.github.io/PolyType/shape-providers.html) article for more details on how to use the library with your types.

## Introduction

TypeShape is a meta-library that facilitates rapid development of high performance datatype-generic programs. It exposes a simplified model for .NET types that makes it easy for library authors to publish production-ready components in just a few lines of code. The built-in source generator ensures that any library built on top of the TypeShape abstractions gets Native AOT support for free.
PolyType is a meta-library that facilitates rapid development of high performance datatype-generic programs. It exposes a simplified model for .NET types that makes it easy for library authors to publish production-ready components in just a few lines of code. The built-in source generator ensures that any library built on top of the PolyType abstractions gets Native AOT support for free.

As a library author, TypeShape lets you write high performance, feature complete generic components that target its [core abstractions](https://eiriktsarpalis.github.io/typeshape-csharp/core-abstractions.html). For example, a parser API using TypeShape might look as follows:
As a library author, PolyType lets you write high performance, feature complete generic components that target its [core abstractions](https://eiriktsarpalis.github.io/PolyType/core-abstractions.html). For example, a parser API using PolyType might look as follows:

```C#
public static class MyFancyParser
Expand All @@ -53,7 +53,7 @@ public static class MyFancyParser
}
```

As an end user, TypeShape lets you generate shape models for your own types that can be used with one or more supported libraries:
As an end user, PolyType lets you generate shape models for your own types that can be used with one or more supported libraries:

```C#
Person? person = MyFancyParser.Parse<Person>(format); // Compiles
Expand All @@ -64,18 +64,18 @@ partial record Person(string name, int age, List<Person> children);

For more information see:

* The [core abstractions](https://eiriktsarpalis.github.io/typeshape-csharp/core-abstractions.html) document for an overview of the core programming model.
* The [typeshape providers](https://eiriktsarpalis.github.io/typeshape-csharp/typeshape-providers.html) document for an overview of the built-in shape providers and their APIs.
* The generated [API documentation](https://eiriktsarpalis.github.io/typeshape-csharp/api/TypeShape.html) for the project.
* The [`TypeShape.Examples`](https://github.com/eiriktsarpalis/typeshape-csharp/tree/main/src/TypeShape.Examples) project for advanced examples of libraries built on top of TypeShape.
* The [core abstractions](https://eiriktsarpalis.github.io/PolyType/core-abstractions.html) document for an overview of the core programming model.
* The [shape providers](https://eiriktsarpalis.github.io/PolyType/shape-providers.html) document for an overview of the built-in shape providers and their APIs.
* The generated [API documentation](https://eiriktsarpalis.github.io/PolyType/api/PolyType.html) for the project.
* The [`PolyType.Examples`](https://github.com/eiriktsarpalis/PolyType/tree/main/src/PolyType.Examples) project for advanced examples of libraries built on top of PolyType.

## Case Study: Writing a JSON serializer

The repo includes a [JSON serializer](https://github.com/eiriktsarpalis/typeshape-csharp/tree/main/src/TypeShape.Examples/JsonSerializer) built on top of the `Utf8JsonWriter`/`Utf8JsonReader` primitives provided by System.Text.Json. At the time of writing, the full implementation is just under 1200 lines of code but exceeds STJ's built-in `JsonSerializer` both in terms of [supported types](https://github.com/eiriktsarpalis/typeshape-csharp/blob/main/tests/TypeShape.Tests/JsonTests.cs) and performance.
The repo includes a [JSON serializer](https://github.com/eiriktsarpalis/PolyType/tree/main/src/PolyType.Examples/JsonSerializer) built on top of the `Utf8JsonWriter`/`Utf8JsonReader` primitives provided by System.Text.Json. At the time of writing, the full implementation is just under 1200 lines of code but exceeds STJ's built-in `JsonSerializer` both in terms of [supported types](https://github.com/eiriktsarpalis/PolyType/blob/main/tests/PolyType.Tests/JsonTests.cs) and performance.

### Performance

Here's a [benchmark](https://github.com/eiriktsarpalis/typeshape-csharp/blob/main/tests/TypeShape.Benchmarks/JsonBenchmark.cs) comparing `System.Text.Json` with the included TypeShape-derived implementation:
Here's a [benchmark](https://github.com/eiriktsarpalis/PolyType/blob/main/tests/PolyType.Benchmarks/JsonBenchmark.cs) comparing `System.Text.Json` with the included PolyType implementation:

#### Serialization

Expand All @@ -84,37 +84,37 @@ Here's a [benchmark](https://github.com/eiriktsarpalis/typeshape-csharp/blob/mai
| Serialize_StjReflection | 491.9 ns | 1.00 | 312 B | 1.00 |
| Serialize_StjSourceGen | 467.0 ns | 0.95 | 312 B | 1.00 |
| Serialize_StjSourceGen_FastPath | 227.2 ns | 0.46 | - | 0.00 |
| Serialize_TypeShapeReflection | 277.9 ns | 0.57 | - | 0.00 |
| Serialize_TypeShapeSourceGen | 273.6 ns | 0.56 | - | 0.00 |
| Serialize_PolyTypeReflection | 277.9 ns | 0.57 | - | 0.00 |
| Serialize_PolyTypeSourceGen | 273.6 ns | 0.56 | - | 0.00 |

#### Deserialization

| Method | Mean | Ratio | Allocated | Alloc Ratio |
|-------------------------------- |-----------:|------:|----------:|------------:|
| Deserialize_StjReflection | 1,593.0 ns | 1.00 | 1024 B | 1.00 |
| Deserialize_StjSourceGen | 1,530.3 ns | 0.96 | 1000 B | 0.98 |
| Deserialize_TypeShapeReflection | 773.1 ns | 0.49 | 440 B | 0.43 |
| Deserialize_TypeShapeSourceGen | 746.7 ns | 0.47 | 440 B | 0.43 |
| Deserialize_PolyTypeReflection | 773.1 ns | 0.49 | 440 B | 0.43 |
| Deserialize_PolyTypeSourceGen | 746.7 ns | 0.47 | 440 B | 0.43 |

Even though both serializers target the same underlying reader and writer types, the TypeShape implementation is ~75% faster for serialization and ~100% faster for deserialization, when compared with System.Text.Json's metadata serializer. As expected, fast-path serialization is still fastest since its implementation is fully inlined.
Even though both serializers target the same underlying reader and writer types, the PolyType implementation is ~75% faster for serialization and ~100% faster for deserialization, when compared with System.Text.Json's metadata serializer. As expected, fast-path serialization is still fastest since its implementation is fully inlined.

## Known libraries based on TypeShape
## Known libraries based on PolyType

The following codebases are based upon TypeShape and may be worth checking out.
The following code bases are based upon PolyType and may be worth checking out.

- [Nerdbank.MessagePack](https://github.com/AArnott/Nerdbank.MessagePack) - a MessagePack library with performance to rival MessagePack-CSharp, and greater simplicity and additional features.
* [Nerdbank.MessagePack](https://github.com/AArnott/Nerdbank.MessagePack) - a MessagePack library with performance to rival MessagePack-CSharp, and greater simplicity and additional features.

## Project structure

The repo consists of the following projects:

* The core `TypeShape` library containing:
* The [core abstractions](https://github.com/eiriktsarpalis/typeshape-csharp/tree/main/src/TypeShape/Abstractions) defining the type model.
* The [reflection provider](https://github.com/eiriktsarpalis/typeshape-csharp/tree/main/src/TypeShape/ReflectionProvider) implementation.
* The [model classes](https://github.com/eiriktsarpalis/typeshape-csharp/tree/main/src/TypeShape/SourceGenModel) used by the source generator.
* The [`TypeShape.SourceGenerator`](https://github.com/eiriktsarpalis/typeshape-csharp/tree/main/src/TypeShape.SourceGenerator) project contains the built-in source generator implementation.
* The [`TypeShape.Roslyn`](https://github.com/eiriktsarpalis/typeshape-csharp/tree/main/src/TypeShape.Roslyn) library exposes a set of components for extracting data models from Roslyn type symbols. Used as the foundation for the built-in source generator.
* [`TypeShape.Examples`](https://github.com/eiriktsarpalis/typeshape-csharp/tree/main/src/TypeShape.Examples) containing library examples:
* The core `PolyType` library containing:
* The [core abstractions](https://github.com/eiriktsarpalis/PolyType/tree/main/src/PolyType/Abstractions) defining the type model.
* The [reflection provider](https://github.com/eiriktsarpalis/PolyType/tree/main/src/PolyType/ReflectionProvider) implementation.
* The [model classes](https://github.com/eiriktsarpalis/PolyType/tree/main/src/PolyType/SourceGenModel) used by the source generator.
* The [`PolyType.SourceGenerator`](https://github.com/eiriktsarpalis/PolyType/tree/main/src/PolyType.SourceGenerator) project contains the built-in source generator implementation.
* The [`PolyType.Roslyn`](https://github.com/eiriktsarpalis/PolyType/tree/main/src/PolyType.Roslyn) library exposes a set of components for extracting data models from Roslyn type symbols. Used as the foundation for the built-in source generator.
* [`PolyType.Examples`](https://github.com/eiriktsarpalis/PolyType/tree/main/src/PolyType.Examples) containing library examples:
* A serializer built on top of System.Text.Json,
* A serializer built on top of System.Xml,
* A serializer built on top of System.Formats.Cbor,
Expand All @@ -126,4 +126,4 @@ The repo consists of the following projects:
* A structural `IEqualityComparer<T>` generator for POCOs and collections,
* An object validator in the style of System.ComponentModel.DataAnnotations.
* A simple .NET object mapper.
* The [`applications`](https://github.com/eiriktsarpalis/typeshape-csharp/tree/main/applications) folder contains sample Native AOT console applications.
* The [`applications`](https://github.com/eiriktsarpalis/PolyType/tree/main/applications) folder contains sample Native AOT console applications.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\TypeShape\TypeShape.csproj" />
<ProjectReference Include="..\..\src\TypeShape.SourceGenerator\TypeShape.SourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\..\src\TypeShape.Examples\TypeShape.Examples.csproj" />
<ProjectReference Include="..\..\src\PolyType\PolyType.csproj" />
<ProjectReference Include="..\..\src\PolyType.SourceGenerator\PolyType.SourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\..\src\PolyType.Examples\PolyType.Examples.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit 4df3a24

Please sign in to comment.