C
C#2y ago
Hulkstance

RangeKey

I'm doing Pulumi IaC. I don't like RangeKey = _rangeKey != null ? (Input<string>?)_rangeKey : null. looks kinda ugly. Is there a better way?
public sealed class DynamoDbTableBuilder
{
private string? _name;
private string? _hashKey;
private string? _rangeKey;

private readonly List<TableAttributeArgs> _tableAttributes = new();

public DynamoDbTableBuilder WithName(string name)
{
_name = name;
return this;
}

public DynamoDbTableBuilder WithTableAttribute(string name, string type)
{
_tableAttributes.Add(new TableAttributeArgs
{
Name = name,
Type = type
});
return this;
}

public DynamoDbTableBuilder WithHashKey(string hashKey)
{
_hashKey = hashKey;
return this;
}

public DynamoDbTableBuilder WithRangeKey(string rangeKey)
{
_rangeKey = rangeKey;
return this;
}

public Table Build()
{
if (string.IsNullOrEmpty(_name))
{
throw new InvalidOperationException("Table name is not set");
}

if (string.IsNullOrEmpty(_hashKey))
{
throw new InvalidOperationException("Hash key is not set");
}

return new Table(_name, new TableArgs
{
Attributes = _tableAttributes,
BillingMode = "PAY_PER_REQUEST",
TableClass = "STANDARD",
StreamEnabled = false,
HashKey = _hashKey,
RangeKey = _rangeKey != null ? (Input<string>?)_rangeKey : null
});
}
}
public sealed class DynamoDbTableBuilder
{
private string? _name;
private string? _hashKey;
private string? _rangeKey;

private readonly List<TableAttributeArgs> _tableAttributes = new();

public DynamoDbTableBuilder WithName(string name)
{
_name = name;
return this;
}

public DynamoDbTableBuilder WithTableAttribute(string name, string type)
{
_tableAttributes.Add(new TableAttributeArgs
{
Name = name,
Type = type
});
return this;
}

public DynamoDbTableBuilder WithHashKey(string hashKey)
{
_hashKey = hashKey;
return this;
}

public DynamoDbTableBuilder WithRangeKey(string rangeKey)
{
_rangeKey = rangeKey;
return this;
}

public Table Build()
{
if (string.IsNullOrEmpty(_name))
{
throw new InvalidOperationException("Table name is not set");
}

if (string.IsNullOrEmpty(_hashKey))
{
throw new InvalidOperationException("Hash key is not set");
}

return new Table(_name, new TableArgs
{
Attributes = _tableAttributes,
BillingMode = "PAY_PER_REQUEST",
TableClass = "STANDARD",
StreamEnabled = false,
HashKey = _hashKey,
RangeKey = _rangeKey != null ? (Input<string>?)_rangeKey : null
});
}
}
2 Replies
ero
ero2y ago
_rangeKey as Input<string>??
Hulkstance
HulkstanceOP2y ago
pulumi
Inputs and Outputs
An in depth look at Pulumi Inputs and Outputs and their usage.
Want results from more Discord servers?
Add your server