Skip to content
This repository has been archived by the owner on Jan 2, 2023. It is now read-only.

Provide regex pattern shortcuts to help users define cleaner models #20

Open
aymanfarhat opened this issue May 25, 2020 · 1 comment
Open
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@aymanfarhat
Copy link
Member

Defining larger models with several regex patterns with lots of repetition can become a maintenance hell for users. Especially that many patterns are usually common ones such as URLs, numbers only, negative numbers etc...

Was thinking, it would be cool to have a pre-defined set of patterns in the lib to allow users to do shortcuts when defining models. Example:

Instead of having to define something like this as a model:

  const baseEntryPattern = {
    pageref: /(?<pageRef>.*)/,
    startedDateTime: /(?<startedDateTime>.*)/,
    request: {
      method: /(?<requestMethod>GET|POST)/,
      url: /(?<requestUrl>[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*))/,
      httpVersion: /(?<requestHttpVersion>.*)/,
      headersSize: /^(?<requestHeaderSize>\-?(\d+\.?\d*|\d*\.?\d+))$/,
      bodySize: /^(?<requestHeaderSize>\-?(\d+\.?\d*|\d*\.?\d+))$/,
    },
    response: {
      status: /^(?<responseStatus>[0-9]{3})/,
      content: {
        size: /^(?<responseContentSize>\-?(\d+\.?\d*|\d*\.?\d+))$/,
      },
      headers: [
        { name: /content-type/i, value: /(?<responseContentType>.*)/ },
        { name: /content-length/i, value: /(?<responseContentLength>.*)/ },
        { name: /cache-control/i, value: /(?<responseCacheControl>.*)/ },
      ]
    },
    timings: (val) => val,
    time: /^(?<time>\-?(\d+\.?\d*|\d*\.?\d+))$/
  };

Would be cleaner to (optionally) use a helper method in the module to define common patterns this way:

  const baseEntryPattern = {
    pageref: Objectron.patterns.any(),
    startedDateTime: Objectron.patterns.any('startDateTime'),
    request: {
      method: /(?<requestMethod>GET|POST)/,
      url: Objectron.patterns.url('requestUrl'),
      httpVersion: Objectron.patterns.any('httpVersionName'),
    },
    response: {
      status: /^(?<responseStatus>[0-9]{3})/,
      content: {
        size: Objectron.patterns.anyNumber('responseContentSize')
      },
    },
    timings: (val) => val,
    time: /^(?<time>\-?(\d+\.?\d*|\d*\.?\d+))$/
  };

Can save lots of effort in maintaining and re-implementing common patterns by the user especially that as the model gets more complex, repeated patterns become confusing and error prone.This could potentially be achieved via implementing a set of methods to generate regex expressions based on pre-defined patterns.

What do you think?

@aymanfarhat aymanfarhat added the enhancement New feature or request label May 25, 2020
@Link-
Copy link
Member

Link- commented May 26, 2020

Loving the contributions @aymanfarhat keep em coming for sure. Yeah it'll definitely be cool to define helper matchers that are extendable and we provide a default set off the shelf. Doesn't cost much to put it in place also.

@Link- Link- added the help wanted Extra attention is needed label Oct 4, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants