Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add source generator with basic attribute generation #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

erictuvesson
Copy link
Member

Input:

namespace Mallos.Searchable.Test
{
    using Mallos.Searchable.Attributes;
    using System;

    [FilterGenerator("MyGeneratedSearchable")]
    public class TestObject
    {
        [FilterKey("v"), FilterStringMatch(FilterMatchType.Match)]
        [FilterKey("value"), FilterStringMatch(FilterMatchType.Contains, StringComparison.OrdinalIgnoreCase)]
        public string Value { get; set; }

        [FilterKey("key"), FilterStringMatch()]
        public string Key { get; set; }

        public TestObject(string value)
        {
            this.Value = value;
        }
    }
}

Generated Output:

using Mallos.Searchable;
using Mallos.Searchable.Attributes;
using System;
using System.Collections.Generic;
using Mallos.Searchable.Test;

public class MyGeneratedSearchableFilter_FilterStringMatch_v : SimpleFilter<TestObject> 
{
    public override string Key => "v";

    protected override bool Check(TestObject item, string value)
    {
        return Mallos.Searchable.Attributes.FilterStringMatchAttribute.Execute(
            item.Value,
            value,
            FilterMatchType.Match
        );
    }
}

public class MyGeneratedSearchableFilter_FilterStringMatch_value : SimpleFilter<TestObject> 
{
    public override string Key => "value";

    protected override bool Check(TestObject item, string value)
    {
        return Mallos.Searchable.Attributes.FilterStringMatchAttribute.Execute(
            item.Value,
            value,
            FilterMatchType.Contains,
            StringComparison.OrdinalIgnoreCase
        );
    }
}

public class MyGeneratedSearchableFilter_FilterStringMatch_key : SimpleFilter<TestObject> 
{
    public override string Key => "key";

    protected override bool Check(TestObject item, string value)
    {
        return Mallos.Searchable.Attributes.FilterStringMatchAttribute.Execute(
            item.Key,
            value
        );
    }
}

public class MyGeneratedSearchable : Searchable<TestObject> 
{
    public MyGeneratedSearchable()
    {
        Filters.Add(new MyGeneratedSearchableFilter_FilterStringMatch_v());
        Filters.Add(new MyGeneratedSearchableFilter_FilterStringMatch_value());
        Filters.Add(new MyGeneratedSearchableFilter_FilterStringMatch_key());
    }

    protected override IEnumerable<TestObject> FreeTextFilter(
        IEnumerable<TestObject> values, bool negative, string text)
    {
        return base.FreeTextFilter(values, negative, text);
    }
}

<TargetFramework>netstandard2.1</TargetFramework>
<TargetFramework>netstandard2.0</TargetFramework>
Copy link
Member Author

@erictuvesson erictuvesson Apr 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have to solve the issue where the Mallos.Searchable.CodeAnalyzer project can reference this by only netstandard2.0.

I would like to add .NET 5 support here too, to be able to work with records.


public SearchableSyntaxReceiver()
{
this.filterAttributes = ReflectionHelper.AllOfType<FilterBaseAttribute>().ToList();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to be able to remove this, then we don't have to depend on Mallos.Searchable project.

But how can it work without using Reflection if we don't know more about the syntax?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant