Skip to content

fix: fix error thrown when template includes intrinsic function #1532

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
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ private void RemoveOrphanedLambdaFunctions(HashSet<string> processedLambdaFuncti
}

var toRemove = new List<string>();
foreach (var resourceName in _templateWriter.GetToken<Dictionary<string, object>>("Resources").Keys)
foreach (var resourceName in _templateWriter.GetKeys("Resources"))
{
var resourcePath = $"Resources.{resourceName}";
var type = _templateWriter.GetToken<string>($"{resourcePath}.Type", string.Empty);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace Amazon.Lambda.Annotations.SourceGenerator.Writers
using System.Collections;
using System.Collections.Generic;

namespace Amazon.Lambda.Annotations.SourceGenerator.Writers
{
/// <summary>
/// This interface contains utility methods to manipulate a YAML or JSON blob
Expand Down Expand Up @@ -56,5 +59,11 @@ public interface ITemplateWriter
/// If a string value starts with '@' then a reference node is created and returned.
/// </summary>
object GetValueOrRef(string value);

/// <summary>
/// Retrieves a list of keys for a specified template path.
/// </summary>
/// <param name="path">dot(.) seperated path. Example "Person.Name.FirstName"</param>
IList<string> GetKeys(string path);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
Expand Down Expand Up @@ -225,5 +226,17 @@ private T GetDeserializedToken<T>(object token)
}
return JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(token));
}

