-
Notifications
You must be signed in to change notification settings - Fork 494
Add [ScheduleEvent] annotation attribute and source generator support #2323
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
Open
GarrettBeatty
wants to merge
1
commit into
feature/dynamodb-annotations
Choose a base branch
from
feature/schedule-annotations
base: feature/dynamodb-annotations
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "Projects": [ | ||
| { | ||
| "Name": "Amazon.Lambda.Annotations", | ||
| "Type": "Minor", | ||
| "ChangelogMessages": [ | ||
| "Added [ScheduleEvent] annotation attribute for declaratively configuring schedule-triggered Lambda functions with support for rate and cron expressions, description, input, and enabled state." | ||
| ] | ||
| } | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...zon.Lambda.Annotations.SourceGenerator/Models/Attributes/ScheduleEventAttributeBuilder.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| using Amazon.Lambda.Annotations.Schedule; | ||
| using Microsoft.CodeAnalysis; | ||
| using System; | ||
|
|
||
| namespace Amazon.Lambda.Annotations.SourceGenerator.Models.Attributes | ||
| { | ||
| /// <summary> | ||
| /// Builder for <see cref="ScheduleEventAttribute"/>. | ||
| /// </summary> | ||
| public class ScheduleEventAttributeBuilder | ||
| { | ||
| public static ScheduleEventAttribute Build(AttributeData att) | ||
| { | ||
| if (att.ConstructorArguments.Length != 1) | ||
| { | ||
| throw new NotSupportedException($"{TypeFullNames.ScheduleEventAttribute} must have constructor with 1 argument."); | ||
| } | ||
| var schedule = att.ConstructorArguments[0].Value as string; | ||
| var data = new ScheduleEventAttribute(schedule); | ||
|
|
||
| foreach (var pair in att.NamedArguments) | ||
| { | ||
| if (pair.Key == nameof(data.ResourceName) && pair.Value.Value is string resourceName) | ||
| { | ||
| data.ResourceName = resourceName; | ||
| } | ||
| else if (pair.Key == nameof(data.Description) && pair.Value.Value is string description) | ||
| { | ||
| data.Description = description; | ||
| } | ||
| else if (pair.Key == nameof(data.Input) && pair.Value.Value is string input) | ||
| { | ||
| data.Input = input; | ||
| } | ||
| else if (pair.Key == nameof(data.Enabled) && pair.Value.Value is bool enabled) | ||
| { | ||
| data.Enabled = enabled; | ||
| } | ||
| } | ||
|
|
||
| return data; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
Libraries/src/Amazon.Lambda.Annotations/Schedule/ScheduleEventAttribute.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text.RegularExpressions; | ||
|
|
||
| namespace Amazon.Lambda.Annotations.Schedule | ||
| { | ||
| /// <summary> | ||
| /// This attribute defines the Schedule event source configuration for a Lambda function. | ||
| /// </summary> | ||
| [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] | ||
| public class ScheduleEventAttribute : Attribute | ||
| { | ||
| private static readonly Regex _resourceNameRegex = new Regex("^[a-zA-Z0-9]+$"); | ||
|
|
||
| /// <summary> | ||
| /// The schedule expression. Supports rate and cron expressions. | ||
| /// Examples: "rate(5 minutes)", "cron(0 12 * * ? *)" | ||
| /// </summary> | ||
| public string Schedule { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// The CloudFormation resource name for the schedule event. | ||
| /// </summary> | ||
| public string ResourceName | ||
| { | ||
| get | ||
| { | ||
| if (IsResourceNameSet) | ||
| { | ||
| return resourceName; | ||
| } | ||
| // Generate a default resource name from the schedule expression | ||
| var sanitized = string.Join(string.Empty, (Schedule ?? string.Empty).Where(char.IsLetterOrDigit)); | ||
| return sanitized.Length > 0 ? sanitized : "ScheduleEvent"; | ||
| } | ||
| set => resourceName = value; | ||
| } | ||
|
|
||
| private string resourceName { get; set; } = null; | ||
| internal bool IsResourceNameSet => resourceName != null; | ||
|
|
||
| /// <summary> | ||
| /// A description for the schedule rule. | ||
| /// </summary> | ||
| public string Description { get; set; } = null; | ||
| internal bool IsDescriptionSet => Description != null; | ||
|
|
||
| /// <summary> | ||
| /// A JSON string to pass as input to the Lambda function. | ||
| /// </summary> | ||
| public string Input { get; set; } = null; | ||
| internal bool IsInputSet => Input != null; | ||
|
|
||
| /// <summary> | ||
| /// If set to false, the event source mapping will be disabled. Default value is true. | ||
|
GarrettBeatty marked this conversation as resolved.
|
||
| /// </summary> | ||
| public bool Enabled | ||
| { | ||
| get => enabled.GetValueOrDefault(true); | ||
| set => enabled = value; | ||
| } | ||
| private bool? enabled { get; set; } | ||
| internal bool IsEnabledSet => enabled.HasValue; | ||
|
|
||
|
GarrettBeatty marked this conversation as resolved.
|
||
| /// <summary> | ||
| /// Creates an instance of the <see cref="ScheduleEventAttribute"/> class. | ||
| /// </summary> | ||
| /// <param name="schedule"><see cref="Schedule"/> property</param> | ||
| public ScheduleEventAttribute(string schedule) | ||
| { | ||
| Schedule = schedule; | ||
| } | ||
|
|
||
| internal List<string> Validate() | ||
| { | ||
| var validationErrors = new List<string>(); | ||
|
|
||
| if (string.IsNullOrEmpty(Schedule)) | ||
| { | ||
| validationErrors.Add($"{nameof(ScheduleEventAttribute.Schedule)} must not be null or empty"); | ||
| } | ||
| else if (!Schedule.StartsWith("rate(") && !Schedule.StartsWith("cron(")) | ||
| { | ||
| validationErrors.Add($"{nameof(ScheduleEventAttribute.Schedule)} = {Schedule}. It must start with 'rate(' or 'cron('"); | ||
| } | ||
|
|
||
| if (IsResourceNameSet && !_resourceNameRegex.IsMatch(ResourceName)) | ||
| { | ||
| validationErrors.Add($"{nameof(ScheduleEventAttribute.ResourceName)} = {ResourceName}. It must only contain alphanumeric characters and must not be an empty string"); | ||
| } | ||
|
|
||
| return validationErrors; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.