Skip to content

Commit

Permalink
chore: code moved from the TagBites.UI repository
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszmalik committed Jan 17, 2024
0 parents commit 72904e1
Show file tree
Hide file tree
Showing 25 changed files with 3,880 additions and 0 deletions.
48 changes: 48 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# top-most EditorConfig file
root = true

# charset
[*]
charset = utf-8

# lines
[*]
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
max_line_length = off

# tabs
[*]
indent_style = space
indent_size = 4
tab_width = 4

[*.{csproj,props,resx,targets}]
indent_style = space
indent_size = 2
tab_width = 2

# rules
# CA2225: Operator overloads have named alternates
dotnet_diagnostic.CA2225.severity = none
# CA1716: Identifiers should not match keywords
dotnet_diagnostic.CA1716.severity = none
# CA1054: URI-like parameters should not be strings
dotnet_diagnostic.CA1054.severity = none
# CA1056: URI-like properties should not be strings
dotnet_diagnostic.CA1056.severity = none
# CA1062: Validate arguments of public methods
dotnet_diagnostic.CA1062.severity = none
# CA1033: Interface methods should be callable by child types
dotnet_diagnostic.CA1033.severity = none
# CA1034: Nested types should not be visible
dotnet_diagnostic.CA1034.severity = none
# CA1308: Normalize strings to uppercase
dotnet_diagnostic.CA1308.severity = none
# CA1805: Do not initialize unnecessarily
dotnet_diagnostic.CA1805.severity = none
# CA1031: Do not catch general exception types
dotnet_diagnostic.CA1031.severity = none
# CA1714: Flags enums should have plural names
dotnet_diagnostic.CA1714.severity = none
19 changes: 19 additions & 0 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: build & test

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build-and-test:
runs-on: ubuntu-latest
timeout-minutes: 15

steps:
- uses: actions/checkout@v2
- uses: TagBites/actions/dotnet-build@master
with:
solution: TagBites.Expressions.sln
- uses: TagBites/actions/dotnet-test@master
21 changes: 21 additions & 0 deletions .github/workflows/publish-preview.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: publish preview

on:
push:
tags:
- "v?[0-9]+.[0-9]+.[0-9]+-preview.[0-9]+"

jobs:
publish-preview:
runs-on: ubuntu-latest
timeout-minutes: 15

steps:
- uses: actions/checkout@v2
- uses: TagBites/actions/dotnet-build@master
with:
solution: TagBites.Expressions.sln
- uses: TagBites/actions/nuget-publish@master
with:
nuget-source: "https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json"
nuget-key: "${{ secrets.GITHUB_TOKEN }}"
21 changes: 21 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: publish

on:
push:
tags:
- "v?[0-9]+.[0-9]+.[0-9]+"

jobs:
publish:
runs-on: ubuntu-latest
timeout-minutes: 15

steps:
- uses: actions/checkout@v2
- uses: TagBites/actions/dotnet-build@master
with:
solution: TagBites.Expressions.sln
- uses: TagBites/actions/nuget-publish@master
with:
nuget-source: "https://api.nuget.org/v3/index.json"
nuget-key: "${{ secrets.NUGET_KEY }}"
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# User-specific files
*.DotSettings
*.user