public IList<string> GetKeys(string path)
{
try
{
return GetToken<Dictionary<string, object>>(path).Keys.ToList();
}
catch (Exception ex)
{
throw new InvalidOperationException($"Unable to retrieve keys for the specified JSON path '{path}'.", ex);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -271,5 +271,17 @@ private T GetDeserializedToken<T>(object token)

return _deserializer.Deserialize<T>(_serializer.Serialize(token));
}

public IList<string> GetKeys(string path)
{
try
{
return GetToken<Dictionary<string, YamlMappingNode>>(path).Keys.ToList();
}
catch (Exception ex)
{
throw new InvalidOperationException($"Unable to retrieve keys for the specified YAML path '{path}'.", ex);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
<ItemGroup>
<None Remove="Snapshots\ServerlessTemplates\customizeResponse.template" />
<None Remove="Snapshots\ServerlessTemplates\dynamicexample.template" />
<None Remove="Snapshots\ServerlessTemplates\intrinsicexample.template" />
<None Remove="Snapshots\ServerlessTemplates\nullreferenceexample.template" />
<None Remove="Snapshots\ServerlessTemplates\subnamespace.template" />
<None Remove="Snapshots\ServerlessTemplates\taskexample.template" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Amazon.Lambda.Core;

namespace TestServerlessApp
{
public class IntrinsicExample_HasIntrinsic_Generated
{
private readonly IntrinsicExample intrinsicExample;

public IntrinsicExample_HasIntrinsic_Generated()
{
SetExecutionEnvironment();
intrinsicExample = new IntrinsicExample();
}

public void HasIntrinsic(string text, Amazon.Lambda.Core.ILambdaContext __context__)
{
intrinsicExample.HasIntrinsic(text, __context__);
}

private static void SetExecutionEnvironment()
{
const string envName = "AWS_EXECUTION_ENV";

var envValue = new StringBuilder();

// If there is an existing execution environment variable add the annotations package as a suffix.
if(!string.IsNullOrEmpty(Environment.GetEnvironmentVariable(envName)))
{
envValue.Append($"{Environment.GetEnvironmentVariable(envName)}_");
}

envValue.Append("amazon-lambda-annotations_0.13.4.0");

Environment.SetEnvironmentVariable(envName, envValue.ToString());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: This template is partially managed by Amazon.Lambda.Annotations (v0.13.4.0).
Resources:
TestServerlessAppIntrinsicExampleHasIntrinsicGenerated:
Type: AWS::Serverless::Function
Metadata:
Tool: Amazon.Lambda.Annotations
Properties:
Description: !Sub 'This is deployed in region ${AWS::Region}'
MemorySize: 256
Timeout: 30
Policies:
- AWSLambdaBasicExecutionRole
PackageType: Image
ImageUri: .
ImageConfig:
Command:
- TestProject::TestServerlessApp.IntrinsicExample_HasIntrinsic_Generated::HasIntrinsic
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Amazon.Lambda.Annotations.SourceGenerators.Tests
{
public class SourceGeneratorTests
public class SourceGeneratorTests : IDisposable
{
[Fact]
public async Task Greeter()
Expand Down Expand Up @@ -298,6 +298,45 @@ public async Task VerifyFunctionReturnVoid()
Assert.Equal(expectedTemplateContent, actualTemplateContent);
}

[Fact]
public async Task VerifyNoErrorWithIntrinsicInTemplate()
{
var expectedTemplateContent = File.ReadAllText(Path.Combine("Snapshots", "ServerlessTemplates", "intrinsicexample.template")).ToEnvironmentLineEndings();
var expectedSubNamespaceGenerated = File.ReadAllText(Path.Combine("Snapshots", "IntrinsicExample_HasIntrinsic_Generated.g.cs")).ToEnvironmentLineEndings();
File.WriteAllText(Path.Combine("TestServerlessApp", "serverless.template"), expectedTemplateContent);

await new VerifyCS.Test
{
TestState =
{
Sources =
{
(Path.Combine("TestServerlessApp", "IntrinsicExample.cs"), File.ReadAllText(Path.Combine("TestServerlessApp", "IntrinsicExample.cs"))),
(Path.Combine("Amazon.Lambda.Annotations", "LambdaFunctionAttribute.cs"), File.ReadAllText(Path.Combine("Amazon.Lambda.Annotations", "LambdaFunctionAttribute.cs"))),
(Path.Combine("Amazon.Lambda.Annotations", "LambdaStartupAttribute.cs"), File.ReadAllText(Path.Combine("Amazon.Lambda.Annotations", "LambdaStartupAttribute.cs"))),
(Path.Combine("TestServerlessApp", "AssemblyAttributes.cs"), File.ReadAllText(Path.Combine("TestServerlessApp", "AssemblyAttributes.cs"))),
},
GeneratedSources =
{
(
typeof(SourceGenerator.Generator),
"IntrinsicExample_HasIntrinsic_Generated.g.cs",
SourceText.From(expectedSubNamespaceGenerated, Encoding.UTF8, SourceHashAlgorithm.Sha256)
)
},
ExpectedDiagnostics =
{
new DiagnosticResult("AWSLambda0103", DiagnosticSeverity.Info).WithArguments("IntrinsicExample_HasIntrinsic_Generated.g.cs", expectedSubNamespaceGenerated),
new DiagnosticResult("AWSLambda0103", DiagnosticSeverity.Info).WithArguments($"TestServerlessApp{Path.DirectorySeparatorChar}serverless.template", expectedTemplateContent)
},
ReferenceAssemblies = ReferenceAssemblies.Net.Net60
}
}.RunAsync();

var actualTemplateContent = File.ReadAllText(Path.Combine("TestServerlessApp", "serverless.template"));
Assert.Equal(expectedTemplateContent, actualTemplateContent);
}

[Fact]
public async Task VerifyFunctionReturnTask()
{
Expand Down Expand Up @@ -646,5 +685,10 @@ public async Task ComplexQueryParameters_AreNotSupported()

}.RunAsync();
}

public void Dispose()
{
File.Delete(Path.Combine("TestServerlessApp", "serverless.template"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Amazon.Lambda.Annotations.SourceGenerators.Tests.WriterTests
public class YamlWriterTests
{
const string yamlContent = @"
Description: !Sub '${AWS::Region}'
Person:
Name:
FirstName: John
Expand All @@ -29,12 +30,14 @@ public void Exists()
yamlWriter.Parse(yamlContent);

// ACT and ASSERT
Assert.True(yamlWriter.Exists("Description"));
Assert.True(yamlWriter.Exists("Person"));
Assert.True(yamlWriter.Exists("Person.Name"));
Assert.True(yamlWriter.Exists("Person.Name.LastName"));
Assert.True(yamlWriter.Exists("Person.Age"));
Assert.True(yamlWriter.Exists("Person.PhoneNumbers"));

Assert.False(yamlWriter.Exists("description"));
Assert.False(yamlWriter.Exists("person"));
Assert.False(yamlWriter.Exists("Person.FirstName"));
Assert.False(yamlWriter.Exists("Person.DOB"));
Expand Down Expand Up @@ -109,10 +112,12 @@ public void RemoveToken()
yamlWriter.Parse(yamlContent);

// ACT
yamlWriter.RemoveToken("Description");
yamlWriter.RemoveToken("Person.Name.LastName");
yamlWriter.RemoveToken("Person.Age");

// ASSERT
Assert.False(yamlWriter.Exists("Description"));
Assert.False(yamlWriter.Exists("Person.Name.LastName"));
Assert.False(yamlWriter.Exists("Person.Age"));
Assert.True(yamlWriter.Exists("Person.Name"));
Expand Down Expand Up @@ -162,5 +167,37 @@ private string SanitizeFileContents(string content)
.Replace("\r\r\n", Environment.NewLine)
.Trim();
}

[Fact]
public void GetKeys()
{
// ARRANGE
const string yamlContent = @"
Resources:
Function1:
Description:
Fn::Sub:
- '${var1}'
- var1: Test
Function2:
Description: !Sub '${AWS::Region}'
";
ITemplateWriter yamlWriter = new YamlWriter();
yamlWriter.Parse(yamlContent);

// ACT
var functionNames = yamlWriter.GetKeys("Resources");
var function1Keys = yamlWriter.GetKeys("Resources.Function1");

// ASSERT
Assert.NotEmpty(functionNames);
Assert.Equal(2, functionNames.Count);
Assert.Equal("Function1", functionNames[0]);
Assert.Equal("Function2", functionNames[1]);
Assert.NotEmpty(function1Keys);
var description = Assert.Single(function1Keys);
Assert.Equal("Description", description);
Assert.Throws<InvalidOperationException>(() => { yamlWriter.GetKeys("Resources.Function2"); });
}
}
}
14 changes: 14 additions & 0 deletions Libraries/test/TestServerlessApp/IntrinsicExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Amazon.Lambda.Annotations;
using Amazon.Lambda.Core;

namespace TestServerlessApp
{
public class IntrinsicExample
{
[LambdaFunction(PackageType = LambdaPackageType.Image)]
public void HasIntrinsic(string text, ILambdaContext context)
{
context.Logger.LogLine(text);
}
}
}