GithubHelp home page GithubHelp logo

manne / obviously Goto Github PK

View Code? Open in Web Editor NEW
7.0 3.0 3.0 161 KB

Roslyn code generator for semantic types

License: MIT License

C# 100.00%
semantic-types code-generator roslyn system-text-json microsoft-aspnetcore-mvc immutable source-generation

obviously's Introduction

Obviously

Semantic Types

NuGet GitHub license

Create semantic types in seconds

Installation

Install this NuGet package Obviously.SemanticTypes (currently in preview).

Usage

Basic

The functionality can be easily used. Declare a partial class and add the attribute SemanticType. The only parameter of this attribute is the actual type of the semantic type. Here it is string.

[SemanticType(typeof(string))]
public partial class EmailAddress { }

⚠ The class must have the partial modifier
ℹ The class can be sealed

What's getting generated

This generator creates

  • The public constructor with a single parameter of the actual type
  • The implementations of
    • the comparable and equatable pattern
    • explicit operator for the actual type.

ℹ This and the others packages are compile-time dependencies. So the compiled assembly does not contain any references on one of the NuGet packages. Even the SemanticType attribute is not in the compiled assembly

Code Generation Example

For details what is getting generated, see Docs/code_generation.md

Advanced

Validation

The input value of the constructor can be validated. Therefore a static method named IsValid has to be implemented. This method must only have a single parameter of the actual type and must have the return type bool.

If the value is not valid, an instance of the semantic type cannot be created.

Validation Example

The example shows the validation of an email address.

[SemanticType(typeof(string))]
public partial class EmailAddress
{
    public static bool IsValid(string value)
    {
        return value.Contains('@');
    }
}

Contribution

  • Create a fork and make a Pull Request
  • Submit a bug
  • Submit an idea

Credits

License

This project is licensed under the MIT License - see the MIT file for details

obviously's People

Contributors

dependabot[bot] avatar manne avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

obviously's Issues

Write is not supported?

Can you please also implement the public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options) method?

LICENSE

Please add LICENSE file to the project and license to nuget package

Immutable Converter Ignoring the Public Properties

Thanks for the excellent converter. This did solve one of my issue with de-serializing the immutable classes. But one of my class has the Constructor parameters along with the public parameters. But this converter is ignoring the public properties it is only taking care of the Constructor parameters.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.