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

Extended defineTypes with filters #126

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

WesleyClements
Copy link

@WesleyClements WesleyClements commented Mar 18, 2022

Just a proposal for extending defineTypes to allow for the definition of filters.
This is really just to continue the work to make working with the annotation in JS more user friendly.
Fundamentally all I did was make it possible to pass in a config object in addition to a DefinitionType.
This would allow changing this code

class SubState extends Schema {
}
class State extends Schema {
}

defineTypes(State , {
  sensitiveString: "string",
  sensitiveSubSchema: SubState ,
  sensitiveArray: ["string"],
  sensitiveMap: { map: "string" },
  sensitiveSet: { set: "string" },
});

filter(function() { /* ... */ })(State .prototype, "sensitiveString");
filter(function() { /* ... */ })(State .prototype, "sensitiveSubSchema");
filter(function() { /* ... */ })(State .prototype, "sensitiveArray");
filterChildren(function() { /* ... */ })(State .prototype, "sensitiveArray");
filter(function() { /* ... */ })(State .prototype, "sensitiveMap");
filterChildren(function() { /* ... */ })(State .prototype, "sensitiveMap");
filter(function() { /* ... */ })(State .prototype, "sensitiveSet");
filterChildren(function() { /* ... */ })(State .prototype, "sensitiveSet");

into this

class SubState extends Schema {
}
class State extends Schema {
}

defineTypes(State , {
  sensitiveString: {
    type: "string",
    filter() { /* ... */ },
  },
  sensitiveSubSchema: {
    type: SubState,
    filter() { /* ... */ },
  },
  sensitiveArray: {
    type: ["string"],
    filter() { /* ... */ },
    filterChildren() { /* ... */ },
  },
  sensitiveMap: {
    type: : { map: "string" },
    filter() { /* ... */ },
    filterChildren() { /* ... */ },
  },
  sensitiveSet: {
    type: { Set: "string" },
    filter() { /* ... */ },
    filterChildren() { /* ... */ },
  },
});

I had to split up annotations.ts in order to appease codeclimate's max file length.
I tired to handle it in a way that reduced impact to the rest of the codebase.

@WesleyClements WesleyClements marked this pull request as ready for review March 18, 2022 06:56
@WesleyClements WesleyClements marked this pull request as draft March 18, 2022 15:54
@WesleyClements WesleyClements marked this pull request as ready for review March 31, 2022 21:51
@@ -122,7 +122,7 @@ export abstract class Schema {
console.error(e);
}

static is(type: DefinitionType) {
static is(type: DefinitionType): type is typeof Schema {
Copy link
Author

Choose a reason for hiding this comment

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

Not related but useful for type inference

return klass._context && klass._context.useFilters;
}

function applyFilter(addFilter:(definition: SchemaDefinition, field: string) => boolean) {
Copy link
Author

Choose a reason for hiding this comment

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

codeclimate flagged filter and filterChildren as potentially duplicate code so I refactored to appease it.

commit 156135e
Author: Wesley Clements <wesley.clements@gmail.com>
Date:   Thu Mar 31 18:02:06 2022 -0400

    reverted change

commit 5cb4f84
Author: Wesley Clements <wesley.clements@gmail.com>
Date:   Thu Mar 31 18:01:30 2022 -0400

    removed auxiliary changes

commit fc129ce
Author: Wesley Clements <wesley.clements@gmail.com>
Date:   Thu Mar 31 16:59:30 2022 -0400

    updated to include match MapSchema

commit 24cf717
Author: Wesley Clements <wesley.clements@gmail.com>
Date:   Thu Mar 31 16:58:19 2022 -0400

    simplified exports

commit ea383d0
Author: Wesley Clements <wesley.clements@gmail.com>
Date:   Thu Mar 31 16:56:35 2022 -0400

    added new line at end of file

commit e23ceae
Author: Wesley Clements <wesley.clements@gmail.com>
Date:   Thu Mar 31 16:56:11 2022 -0400

    reverted change

commit 93d99af
Author: Wesley Clements <wesley.clements@gmail.com>
Date:   Thu Mar 31 16:54:25 2022 -0400

    updated exports to fix tests

commit 58a7aa1
Author: Wesley Clements <wesley.clements@gmail.com>
Date:   Thu Mar 31 16:51:29 2022 -0400

    reverted changes

commit 6333d60
Author: Wesley Clements <wesley.clements@gmail.com>
Date:   Thu Mar 31 16:50:19 2022 -0400

    removed unused type

commit b11f201
Author: Wesley Clements <wesley.clements@gmail.com>
Date:   Thu Mar 31 16:49:59 2022 -0400

    fixed typing conflicting with function

commit c9d6e1a
Author: Wesley Clements <wesley.clements@gmail.com>
Date:   Thu Mar 31 16:48:57 2022 -0400

    reverted changes

commit fc84e37
Author: Wesley Clements <wesley.clements@gmail.com>
Date:   Thu Mar 31 16:48:27 2022 -0400

    moved types around to better reflect domains

commit 7c0fc9f
Author: Wesley Clements <wesley.clements@gmail.com>
Date:   Thu Mar 31 16:40:00 2022 -0400

    refactored to reduce file bloat and impact on other files

commit ac43f0f
Author: Wesley Clements <wesley.clements@gmail.com>
Date:   Thu Mar 31 15:21:49 2022 -0400

    refactored to use optional chaining

commit e0df843
Author: Wesley Clements <wesley.clements@gmail.com>
Date:   Thu Mar 31 14:51:53 2022 -0400

    split annotations file into separate files by type

commit e3e69c0
Author: Wesley Clements <wesley.clements@gmail.com>
Date:   Thu Mar 31 14:21:35 2022 -0400

    simplified types and implementation

commit 8dfe1f0
Author: Wesley Clements <wesley.clements@gmail.com>
Date:   Tue Mar 29 20:40:55 2022 -0400

    changed to use Schema.is

commit c7f0300
Author: Wesley Clements <wesley.clements@gmail.com>
Date:   Tue Mar 29 20:39:38 2022 -0400

    reverted "fix"

commit 4747b73
Author: Wesley Clements <wesley.clements@gmail.com>
Date:   Tue Mar 29 20:34:52 2022 -0400

    fixed isPrototypeOf

commit d723bbc
Author: Wesley Clements <wesley.clements@gmail.com>
Date:   Tue Mar 29 20:15:29 2022 -0400

    simplified implementation

commit cd7847b
Author: Wesley Clements <wesley.clements@gmail.com>
Date:   Fri Mar 18 03:00:30 2022 -0400

    added defaults for generics

commit 8c0c52a
Author: Wesley Clements <wesley.clements@gmail.com>
Date:   Fri Mar 18 02:53:15 2022 -0400

    updated exports

commit 9f06c01
Author: Wesley Clements <wesley.clements@gmail.com>
Date:   Fri Mar 18 02:53:00 2022 -0400

    refactored to address file length issue

commit 706fb8e
Author: Wesley Clements <wesley.clements@gmail.com>
Date:   Fri Mar 18 02:00:31 2022 -0400

    fixed strict equality for null
@WesleyClements WesleyClements force-pushed the extended-defineTypes-with-filters branch from 156135e to 8dd3160 Compare March 31, 2022 22:31
@WesleyClements WesleyClements marked this pull request as draft March 31, 2022 22:33
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.

None yet

1 participant