CoolFace
Datasetpublic

basant307/AI_Governance_Project

sourceHugging Faceapache-2.0updated 2mo agoView on Hugging Face
0likes48downloads
mapfield.js127 linesDownload Raw Back to src
1"use strict";2module.exports = MapField;3 4// extends Field5var Field = require("./field");6((MapField.prototype = Object.create(Field.prototype)).constructor = MapField).className = "MapField";7 8var types   = require("./types"),9    util    = require("./util");10 11/**12 * Constructs a new map field instance.13 * @classdesc Reflected map field.14 * @extends FieldBase15 * @constructor16 * @param {string} name Unique name within its namespace17 * @param {number} id Unique id within its namespace18 * @param {string} keyType Key type19 * @param {string} type Value type20 * @param {Object.<string,*>} [options] Declared options21 * @param {string} [comment] Comment associated with this field22 */23function MapField(name, id, keyType, type, options, comment) {24    Field.call(this, name, id, type, undefined, undefined, options, comment);25 26    /* istanbul ignore if */27    if (!util.isString(keyType))28        throw TypeError("keyType must be a string");29 30    /**31     * Key type.32     * @type {string}33     */34    this.keyType = keyType; // toJSON, marker35 36    /**37     * Resolved key type if not a basic type.38     * @type {ReflectionObject|null}39     */40    this.resolvedKeyType = null;41 42    // Overrides Field#map43    this.map = true;44}45 46/**47 * Map field descriptor.48 * @interface IMapField49 * @extends {IField}50 * @property {string} keyType Key type51 */52 53/**54 * Extension map field descriptor.55 * @interface IExtensionMapField56 * @extends IMapField57 * @property {string} extend Extended type58 */59 60/**61 * Constructs a map field from a map field descriptor.62 * @param {string} name Field name63 * @param {IMapField} json Map field descriptor64 * @returns {MapField} Created map field65 * @throws {TypeError} If arguments are invalid66 */67MapField.fromJSON = function fromJSON(name, json) {68    return new MapField(name, json.id, json.keyType, json.type, json.options, json.comment);69};70 71/**72 * Converts this map field to a map field descriptor.73 * @param {IToJSONOptions} [toJSONOptions] JSON conversion options74 * @returns {IMapField} Map field descriptor75 */76MapField.prototype.toJSON = function toJSON(toJSONOptions) {77    var keepComments = toJSONOptions ? Boolean(toJSONOptions.keepComments) : false;78    return util.toObject([79        "keyType" , this.keyType,80        "type"    , this.type,81        "id"      , this.id,82        "extend"  , this.extend,83        "options" , this.options,84        "comment" , keepComments ? this.comment : undefined85    ]);86};87 88/**89 * @override90 */91MapField.prototype.resolve = function resolve() {92    if (this.resolved)93        return this;94 95    // Besides a value type, map fields have a key type that may be "any scalar type except for floating point types and bytes"96    if (types.mapKey[this.keyType] === undefined)97        throw Error("invalid key type: " + this.keyType);98 99    return Field.prototype.resolve.call(this);100};101 102/**103 * Map field decorator (TypeScript).104 * @name MapField.d105 * @function106 * @param {number} fieldId Field id107 * @param {"int32"|"uint32"|"sint32"|"fixed32"|"sfixed32"|"int64"|"uint64"|"sint64"|"fixed64"|"sfixed64"|"bool"|"string"} fieldKeyType Field key type108 * @param {"double"|"float"|"int32"|"uint32"|"sint32"|"fixed32"|"sfixed32"|"int64"|"uint64"|"sint64"|"fixed64"|"sfixed64"|"bool"|"string"|"bytes"|Object|Constructor<{}>} fieldValueType Field value type109 * @returns {FieldDecorator} Decorator function110 * @template T extends { [key: string]: number | Long | string | boolean | Uint8Array | Buffer | number[] | Message<{}> }111 */112MapField.d = function decorateMapField(fieldId, fieldKeyType, fieldValueType) {113 114    // submessage value: decorate the submessage and use its name as the type115    if (typeof fieldValueType === "function")116        fieldValueType = util.decorateType(fieldValueType).name;117 118    // enum reference value: create a reflected copy of the enum and keep reuseing it119    else if (fieldValueType && typeof fieldValueType === "object")120        fieldValueType = util.decorateEnum(fieldValueType).name;121 122    return function mapFieldDecorator(prototype, fieldName) {123        util.decorateType(prototype.constructor)124            .add(new MapField(fieldName, fieldId, fieldKeyType, fieldValueType));125    };126};127 
basant307/AI_Governance_Project · CoolFace