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

Could json logic operators be protected interal? #368

Closed
dcook-net opened this issue Jan 19, 2023 · 5 comments · Fixed by #369 or #370
Closed

Could json logic operators be protected interal? #368

dcook-net opened this issue Jan 19, 2023 · 5 comments · Fixed by #369 or #370
Labels
enhancement New feature in an existing library pkg:logic

Comments

@dcook-net
Copy link

Hi,

I was wondering if there was a technical reason for marking the Json Logic operators as internal?

We are trying to extend the library a little for internal use, and would like to override the behaviour of several comparison operators in certain circumstances, so that if the data being operated on meets a certain condition, ie, it looks like a Date, then we perform our own comparison, otherwise, we defer to the code that already exists in say <, >, =>, =< for example.

Currently, we can't inherit from or instansiate these operators, without maybe resorting to reflection.

Would you be open to the idea of changing the access modifiers on operators, and thier fields to protected internal?

I can raise a PR for this work if it's something that works for you.
If this is not desirable for you, can you suggest an alternative?

For reference, here is a sample of what we are trying to achieve;

[Operator(">")]
public class CustomMoreThanRule : Json.Logic.Rules.MoreThanRule
{
     public CustomMoreThanRule(Rule a, Rule b) : base(a, b)
     {
     }

     public override JsonNode? Apply(JsonNode? data, JsonNode? contextData = null)
     {
         var a = A.Apply(data, contextData);
         var b = B.Apply(data, contextData);

         // ignoring for now what happens when only one is a date!
         if (a.IsDateValue() && b.IsDateValue())
             return CompareDates(a, b);

         return base.Apply(data, contextData);
    }
}

Thanks,
Dave

@dcook-net dcook-net added the question Further information is requested label Jan 19, 2023
@gregsdennis
Copy link
Owner

gregsdennis commented Jan 19, 2023

I was wondering if there was a technical reason for marking the Json Logic operators as internal?

It's more a philisophical reason. The primary mechanism for creating custom behavior is through defining custom rules. If you register an operation that already exists, it'll overwrite with your rule.

However, I can see an argument for wanting to actually override the rule class in order to take advantage of the default behavior except for certain cases.

I was going to suggest a composition approach for now, but that requires that you be able to access the MoreThanRule constructor, which is also internal.

Yeah, I think internal protected is a good option. Give me a couple days, and I'll have an update out.

@gregsdennis gregsdennis added enhancement New feature in an existing library and removed question Further information is requested labels Jan 19, 2023
@dcook-net
Copy link
Author

I agree that composition approach would be preferable, though that would require making the classes and their constructors public.
You could also enforce composition over inheritance by sealing the operator classes.

@dcook-net
Copy link
Author

Created a PR for the internal protected approach.
Happy to apply changes if this isn't what you were thinking.

Cheers,
Dave

@gregsdennis
Copy link
Owner

Thanks for the PR. I'll have a look over it today.

I agree that composition approach would be preferable

My philosophical objection wasn't so much one of "composition over inheritance" as these operations are well-defined. Making an operation do something else isn't interoperable. However, you do provide a good use case, and so long as you understand the interoperability implications of your change, I shouldn't prevent you from making it.

@dcook-net
Copy link
Author

Ah, I see whay you mean. Yes, I agree.

I've added to the docs to explain those implications.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature in an existing library pkg:logic
Projects
None yet
2 participants