https://github.com/ktsu-dev/semanticstring
https://github.com/ktsu-dev/semanticstring
Last synced: about 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/ktsu-dev/semanticstring
- Owner: ktsu-dev
- License: mit
- Created: 2024-12-24T04:52:42.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-25T06:02:32.000Z (about 1 year ago)
- Last Synced: 2025-03-25T07:19:35.501Z (about 1 year ago)
- Language: C#
- Size: 120 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
- Authors: AUTHORS.md
Awesome Lists containing this project
README
# SemanticString
A library that provides a transparent wrapper around system strings and gives you strong typing, compile time feedback, and runtime validation.
The intention is to be able to provide usage context and validation to naked strings in a similar way that Enums do for integers.
## Usage
```csharp
using ktsu.Semantics;
// Create a strong type by deriving a record class from SemanticString:
public record class MyStrongString : SemanticString { }
public class MyDemoClass
{
public MyStrongString Stronk { get; set; } = new();
public static void Demo(MyStrongString inStrongString, string inSystemString, out MyStrongString outStrongString, out string outSystemString)
{
// You can implicitly cast down to a System.String
outSystemString = inStrongString;
// You must explicitly cast up to a StrongString
outStrongString = (MyStrongString)inSystemString;
//You can provide a StrongString to a method that expects a System.String
Path.Combine(inStrongString, inSystemString);
// You can use the .WeakString property or the .ToString() method to get the value of the underlying System.String
outSystemString = inStrongString.WeakString;
outSystemString = inStrongString.ToString();
// You can not implicitly cast up to a StrongString
// outStrongString = inSystemString; // This will not compile
// You can not cast from one StrongString to another
// OtherStrongString other = inStrongString; // This will not compile
// OtherStrongString other = (OtherStrongString)inStrongString; // This will not compile either
}
}
```
## Validation
You can provide custom validators which will throw a `FormatException` at runtime to help you catch data errors.
Implement the `ktsu.Semantics.ISemanticStringValidator` interface and provide it as a generic parameter when deriving your class:
```csharp
public abstract class StartsWithHttp : ISemanticStringValidator
{
public static bool IsValid(SemanticString? semanticString)
{
ArgumentNullException.ThrowIfNull(semanticString);
return semanticString.StartsWith("http", StringComparison.InvariantCultureIgnoreCase);
}
}
public abstract class EndsWithDotCom : ISemanticStringValidator
{
public static bool IsValid(SemanticString? semanticString)
{
ArgumentNullException.ThrowIfNull(semanticString);
return semanticString.EndsWith(".com", StringComparison.InvariantCultureIgnoreCase);
}
}
public record class MyValidatedString : SemanticString { }
```