All files / ua decode.js

95.71% Statements 67/70
90.2% Branches 46/51
100% Functions 3/3
95.65% Lines 66/69

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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131  55x 110x   55x 55x 55x 55x 55x 55x 42x     42x     42x 2x   40x     40x   55x   729x 729x 729x 729x 729x 131x   598x   4x 4x     1x 1x     7x 7x     1x 1x     5x 5x     1x 1x     142x 142x     1x 1x     1x 1x     1x 1x     6x 6x     177x 177x     38x 38x     25x 25x     60x 60x 18x     42x 42x 42x   54x   54x               42x 42x     128x 558x 558x 558x                   580x   55x  
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const Bucket_1 = __importDefault(require("./Bucket"));
const guards_1 = require("./guards");
const factory_1 = __importDefault(require("./factory"));
const utils_1 = require("./utils");
const NewArray = (subtype, length) => {
    Iif (subtype === 'uint8') {
        return new Uint8Array(length);
    }
    Iif (subtype === 'uint16') {
        return new Uint16Array(length);
    }
    if (subtype === 'uint32') {
        return new Uint32Array(length);
    }
    Iif (subtype === 'float64') {
        return new Float64Array(length);
    }
    return new Array(length);
};
const decode = (args) => {
    var _a, _b;
    const { bytes, instance, position } = args;
    const key = (_a = args.key, (_a !== null && _a !== void 0 ? _a : ''));
    const type = (_b = args.type, (_b !== null && _b !== void 0 ? _b : typeof instance));
    const bucket = new Bucket_1.default(bytes, position);
    if (guards_1.isDecoder(instance)) {
        return instance.decode(bytes, position);
    }
    switch (type) {
        case 'boolean': {
            Reflect.set(instance, key, bucket.readBoolean());
            break;
        }
        case 'int8': {
            Reflect.set(instance, key, bucket.readInt8());
            break;
        }
        case 'uint8': {
            Reflect.set(instance, key, bucket.readUint8());
            break;
        }
        case 'int16': {
            Reflect.set(instance, key, bucket.readInt16());
            break;
        }
        case 'uint16': {
            Reflect.set(instance, key, bucket.readUint16());
            break;
        }
        case 'int32': {
            Reflect.set(instance, key, bucket.readInt32());
            break;
        }
        case 'uint32': {
            Reflect.set(instance, key, bucket.readUint32());
            break;
        }
        case 'int64': {
            Reflect.set(instance, key, bucket.readInt64());
            break;
        }
        case 'uint64': {
            Reflect.set(instance, key, bucket.readUint64());
            break;
        }
        case 'float32': {
            Reflect.set(instance, key, bucket.readFloat32());
            break;
        }
        case 'float64': {
            Reflect.set(instance, key, bucket.readFloat64());
            break;
        }
        case 'string': {
            Reflect.set(instance, key, bucket.readString());
            break;
        }
        case 'Date': {
            Reflect.set(instance, key, bucket.readDate());
            break;
        }
        case 'ByteString': {
            Reflect.set(instance, key, bucket.readByteString());
            break;
        }
        case 'Array': {
            const n = bucket.readInt32();
            if (n === -1) {
                return bucket.position;
            }
            // create array by name with length
            const subtype = Reflect.getMetadata('design:subtype', instance, key);
            const a = NewArray(subtype, n);
            for (let i = 0; i < n; i++) {
                // create new instance of given type and add it to the array
                a[i] = factory_1.default(subtype);
                // decode instance
                bucket.position = decode({
                    bytes: bucket.bytes,
                    instance: utils_1.isPrimitiveType(subtype) ? a : a[i],
                    key: i,
                    type: subtype,
                    position: bucket.position
                });
            }
            Reflect.set(instance, key, a);
            break;
        }
        default:
            for (const name of Object.getOwnPropertyNames(instance)) {
                Eif (guards_1.isNotNullObject(instance) && guards_1.keyInObject(instance, name)) {
                    const type = Reflect.getMetadata('design:type', instance, name);
                    bucket.position = decode({
                        bytes: bytes,
                        instance: type === 'object' ? instance[name] : instance,
                        key: name,
                        type,
                        position: bucket.position
                    });
                }
            }
    }
    return bucket.position;
};
exports.default = decode;