# Folders
/Build/keys
/Build/*.exe
/Build/*.dll
/Build/*.nupkg
/TestResults
.vs/
packages/
[Bb]in/
[Oo]bj/
63 changes: 63 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<Project>

<!-- Company/Copyright -->
<PropertyGroup>
<Company>Tag Bites sp. z o.o.</Company>
<Authors>Tag Bites sp. z o.o.</Authors>
<CopyrightSinceYear>2012</CopyrightSinceYear>

<Copyright>© $(CopyrightSinceYear)-$([System.DateTime]::Today.ToString(`yyyy`)) $(Company)</Copyright>
</PropertyGroup>

<!-- Bin/Obj directories -->
<PropertyGroup>
<BaseOutputPath>$(SolutionDir)bin\$(MSBuildProjectName)\</BaseOutputPath>
<PackageOutputPath>$(SolutionDir)bin\</PackageOutputPath>
<BaseIntermediateOutputPath>$(SolutionDir)bin\obj\$(MSBuildProjectName)\</BaseIntermediateOutputPath>
</PropertyGroup>

<!-- Compilation settings -->
<PropertyGroup>
<LangVersion>latest</LangVersion>
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
<Deterministic>true</Deterministic>
<Nullable>enable</Nullable>
</PropertyGroup>

<!-- Documentation -->
<PropertyGroup>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

<!-- Warnings -->
<PropertyGroup>
<NoWarn>1701;1702;1591;NU5048;NU5125;IDE0290</NoWarn>
</PropertyGroup>

<!-- Globalization -->
<PropertyGroup>
<NeutralLanguage>en-US</NeutralLanguage>
</PropertyGroup>

<!-- Ignored files/folders -->
<PropertyGroup>
<DefaultItemExcludes>$(DefaultItemExcludes);*.csproj.DotSettings</DefaultItemExcludes>
</PropertyGroup>

<!-- Usings -->
<ItemGroup>
<Using Include="System" />
<Using Include="System.Collections" />
<Using Include="System.Collections.Generic" />
<Using Include="System.IO" />
<Using Include="System.Linq" />
<Using Include="System.Threading" />
<Using Include="System.Threading.Tasks" />
<Using Include="JetBrains.Annotations" />
</ItemGroup>

<!-- Includes -->
<Import Condition="exists('Version.props')" Project="Version.props" />
<Import Condition="exists('Package.props')" Project="Package.props" />

</Project>
21 changes: 21 additions & 0 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project>

<ItemGroup>
<PackageReference Include="MinVer" Version="4.3.0">
<PrivateAssets>All</PrivateAssets>
</PackageReference>
</ItemGroup>

<!-- Copy package to local packages repository -->
<Target Name="CopyPackage" AfterTargets="Pack" Condition="exists('$(MSBuildThisFileDirectory)..\LocalPackages\')">
<Message Text="Copy '$(PackageOutputPath)$(PackageId).$(PackageVersion).nupkg' to '$(MSBuildThisFileDirectory)..\LocalPackages\'" Importance="high"/>
<Copy
SourceFiles="$(PackageOutputPath)$(PackageId).$(PackageVersion).nupkg"
DestinationFolder="$(MSBuildThisFileDirectory)..\LocalPackages\"
ContinueOnError="false" />

<Message Text="Delete local package folder 'C:\Users\$([System.Environment]::UserName)\.nuget\packages\$(PackageId)\$(PackageVersion)'" Importance="high"/>
<RemoveDir Directories="C:\Users\$([System.Environment]::UserName)\.nuget\packages\$(PackageId)\$(PackageVersion)" />
</Target>

</Project>
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Tag Bites

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
17 changes: 17 additions & 0 deletions Package.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project>
<PropertyGroup>
<Description>Converts C# text expressions into LINQ expressions using **Roslyn**, supporting complete language syntax.</Description>
<PackageTags>LINQ;expressions;Roslyn;csharp</PackageTags>

<PackageProjectUrl>https://github.com/TagBites/TagBites.Expressions</PackageProjectUrl>
<PackageIconUrl></PackageIconUrl>

<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageLicenseExpression>MIT</PackageLicenseExpression>

<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/TagBites/TagBites.Expressions.git</RepositoryUrl>

<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>
</Project>
86 changes: 86 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# TagBites.Expressions

[![Nuget](https://img.shields.io/nuget/v/TagBites.Expressions.svg)](https://www.nuget.org/packages/TagBites.Expressions/)
[![License](https://img.shields.io/github/license/TagBites/TagBites.Expressions)](https://github.com/TagBites/TagBites.Expressions/blob/master/LICENSE)

Converts C# text expressions into LINQ expressions using **Roslyn**, supporting complete language syntax.

## Example

```csharp
public void BasicUseTest()
{
var expression = "new [] { 1, 2, 3 }.Select(x => (x, x + 1).Item2).Sum()";
var func = ExpressionParser.Parse(expression, null).Compile();

Assert.Equal(9, func.DynamicInvoke());
}

public void SimpleTest()
{
var func = Parse("(a + b) / (double)b");
Assert.Equal(2.5d, func(3, 2));

func = Parse("a switch { 1 => b, 2 => b * 2, _ => b + a }");
Assert.Equal(2, func(1, 2));
Assert.Equal(4, func(2, 2));
Assert.Equal(5, func(3, 2));

static Func<int, int, double> Parse(string expression)
{
var options = new ExpressionParserOptions
{
Parameters =
{
(typeof(int), "a"),
(typeof(int), "b")
},
ResultCastType = typeof(double)
};
var lambda = ExpressionParser.Parse(expression, options);
return (Func<int, int, double>)lambda.Compile();
}
}

public void TypeTest()
{
var m = new TestModel { X = 1, Y = 2 };

var func = Parse("X + Y");
Assert.Equal(3, func(m));

func = Parse("X + Nested.X");
Assert.Equal(3, func(m));

func = Parse("X + new TestModel { X = 1, Y = 2 }.Y");
Assert.Equal(3, func(m));

func = Parse("X + (X == 1 ? Nested.X : Nested.Y)");
Assert.Equal(3, func(m));
Assert.Equal(7, func(new TestModel { X = 2, Y = 3 }));

static Func<TestModel, int> Parse(string expression)
{
var options = new ExpressionParserOptions
{
Parameters =
{
(typeof(TestModel), "this")
},
UseFirstParameterAsThis = true,
ResultType = typeof(int)
};
var lambda = ExpressionParser.Parse(expression, options);
return (Func<TestModel, int>)lambda.Compile();
}
}

private class TestModel
{
private TestModel? _nested;

public int X { get; set; }
public int Y { get; set; }
public TestModel Nested => _nested ??= new TestModel { X = Y, Y = X + Y };
}
```
49 changes: 49 additions & 0 deletions TagBites.Expressions.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.4.33205.214
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{62CB84CB-ABBE-4834-BBD3-9084940552FE}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
.gitignore = .gitignore
Directory.Build.props = Directory.Build.props
Directory.Build.targets = Directory.Build.targets
Package.props = Package.props
README.md = README.md
Version.props = Version.props
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TagBites.Expressions", "src\TagBites.Expressions\TagBites.Expressions.csproj", "{259881F0-BEA4-4FED-AA24-409352657EF7}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TagBites.Expressions.Tests", "tests\TagBites.Expressions.Tests\TagBites.Expressions.Tests.csproj", "{A0222F96-ED70-4D16-A4B6-D4407024E491}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".github", ".github", "{1870CE1A-8E9A-40F1-AA6F-5A4502F1679E}"
ProjectSection(SolutionItems) = preProject
.github\workflows\build-and-test.yml = .github\workflows\build-and-test.yml
.github\workflows\publish-preview.yml = .github\workflows\publish-preview.yml
.github\workflows\publish.yml = .github\workflows\publish.yml
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{259881F0-BEA4-4FED-AA24-409352657EF7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{259881F0-BEA4-4FED-AA24-409352657EF7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{259881F0-BEA4-4FED-AA24-409352657EF7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{259881F0-BEA4-4FED-AA24-409352657EF7}.Release|Any CPU.Build.0 = Release|Any CPU
{A0222F96-ED70-4D16-A4B6-D4407024E491}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A0222F96-ED70-4D16-A4B6-D4407024E491}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A0222F96-ED70-4D16-A4B6-D4407024E491}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A0222F96-ED70-4D16-A4B6-D4407024E491}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4C4ED72A-E0F0-44B5-8F52-EBB27C7E235B}
EndGlobalSection
EndGlobal
Loading

0 comments on commit 72904e1

Please sign in to comment.