Skip to content

Changed custom pattern handling to not silently ignore errors and other minor improvements #53

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

Merged
merged 2 commits into from
Mar 24, 2022

Conversation

sandreas
Copy link
Contributor

@sandreas sandreas commented Mar 12, 2022

Hey there,

here is the PR you mentioned in #52.

Some notes:

  • I added a constructor for adding custom patterns by IEnumerable<(string, string)>, because I thought Stream is not neccessary in all the use cases
  • I noticed, that the default constructor loads custom patterns from a manifest file and added a comment, wether this is a good idea
  • While the PatternViolations list I added might be a quick fix for the silently ignores errors problem, I'm not sure, that this is a good design decision in the long term. I would think over an API redesign calling Grok statically with a fluent interface removing all logic from the constructor... Maybe there is also room for using async/await. Example see below (just an idea, maybe we should discuss that as soon as you have more time):
public class Grok
{
    // I prefer IEnumberable over Dictionary because of thread safety
    private IEnumerable<(string, string)> _patterns;

    private Grok(string definition)
    {
        // ...
    }

    public static Grok Define(string definition)
    {
        EnsureDefintionIsValid(definition);
        return new Grok(definition);
    }

    public Grok WithPatterns(IEnumerable<(string, string)> patterns)
    {
        var patternsArray = patterns.ToArray();
        EnsurePatternsAreValid(patternsArray);
        _patterns = patternsArray;
        return this;
    }

    private void EnsurePatternsAreValid(IEnumerable<(string, string)> patterns)
    {
        // ...
    }

    private static void EnsureDefintionIsValid(string definition)
    {
        // ...
    }

    public bool TryMatch(string subject, out IEnumerable<GrokItem> matches)
    {
        matches = new List<GrokItem>();
        // ...
        return true;
    }
}

try {
  var matcher = Grok.Define("%{ZIPCODE:zipcode}:%{EMAILADDRESS:email}").WithPatterns(new[] {
      ("ZIPCODE", "[1-9]{1}[0-9]{2}\\s{0,1}[0-9]{3}"), 
      ("FLOAT", "[+-]?([0-9]*[.,])?[0-9]+"),
  });
  
  if(matcher.TryMatch("122 001:[email protected]", out var matches)) {
    Console.WriteLine("Matches found: " + matches.Count);
  } else {
    Console.WriteLine("No matches found");
  };
} catch(GrokDefinitionException e) {
  Console.WriteLine("Invalid definition: " + e.Message);
} catch(GrokPatternException pe) {
  Console.WriteLine("Invalid custom pattern: " + e.Message);
}

@Marusyk Marusyk self-assigned this Mar 13, 2022
@Marusyk Marusyk linked an issue Mar 13, 2022 that may be closed by this pull request
Copy link
Owner

@Marusyk Marusyk left a comment

Choose a reason for hiding this comment

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

Many thanks for your contribution and interest in the project.

  • The new constructor will be a nice feature
  • The constructor loads default patterns (not custom, from custom file), it is an important requirement and must be done automatically during the Grok initialization (in order not to force the user/consumer to do it - dangerous - there is no guarantee that the user will do this)
  • PatternViolations concept will not fix the issue with ignoring errors, but adds more complexity and, again, requires additional operations from users
  • With what actions can you use async/await here?
  • A fluent interface is a good idea and can be designed however I'd avoid any static

Copy link
Owner

@Marusyk Marusyk left a comment

Choose a reason for hiding this comment

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

👍 LGTM
Great job! Thank you

@Marusyk Marusyk merged commit 5299637 into Marusyk:main Mar 24, 2022
@ealvesss
Copy link

Any help for first issue?

@Marusyk
Copy link
Owner

Marusyk commented Sep 28, 2022

Hi @EltonAlvess,
what do you mean? This is not an issue, it's a pull request that has already been merged

@Marusyk Marusyk added this to the 1.2.0 milestone Aug 21, 2023
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.

Invalid custom patterns are silently ignored
3 participants