Created
July 14, 2022 22:34
-
-
Save jabalsad/d336b9278bdc3f7749803e0594fadea2 to your computer and use it in GitHub Desktop.
DynamoDB PartiQL transaction values using reflection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func dynamoValue(i any) (string, []types.AttributeValue, error) { | |
val := reflect.ValueOf(i) | |
fields := reflect.VisibleFields(reflect.TypeOf(i)) | |
names := make([]string, 0) | |
values := make([]types.AttributeValue, 0) | |
for _, field := range fields { | |
name := field.Tag.Get("dynamodbav") | |
if name != "" && name != "-" { | |
names = append(names, name) | |
v := val.FieldByName(field.Name).Interface() | |
attr, err := attributevalue.Marshal(v) | |
if err != nil { | |
return "", nil, err | |
} | |
values = append(values, attr) | |
} | |
} | |
elems := make([]string, len(names)) | |
for i, name := range names { | |
elems[i] = "\"" + name + "\":?" | |
} | |
str := fmt.Sprintf("{%s}", strings.Join(elems, ", ")) | |
return str, values, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment