andreas-dorfer / base-types Goto Github PK
View Code? Open in Web Editor NEWFight primitive obsession and create expressive domain models with source generators.
License: MIT License
Fight primitive obsession and create expressive domain models with source generators.
License: MIT License
There's already a way to make that work. You can specify that you want an implicit cast to the wrapped type. For example:
[DateTime, BaseType(Cast.Implicit)] public partial record MyDateTime;
That way, all the operators should work out of the box. Beware that the BaseTypeAttribute in the released version doesn't work on struct records. But that will be fixed in the next release.
Originally posted by @Andreas-Dorfer in #13 (comment)
Generate TryParse (when possible) to support use in minimal APIs.
I want to light up this library, open many PRs and use it in production. A discord server for base-types would be handy.
Add XML comments to the generated methods.
Emit helpful compiler warnings when something went wrong.
Make generated casts configurable via BaseTypeAttribute
:
Implement IParsable if the wrapped type implements IParsable.
Would be cool to be able to do something like this:
Example interface:
public interface IOperators<in TSelf, in TOther, out TResult>
where TSelf : IOperators<TSelf, TOther,TResult>
{
static abstract TResult operator ==(TSelf self, TOther other);
static abstract TResult operator ==(TOther self, TSelf other);
static abstract TResult operator !=(TSelf self, TOther other);
static abstract TResult operator !=(TOther self, TSelf other);
}
For struct implementation:
public interface IOperators<TSelf, TOther, out TResult>
where TSelf : IOperators<TSelf, TOther,TResult>
{
static abstract TResult operator ==(in TSelf self, in TOther other);
static abstract TResult operator ==(in TOther self,in TSelf other);
static abstract TResult operator !=(in TSelf self,in TOther other);
static abstract TResult operator !=(in TOther self,in TSelf other);
}
Build with GitHub Actions.
Some of the latest changes aren't reflected in README yet.
Implement IBaseType
explicitly.
Simplify static base type validations with a source generator.
Only implement IComparable when the wrapped type implements IComparable.
Add .editorconfig
Hello!
I was working on a PR myself for this, but I have been stumped since byte[] and BitArray do not implement the IComparable interface.
You know your library much better than I do - do you have any advice how an implementation for this can be done?
My thoughts are to:
byte[]
byte[]
to the wrapper typeWhat do you think? Any advice? Im struggling with the wrapper type being convertible to your IBaseType
in the code
Can you point me in the right direction with solving this?
public class BitArrayComparable : IComparable<byte[]>
{
private readonly byte[] _bitArray;
public BitArrayComparable(byte[] bitArray)
{
this._bitArray = bitArray;
}
/// <inheritdoc />
public int CompareTo(byte[]? other)
{
var len = Math.Min(this._bitArray.Length, other.Length);
for (var i = 0; i < len; i++)
{
var c = this._bitArray[i]
.CompareTo(other[i]);
if (c != 0)
{
return c;
}
}
return other.Length.CompareTo(other.Length);
}
}
I had something like this, but its not convertible to IBaseType
Thanks
One of the advantages of this library is to reduce the amount of boiler plate required to support value objects. It is common to use the From
pattern for object creation as it is shorter then Create
. Here are some libraries using this pattern :
Here is a simple PR : #27
In release mode projects uses a package reference and crashes with the error Using the generic type 'IBaseType<TWrapped>' requires 1 type arguments
.
<ItemGroup Condition="'$(Configuration)'!='Debug'">
<PackageReference Include="AndreasDorfer.BaseTypes.Core" Version="1.4.0" />
</ItemGroup>
<ItemGroup Condition="'$(Configuration)'=='Debug'">
<ProjectReference Include="..\AD.BaseTypes.Core\AD.BaseTypes.Core.csproj" />
</ItemGroup>
Publishing a branch relying on a nuget package still not built is supposed to work? The use of package references in release looks strange, maybe you can explain. Thanks
I'm looking for ways to remove the memory allocation caused by attributes in the generated classes.
[Years(1990, 1999)] partial record SomeWeekendInThe90s;
...
public SomeWeekendInThe90s(DateTime value)
{
new YearsAttribute(1990, 1999).Validate(value);
this.value = value;
}
What we need to know is what validation function to call with what values as parameters.
Maybe we could use some static interface with a convention to route parameters from the attribute to the validation function.
[AttributeUsage(AttributeTargets.Class)]
public class MaxLengthStringAttribute : Attribute
{
public MaxLengthStringAttribute(int maxLength) { }
public static void Validate(string value, int maxLength) => StringValidation.MaxLength(maxLength, value);
}
Then it could generate something like
[MaxLengthString(25)] partial record Name;
...
public Name(string value)
{
MaxLengthStringAttribute.Validate(value, 25);
this.value = value;
}
Is there the possibility to support record structs?
Great library!
Would love to upgrade it to net7
- if I have some time I can make a pull request - if you beat me to it thats great too.
You know the library better than I do, I have to examine the F# and other projects you have.
If you want - you can target both net6 and net7 like this:
Source Example Meziantou.Framework/Directory.Build.props
<PropertyGroup>
<LatestTargetFrameworkLts>net6.0</LatestTargetFrameworkLts>
<LatestTargetFramework>net7.0</LatestTargetFramework>
<LatestTargetFrameworkPreview></LatestTargetFrameworkPreview>
<LatestTargetFrameworkLtsWindows>$(LatestTargetFrameworkLts)-windows</LatestTargetFrameworkLtsWindows>
<LatestTargetFrameworkWindows>$(LatestTargetFramework)-windows</LatestTargetFrameworkWindows>
<LatestTargetFrameworkPreviewWindows Condition="'$(LatestTargetFrameworkPreview)' != ''">$(LatestTargetFrameworkPreview)-windows</LatestTargetFrameworkPreviewWindows>
<LatestTargetFrameworks>$(LatestTargetFrameworkPreview);$(LatestTargetFramework);$(LatestTargetFrameworkLts)</LatestTargetFrameworks>
<LatestTargetFrameworksWindows>$(LatestTargetFrameworkPreviewWindows);$(LatestTargetFrameworkWindows);$(LatestTargetFrameworkLtsWindows)</LatestTargetFrameworksWindows>
<SuppressNETCoreSdkPreviewMessage>true</SuppressNETCoreSdkPreviewMessage>
</PropertyGroup>
Something like that
Very cool library! I have tried many 'base type' libraries, and I love how your validation works.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.