Skip to content

feat(@schematics/angular): Added multiselect guard implements option #13109

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 1 commit into from
Jan 15, 2019

Conversation

okeefem2
Copy link
Contributor

@okeefem2 okeefem2 commented Dec 1, 2018

Follow up to #13031, using the multiselect option for the guard schematic to ask the user which methods they would like to implement. I purposefully left out CanDeactivate because of the need to tie in a component. This would be a good candidate for a future PR though!

Fixes #13400

Thanks,

Michael

@mgechev mgechev added the needs: discussion On the agenda for team meeting to determine next steps label Dec 20, 2018
@mgechev mgechev requested review from hansl and clydin January 3, 2019 18:32
@mgechev mgechev removed the needs: discussion On the agenda for team meeting to determine next steps label Jan 3, 2019
@clydin clydin added the target: major This PR is targeted for the next major release label Jan 3, 2019
Copy link
Member

@clydin clydin left a comment

Choose a reason for hiding this comment

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

I apologize for the delay in reviewing. Looks good; just several comments.

"type": "list",
"multiselect": true,
"items": [
{ "value": "CanActivate", "label": "CanActivate" },
Copy link
Member

Choose a reason for hiding this comment

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

You can remove the label. The label is optional and will use the value if not present.

@okeefem2 okeefem2 force-pushed the feat-guard-schematic-multiselect branch from 2e747a7 to 39a36e7 Compare January 9, 2019 03:17
@okeefem2
Copy link
Contributor Author

okeefem2 commented Jan 9, 2019

@clydin Thanks for the review! I have pushed changes, let me know if any more are necessary.

Copy link
Member

@clydin clydin left a comment

Choose a reason for hiding this comment

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

Looks good.
Just two small suggestions.

@alan-agius4
Copy link
Collaborator

Can you add fixes #13400?

Thanks

@okeefem2 okeefem2 force-pushed the feat-guard-schematic-multiselect branch 2 times, most recently from 08baa85 to 219a6ec Compare January 10, 2019 13:00
"description": "Specifies which interfaces to implement.",
"uniqueItems": true,
"items": {
"type": "string"
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks like an enum instead of general string.

Copy link
Contributor Author

@okeefem2 okeefem2 Jan 15, 2019

Choose a reason for hiding this comment

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

@hansl If I change this to enum, I get the following build error:

Error: Invalid type enum in JSON Schema at Schema#/properties/implements/items

I realized that the way this is being used the items could be updated to just a list of strings, thank you for pointing that out. Let me know what you think!

Copy link
Collaborator

@alan-agius4 alan-agius4 Jan 15, 2019

Choose a reason for hiding this comment

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

Should be something like;

    "implements": {
      "type": "array",
      "description": "Specifies which interfaces to implement.",
      "default": "CanActivate",
      "enum": [
           "CanActivate",
           "CanActivateChild",
           "CanLoad"
      ]
      ....
    },

Copy link
Contributor Author

@okeefem2 okeefem2 Jan 15, 2019

Choose a reason for hiding this comment

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

@alan-agius4 I'm sorry I could be totally missing something (my first time working with json schema 😬), but the x prompt logic requires this array to be named as items: [] as seen in https://github.com/angular/angular-cli/blob/master/packages/angular/cli/models/schematic-command.ts#L311

Are you saying in addition to the x-prompt defintion, also include the enum definition you describe here?

Thanks for your help btw! Just trying to learn and make sure I understand.

Copy link
Collaborator

@alan-agius4 alan-agius4 Jan 15, 2019

Choose a reason for hiding this comment

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

Not really, I think it should be something like;

 "implements": {
      "type": "array",
      "description": "Specifies which interfaces to implement.",
      "default": "CanActivate",
      "items": {
        "enum": [
          "CanActivate",
          "CanActivateChild",
          "CanLoad"
        ]
      },
      "x-prompt": {
        "message": "Which interfaces would you like to implement?",
        "type": "list",
        "multiselect": true,
        "items": [
          "CanActivate",
          "CanActivateChild",
          "CanLoad"
        ]
      }
}

The .... above meant, continuation with x-prompt definition.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Note: updated the above is I spotted a mistake

Copy link
Member

@clydin clydin Jan 15, 2019

Choose a reason for hiding this comment

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

Note also that I have a followup planned to add auto-configuration of multi-select based on the schema. It will eliminate the need to manually specify most of the prompt options and is the reason I originally suggested just using a string constraint for now (this schema would be adjusted in the process either way).

Copy link
Collaborator

Choose a reason for hiding this comment

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

in that case, it LGTM

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@clydin would I be able to help out with the implementation of the multi select auto configuration?? I’d love to keep learning and contributing and this could transition well from the work I’ve done so far. Let me know what you think and thanks everyone for the help so far!

Copy link
Contributor

@cyrilletuzi cyrilletuzi Feb 2, 2019

Choose a reason for hiding this comment

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

@clydin @okeefem2 @alan-agius4 For info, the decision made here causes issues like this one: cyrilletuzi/vscode-angular-schematics#32. The x-prompts is a specific use of the schema, the classic schema properties should be respected for other tools to work (ie. possible values must be in anyOf - or similar, not sure of the good property for this case). For the same reason, implements should be flagged as required (or else it should generate a correct file without it, currently ng g guard without implements generates a file with errors).

@kyliau
Copy link
Contributor

kyliau commented Jan 14, 2019

@okeefem2 Could you please fix the merge conflict?

@kyliau kyliau added the action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews label Jan 14, 2019
@okeefem2 okeefem2 force-pushed the feat-guard-schematic-multiselect branch from 219a6ec to 36f78e6 Compare January 15, 2019 11:07
Adds support for the guard schematic to use multiselect for which interfaces the guard should implement.

Fixes angular#13400
@okeefem2 okeefem2 force-pushed the feat-guard-schematic-multiselect branch from 36f78e6 to b4ee4e5 Compare January 15, 2019 11:12
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
return true;
}
<% } %><% if (implements.includes('CanActivateChild')) { %>canActivateChild(
Copy link
Collaborator

Choose a reason for hiding this comment

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

you can eventually use the enum type here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@alan-agius4 Are you saying to create an enum for the values instead of comparing hard coded strings? If yes would this live in the index.ts file or should it have it’s own file?

Copy link
Collaborator

@alan-agius4 alan-agius4 Jan 15, 2019

Choose a reason for hiding this comment

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

Hi, the enum is created from the json schema, you don't need to redefine it.

You can run yarn build it will generate an new schema with the enum you created in the json schema.

So you can import it like such

import { Schema as GuardOptions, Implement } from './schema';

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh very cool. I will make these changes later today when I get home. Thanks!

Copy link
Collaborator

@alan-agius4 alan-agius4 left a comment

Choose a reason for hiding this comment

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

See comments above

@clydin clydin removed the action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews label Jan 15, 2019
Copy link
Collaborator

@alan-agius4 alan-agius4 left a comment

Choose a reason for hiding this comment

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

LGTM, following @clydin comment.

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 12, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
target: major This PR is targeted for the next major release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add option to generate a guard for CanDeactivate, CanActivateChild, CanLoad
8 participants