Created
September 10, 2019 01:39
-
-
Save wrumsby/2bb7b6eb5d43c63a5eb066f989b853f8 to your computer and use it in GitHub Desktop.
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
import 'reflect-metadata'; | |
import AWS from 'aws-sdk'; | |
const metadataKey = Symbol('snsAttribute'); | |
function snsAttribute(isSnsAttribute: boolean = true) { | |
return Reflect.metadata(metadataKey, isSnsAttribute); | |
} | |
class Alex { | |
public x: string; | |
@snsAttribute() | |
public y: string; | |
} | |
const a = new Alex(); | |
a.x = 'x'; | |
a.y = 'why'; | |
function hasSnsAttribute(target: any, propertyKey: string) { | |
return Reflect.getMetadata(metadataKey, target, propertyKey); | |
} | |
const attrs = Object.entries(a).reduce( | |
(previousValue: AWS.SNS.MessageAttributeMap, currentValue: [string, any]) => { | |
const [key, value] = currentValue; | |
if (hasSnsAttribute(a, key)) { | |
const attribute: AWS.SNS.MessageAttributeValue = { | |
DataType: 'String', | |
StringValue: value, | |
}; | |
return Object.assign(previousValue, { | |
[key]: attribute, | |
}); | |
} | |
return previousValue; | |
}, | |
{} as AWS.SNS.MessageAttributeMap, | |
); | |
console.dir(attrs); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment