Code coverage report for thriftify/specs/astruct.js

Statements: 93.44% (57 / 61)      Branches: 80.77% (21 / 26)      Functions: 100% (4 / 4)      Lines: 93.44% (57 / 61)      Ignored: none     

All files » thriftify/specs/ » astruct.js
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                                            1 1 1 1 1   1 60 30   30 30 30 30 30   30     1 14 3   11   11     11 11 11 11 11 11 30 30 30 30       1 6 6 9 9 9     9         9 9 1         8   5     1 10 10 27 27 27 14         13 13 2         11 11 11     8     1 1  
// Copyright (c) 2015 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
 
'use strict';
 
var thriftrw = require('thriftrw');
var TField = thriftrw.TField;
var TYPE = thriftrw.TYPE;
var util = require('util');
var ret = require('./ret');
 
function AField(opts) {
    if (!(this instanceof AField)) {
        return new AField(opts);
    }
    opts = opts || {};
    this.id = opts.id;
    this.name = opts.name;
    this.type = opts.type;
    this.required = opts.required || false;
    // TODO: handle defaultValue
    this.defaultValue = opts.defaultValue || null;
}
 
function AStruct(opts) {
    if (!(this instanceof AStruct)) {
        return new AStruct(opts);
    }
    this.typeid = TYPE.STRUCT;
 
    Iif (opts.fields === undefined) {
        throw new Error('AStruct requires fields');
    }
    this.name = opts.name || null;
    this.fields = opts.fields;
    this.fieldsById = {};
    this.fieldsByName = {};
    this.fieldNames = [];
    for (var index = 0; index < this.fields.length; index++) {
        var field = this.fields[index];
        this.fieldsById[field.id] = field;
        this.fieldsByName[field.name] = field;
        this.fieldNames[index] = field.name;
    }
}
 
AStruct.prototype.reify = function reify(tstruct) {
    var result = {};
    for (var index = 0; index < tstruct.fields.length; index++) {
        var tfield = tstruct.fields[index];
        var afield = this.fieldsById[tfield.id];
        Iif (!afield) {
            continue;
        }
        Iif (afield.type.typeid !== tfield.typeid) {
            return ret.error(new Error(util.format(
                'AStruct::reify expects field %d typeid %d; received %d',
                tfield.id, afield.type.typeid, tfield.typeid)));
        }
        var t = afield.type.reify(tfield.val);
        if (t.error) {
            return ret.chain(t, {
                type: 'astruct',
                field: afield
            });
        }
        result[afield.name] = t.value;
    }
    return ret.just(result);
};
 
AStruct.prototype.uglify = function uglify(struct) {
    var tstruct = new thriftrw.TStruct();
    for (var index = 0; index < this.fields.length; index++) {
        var field = this.fields[index];
        var value = struct[field.name];
        if (value == null) {
            Iif (field.required) {
                return ret.error(new Error(util.format('typename %s; missing required field %s',
                    this.name, field.name)));
            }
        } else {
            var t = field.type.uglify(value);
            if (t.error) {
                return ret.chain(t, {
                    type: 'astruct',
                    field: field
                });
            }
            var tvalue = t.value;
            var tfield = new TField(field.type.typeid, field.id, tvalue);
            tstruct.fields.push(tfield);
        }
    }
    return ret.just(tstruct);
};
 
module.exports.AField = AField;
module.exports.AStruct = AStruct;