Home > Mobile >  Using anonymous objects instead of Dictionary<string, object?> results into compile-time error
Using anonymous objects instead of Dictionary<string, object?> results into compile-time error

Time:11-27

I'm doing Pulumi C# Infrastructure as Code (IaC).

Pulumi docs uses Dictionary<string, object?> which I want to replace with anonymous objects because they look better.

The issue is that I'm getting a compile-time error with the anonymous objects.

x is ImmutableArray<string>.

Fine

var xpolicyDoc = Output
    .All(tables.UserTradesTableArn)
    .Apply(x =>
        new Dictionary<string, object?>
        {
            { "Version", "2012-10-17" },
            {
                "Statement", new[]
                {
                    new Dictionary<string, object?>
                    {
                        { "Effect", "Allow" },
                        {
                            "Action", new[]
                            {
                                "dynamodb:BatchWriteItem",
                                "dynamodb:PutItem",
                                "dynamodb:DeleteItem",
                                "dynamodb:UpdateItem",
                                "dynamodb:DescribeTable",
                                "dynamodb:Query"
                            }
                        },
                        {
                            "Resource", new[] { x[0] }
                        }
                    },
                    new Dictionary<string, object?>
                    {
                        { "Effect", "Allow" },
                        { "Action", "dynamodb:ListTables" },
                        { "Resource", "*" }
                    }
                }
            }
        });

var policy = new Policy($"{tableName}-table-policy", new PolicyArgs
{
    PolicyDocument = policyDoc.Apply(x => JsonSerializer.Serialize(x))
});

Compile-time error

x[0]: Anonymous type projection initializer must be simple name or member access expression

var policyDoc = Output
    .All(tables.UserTradesTableArn)
    .Apply(x =>
        new
        {
            Version = "2012-10-17",
            Statement = new[]
            {
                new
                {
                    Effect = "Allow",
                    Action = new[]
                    {
                        "dynamodb:BatchWriteItem",
                        "dynamodb:PutItem",
                        "dynamodb:DeleteItem",
                        "dynamodb:UpdateItem",
                        "dynamodb:DescribeTable",
                        "dynamodb:Query"
                    },
                    Resource = new { x[0] }
                },
                new
                {
                    Effect = "Allow",
                    Action = "dynamodb:ListTables",
                    Resource = "*"
                }
            }
        });

var policy = new Policy($"{tableName}-table-policy", new PolicyArgs
{
    PolicyDocument = policyDoc.Apply(x => JsonSerializer.Serialize(x))
});

CodePudding user response:

It should be either

Resource = new [] { x[0] }

Or

Resource = x[0]

Or

Resource = new { SomeProperty = x[0] }

What you have is missing a property name.

  • Related