All files / fabric-contract-api/lib/annotations object.js

100% Statements 33/33
100% Branches 18/18
100% Functions 5/5
100% Lines 33/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87            2x 2x 2x 2x   2x 4x     2x 6x 4x   4x   4x   4x   4x     4x   4x 2x               2x 1x     2x                             4x   4x       2x 3x 3x   3x   3x   3x 2x   2x 2x     3x   3x   3x      
/*
 * Copyright IBM Corp. All Rights Reserved.
 *
 * SPDX-License-Identifier: Apache-2.0
 */
 
const Logger = require('../logger');
const logger = Logger.getLogger('./lib/annotations/object.js');
const utils = require('./utils');
require('reflect-metadata');
 
const getProto = function (prototype) {
    return Object.getPrototypeOf(prototype);
};
 
module.exports.Object = function Object (opts) {
    return (target) => {
        logger.info('@Object args: Target -> %s', target.constructor.name);
 
        const objects = Reflect.getMetadata('fabric:objects', global) || {};
 
        logger.debug('Existing fabric:objects %s', objects);
 
        const properties = Reflect.getMetadata('fabric:object-properties', target.prototype) || {};
 
        logger.debug('Existing fabric:object-properties for target', properties);
 
        // check for the presence of a supertype
        const supertype = getProto(target.prototype).constructor.name;
 
        if (supertype === 'Object') {
            objects[target.name] = {
                '$id': target.name,
                type: 'object',
                cnstr: target,
                properties: properties
            };
 
            // add in the discriminator property name if one has been supplied in the object annotations
            if (opts && opts.discriminator) {
                objects[target.name].discriminator = {propertyName: opts.discriminator};
            }
        } else {
            objects[target.name] = {
                '$id': target.name,
                cnstr: target,
                allOf: [
                    {
                        type: 'object',
                        properties: properties
                    },
                    {
                        '$ref': `${supertype}`
                    }
                ]
            };
        }
 
        Reflect.defineMetadata('fabric:objects', objects, global);
 
        logger.debug('Updated fabric:objects', objects);
    };
};
 
module.exports.Property = function Property (name, type) {
    return (target, propertyKey) => {
        logger.debug('@Property args:', `Property Key -> ${propertyKey}, Name -> ${name}, Type -> ${type},`, 'Target ->', target.constructor.name);
 
        const properties = Reflect.getOwnMetadata('fabric:object-properties', target) || {};
 
        logger.debug('Existing fabric:object-properties for target', properties);
 
        if (!name || !type) {
            name = propertyKey;
 
            const metaType = Reflect.getMetadata('design:type', target, propertyKey);
            type = typeof metaType === 'function' ? metaType.name : metaType.toString();
        }
 
        properties[name] = utils.generateSchema(type, false);
 
        Reflect.defineMetadata('fabric:object-properties', properties, target);
 
        logger.debug('Updated fabric:object-properties for target', properties);
    };
};