Skip to content
Snippets Groups Projects
Select Git revision
  • develop default protected
  • simmc-based
  • drill-experiment
  • tg-felipe
  • issue/97
  • issue/63
  • icde-2019-experiments
  • issue/85
  • master protected
  • issue/20
  • refactor/engine
  • issue/6
  • feature/diagrams
  • wip-transformers
14 results

source.ts

Blame
  • source.ts 4.71 KiB
    /*
     * Copyright (C) 2015 Centro de Computacao Cientifica e Software Livre
     * Departamento de Informatica - Universidade Federal do Parana
     *
     * This file is part of blendb.
     *
     * blendb is free software: you can redistribute it and/or modify
     * it under the terms of the GNU General Public License as published by
     * the Free Software Foundation, either version 3 of the License, or
     * (at your option) any later version.
     *
     * blendb is distributed in the hope that it will be useful,
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     * GNU General Public License for more details.
     *
     * You should have received a copy of the GNU General Public License
     * along with blendb.  If not, see <http://www.gnu.org/licenses/>.
     */
    
    import { EnumType } from "./enumType";
    import { DataType } from "../common/types";
    
    /** Attribute of a source. */
    export interface Field {
        /** Field name. */
        name: string;
        /** Brief description of what this field represent. */
        description?: string;
        /* Field data type. */
        dataType: DataType;
        /* Enumerable type name, used if data type is enumerable type. */
        enumType?: string;
    }
    
    /** Field definition used in configuration file. */
    export interface FieldStr {
        /** Field name. */
        name: string;
        /** Brief description of what this field represent. */
        description?: string;
        /* Field data type. */
        dataType: string;
    }
    
    /** Parameters used to create a source object. */
    export interface SourceOptions {
        /** Source name. */
        name: string;
        /** Brief description of what this source represent. */
        description?: string;
        /** Set of fields that define the attributes of this source. */
        fields: Field[];
    }
    
    /**
     * Parameters used to define source object in the configuration file.
     * Also the string description of a source.
     */
    export interface SourceStrOptions {
        /** Source name. */
        name: string;
        /** Brief description of what this field represent. */
        description?: string;
        /** Set of fields that define the attributes of this source. */
        fields: FieldStr[];
    }
    
    /**
     * A definition of a "object" that can be inserted into BlenDB.
     * To perform a insertion in the database is required to know "what"
     * is being inserted. This "what" is a source object that is
     * nothing more than a set o attributes (called fields) and
     * a identifier (called name).
     */
    export class Source {
        /** Source name. */
        public readonly name: string;
        /** Brief description of what this field represent. */
        public readonly description: string;
        /** Set of fields that define the attributes of this source. */
        public readonly fields: Field[];
    
        /**
         * Create a source.
         * @param options - Parameters required to create a source.
         */
        constructor(options: SourceOptions) {
            this.name = options.name;
            this.description = (options.description) ? options.description : "";
            this.fields = options.fields.map((item) => {
                return {
                    name: item.name,
                    description: (item.description) ? item.description : "",
                    dataType: item.dataType,
                    enumType: (item.enumType) ? item.enumType : ""
                };
            });
        }
    
        /**
         * Created a object with the same options used to create this
         * source as strings. Used to inform the API users.
         */
        public strOptions(): SourceStrOptions {
            return {
                name: this.name,
                description: this.description,
                fields: Source.stringfyFieldDataType(this.fields),
            };
        }
    
        /**
         * Created a set of objects with the same options used to create the
         * given fields as strings. Used to inform the API users.
         * @param opts - Set of fields to be put in string struct.
         */
        public static stringfyFieldDataType(opts: Field[]): FieldStr[] {
            let str: FieldStr[];
            str = opts.map((i) => {
                return {
                    name : i.name,
                    description: i.description,
                    dataType: (i.dataType !== DataType.NONE) ? EnumType.stringfyDataType(i.dataType) : i.enumType
                };
            });
            return str;
        }
    
        /**
         * Created a set of fields given its options.
         * @param opts - Set of fields in string struct.
         */
        public static parseFieldDataType(opts: FieldStr[]): Field[] {
            let str: Field[];
            str = opts.map((i) => {
                return {
                    name : i.name,
                    description: i.description,
                    dataType: EnumType.parseDataType(i.dataType),
                    enumType: (EnumType.parseDataType(i.dataType) === DataType.NONE) ? i.dataType : ""
                };
            });
            return str;
        }
    }