Clever / wag

sWAGger - Web API Generator
Apache License 2.0
77 stars 6 forks source link

fix dynamoodb codegen for datetime partition keys for GSIs #474

Closed dxu429 closed 8 months ago

dxu429 commented 8 months ago

Jira

https://clever.atlassian.net/browse/DD-5016

Overview

~1. Fix import for dynamodb - should only be imported in dynamodb.go when there are transactions~

Currently code gen for date-time HASH keys for GSI's doesn't properly convert strfmt.Datetime to strings in the generated getBy{GSI} functions.

Example

here input.endDate has type strfmt.Date, which should be converted to a string

Before

 <<<<<<< type mismatch>>>>>>>>
if input.EndDate == "" {
 <<<<<<< type mismatch>>>>>>>>
    return fmt.Errorf("Hash key input.EndDate cannot be empty")
}
queryInput := &dynamodb.QueryInput{
    TableName: aws.String(t.name()),
    IndexName: aws.String("endDate"),
    ExpressionAttributeNames: map[string]*string{
        "#ENDDATE": aws.String("endDate"),
    },
    ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
        ":endDate": &dynamodb.AttributeValue{
<<<<<<< type mismatch>>>>>>>>
            S: aws.String(input.EndDate),
<<<<<<< type mismatch>>>>>>>>
        },
    },
    ScanIndexForward: aws.Bool(!input.Descending),
    ConsistentRead:   aws.Bool(false),
}

After

if toDynamoTimeString(input.EndDate) == "" {
    return fmt.Errorf("Hash key input.EndDate cannot be empty")
}
queryInput := &dynamodb.QueryInput{
    TableName: aws.String(t.name()),
    IndexName: aws.String("endDate"),
    ExpressionAttributeNames: map[string]*string{
        "#ENDDATE": aws.String("endDate"),
    },
    ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
        ":endDate": &dynamodb.AttributeValue{
            S: aws.String(toDynamoTimeString(input.EndDate)),
        },
    },
    ScanIndexForward: aws.Bool(!input.Descending),
    ConsistentRead:   aws.Bool(false),
}

Testing

  1. Created a sample schema to test ThingWithDatetimeGSI
  2. Tested by building & using built executable to make generate on a service I'm using this on. https://github.com/Clever/order-service/compare/master...subscriptions-model Screenshot 2024-03-04 at 12 10 21 PM Screenshot 2024-03-04 at 12 11 49 PM
dxu429 commented 8 months ago

@tnsardesai I'm using a datetime for the HASH key