author Jeremy Daly <jeremy@jeremydaly.com>
 *  license import { EntityAttributes , EntityAttributeConfig , EntityCompositeAttributes , EntityAttributeConfiguration } from '../classes/Entity' ; import { TrackingInfo } from './parseEntity' ; import parseMapping from './parseMapping' ; import parseCompositeKey from './parseCompositeKey' ; import { error , typeError , validTypes } from './utils' ; export default ( attributes , track ) => { attributes = Object . keys ( attributes ) . reduce ( ( acc , field ) => { if ( typeof attributes [ field ] === 'string' ) { if ( ! validTypes . includes ( attributes [ field ] . toString ( ) ) ) { typeError ( field ) ; } return Object . assign ( acc , parseMapping ( field , { type : attributes [ field ] } as EntityAttributeConfig , track ) ) ; } else if ( Array . isArray ( attributes [ field ] ) ) { return Object . assign ( acc , parseCompositeKey ( field , attributes [ field ] as EntityCompositeAttributes , track , attributes ) ) ; } else { const fieldVal = attributes [ field ] as EntityAttributeConfig ; fieldVal . type = ! fieldVal . type ? 'string' : fieldVal . type ; if ( ! validTypes . includes ( fieldVal . type ) ) { typeError ( field ) ; } return Object . assign ( acc , parseMapping ( field , fieldVal , track ) ) ; } } , { } ) ; if ( ! track . keys . partitionKey ) error ( 'Entity requires a partitionKey attribute' ) ; return { keys : track . keys , attributes : attributes as EntityAttributeConfiguration } ; } ;