Skip to content
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

Support for deriving property names from DynamoDBContext attributes #302

Open
danielmarbach opened this issue May 1, 2023 · 0 comments
Open

Comments

@danielmarbach
Copy link
Contributor

danielmarbach commented May 1, 2023

Describe the feature.

When using the DynamoDB context users can attribute their classes with attributes like DynamoDBHashKey, DynamoDBRangeKey and DynamoDBProperty. These attributes allow overriding the property name. Currently when the mapper is used with classes containing those attributes all renamed property need to redundantly declare JsonPropertyName to make the mapper respect the proper names.

    class Customer
    {
        [DynamoDBHashKey("PK")]
        [JsonPropertyName("PK")]
        public string PartitionKey { get; set; }

        [DynamoDBRangeKey("SK")]
        [JsonPropertyName("SK")]
        public string SortKey { get; set; }

        [DynamoDBProperty("customer_preferred")]
        [JsonPropertyName("customer_preferred")]
        public bool CustomerPreferred { get; set; }
    }

With .NET 7 and higher it is possible to add IJsonTypeInfoResolver implementations that get access to either reflection based or source generation based metadata. With that in place it is possible to extract the property information, check for the attributes and emit corresponding property names automatically which would make it possible to use a class without having to attribute properties. Example

    class Customer
    {
        [DynamoDBHashKey("PK")]
        public string PartitionKey { get; set; }

        [DynamoDBRangeKey("SK")]
        public string SortKey { get; set; }

        [DynamoDBProperty("customer_preferred")]
        public bool CustomerPreferred { get; set; }
    }

Additional Context

Pseudo code (just a brainstorm, there might be more elegant solutions)

public class DynamoDBContextAwareTypeInfoResolver: DefaultJsonTypeInfoResolver
{
    public override JsonTypeInfo GetTypeInfo(Type type, JsonSerializerOptions options)
    {
        JsonTypeInfo typeInfo = base.GetTypeInfo(type, options);
        if (typeInfo.Kind == JsonTypeInfoKind.Object)
        {
            foreach (JsonPropertyInfo property in typeInfo.Properties)
            {
                var attribute = property.AttributeProvider.GetCustomAttributes(typeof(DynamoDBRenamableAttribute));
                if(attribute is DynamoDBRenamableAttribute renameAttribute) {
                    property.Name = renameAttribute.AttributeName;
                }
            }
        }
        return typeInfo;
    }
}
``
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants