author Jeremy Daly <jeremy@jeremydaly.com>
 *  license import { TableAttributes , TableAttributeConfig , ParsedTableAttribute , DynamoDBTypes } from '../classes/Table' ; import { error , typeError , keyTypeError , validTypes , validKeyTypes } from './utils' ; export default ( attrs , partitionKey , sortKey ) => Object . keys ( attrs ) . reduce ( ( acc , field ) => { if ( typeof attrs [ field ] === 'string' ) { if ( [ partitionKey , sortKey ] . includes ( field ) && ! validKeyTypes . includes ( attrs [ field ] . toString ( ) ) ) { keyTypeError ( field ) ; } if ( ! validTypes . includes ( attrs [ field ] . toString ( ) ) ) { typeError ( field ) ; } return Object . assign ( acc , parseAttributeConfig ( field , { type : attrs [ field ] } as TableAttributeConfig ) ) ; } else { const fieldVal = attrs [ field ] as TableAttributeConfig ; if ( [ partitionKey , sortKey ] . includes ( field ) && ! validKeyTypes . includes ( fieldVal . type ) ) { keyTypeError ( field ) ; } if ( ! validTypes . includes ( fieldVal . type ) ) { typeError ( field ) ; } return Object . assign ( acc , parseAttributeConfig ( field , fieldVal ) ) ; } } , { } ) ; const parseAttributeConfig = ( field , config ) : ParsedTableAttribute => { Object . keys ( config ) . forEach ( prop => { switch ( prop ) { case 'type' : break ; case 'setType' : if ( config . type !== 'set' ) error ( `'setType' is only valid for type 'set'` ) ; if ( ! [ 'string' , 'number' , 'binary' ] . includes ( config [ prop ] ! ) ) error ( `Invalid 'setType', must be 'string', 'number', or 'binary'` ) ; break ; default : error ( `'${ prop }' is not a valid property type` ) ; } } ) ; return { [ field ] : { ... config , mappings : { } } } ; } ;