1.0.0 • Published 8 years ago

qe-namespace v1.0.0

Weekly downloads
2
License
MIT
Repository
github
Last release
8 years ago

Qlik Engine Structs Documentation parser and class builder

Project consists of two parts:

  • Script for uploading and parsing Qlik Engine API Structs Documentation into json files
  • Script for generating namespace of JavaScript classes that encapsulate JSON structure of Qlik Engine API data sets and provide some type checking

Installing

npm install qe-namespace

Uploading and parsing Qlik Engine Documentation

const qen = require("qe-namespace");
qen.loadStructs("September2017");
  • Function loadStructs(${version}) will extract struct list from Struct Overview page
  • Then each struct page will be parsed into ./lib/qlikDocs/${version}/structs/${structName}.json
  • At the end all structures are merged into ./lib/qlikDocs/${version}/structs.json

Here is an example of some properties extracted from HyperCubeDef page and saved into HyperCubeDef.json:

{
    "qDimensions": {
        "desc": "Array of dimensions.",
        "class": "NxDimension",
        "type": "array"
    },
    "qMeasures": {
        "desc": "Array of measures.",
        "class": "NxMeasure",
        "type": "array"
    },
    "qInitialDataFetch": {
        "desc": "Initial data set.",
        "class": "NxPage",
        "type": "array"
    },
    "qMode": {
        "desc": "Defines the way the data are handled internally by the engine.\nDefault value is DATA_MODE_STRAIGHT.\nA pivot table can contain several dimensions and measures whereas a stacked pivot table can contain several dimensions but only one measure.\n",
        "values": {
            "S": "for straight table representation; DATA_MODE_STRAIGHT",
            "P": "for pivot table representation; DATA_MODE_PIVOT",
            "K": "for stacked table representation; DATA_MODE_PIVOT_STACK",
            "T": "for tree representation; DATA_MODE_TREE"
        },
        "type": "primitive"
    },
    "qSortbyYValue": {
        "desc": "To enable the sorting by ascending or descending order in the values of a measure. \nThis property applies to pivot tables and stacked pivot tables.\nIn the case of a pivot table, the measure or pseudo dimension should be defined as a top dimension. The sorting is restricted to the values of the first measure in a pivot table.\n",
        "values": {
            "0": "for no sorting",
            "1": "for sorting ascending",
            "-1": "for sorting descending"
        },
        "type": "primitive"
    }
}

Attributes class and values could be used for instance and possible value checking in setter functions.

Building Qlik Engine Struct namespace

node.js case:

const fs = require("fs");
const qen = require("qe-namespace");

var json = fs.readFileSync("./node_modules/qe-namespace/lib/qlikDocs/September2017/structs.json");
var n = qen.buildNamespace(json);
var hyperCubeDef = new n.HyperCubeDef;

require.js case:

define([
    "text!structs.json",
    "qe-namespace"
], function(
    json,
    qen
){
    var n = qen.buildNamespace(json);
    var hyperCubeDef = new n.HyperCubeDef;
})

In this implementation each constructor adds to result object .set and .init collections for setting and initialisation functions. This collections are costructed with Function expression, so JSON.parse ingore them.

var format = new st.FileDataFormat;
format.set("qType", "CSV")     //need to surf through documentation for possible properties
format.set.qType("CSV")        //no need to surf through documentation for possible properties
format.qType === "CSV" // true

For properties, which class presented in namespace, constructor adds initialisation function into .init collection:

var dim = new st.NxDimension;
dim.init.qDef() // equal to dim.set.qDef(new st.NxInlineDimensionDef)
format.qDef instanseof NxInlineDimensionDef // true

Also setters check instanceof for setting value:

dim.set.qDef({});
//TypeError: setted object is not an instance of NxInlineDimensionDef

You can awoid instanceof checking by setting value directly to the property:

dim.qDef = { qFieldDefs:[] };

If property has values attribute, setter will check for possible values:

var format = new st.FileDataFormat;
format.set.qType("123")
/*
TypeError: wrong value setted. posible values:
{
    "CSV": "Delimited",
    "FIX": "Fixed Record",
    "DIF": "Data Interchange Format",
    "EXCEL_BIFF": "Microsoft Excel (XLS)",
    "EXCEL_OOXML": "Microsoft Excel (XLSX)",
    "HTMLfor HTML": "",
    "QVD": "QVD file",
    "XML": "XML",
    "QVX": "QVX file",
    "JSON": "JSON format",
    "KML": "KML file"
}
*/

Function .push for properties with type:array checks class of pushed value:

var cube = new st.HyperCubeDef;
cube.qDimensions.push({})
//TypeError: pushed value is not an instance of NxDimension

Stringify

Each constructor adds stringify function:

this.stringify = JSON.stringify.bind(null, this);

Fluent interface

Functions from .set collection return initial object:

var format = new st.FileDataFormat;
format.set.qType("CSV")
    .set.qComment("comment")
    .set.qCodePage("utf8")

Functions from .init collection return initialized object:

var dim = new st.NxDimension;
var dimDef = dim.init.qDef();

Function .push for properties type:array returns initial array:

var cube = new st.HyperCubeDef;
cube.qDimensions.push(new st.NxDimension)
    .push(new st.NxDimension)
    .push(new st.NxDimension);

If array items class presented in namespace, method .pushNew added. This method returns pushed object:

var cube = new st.HyperCubeDef;
cube.qInitialDataFetch.pushNew()
    .set.qLeft(0)
    .set.qTop(0)
    .set.qWidth(4)
    .set.qHeight(50);
cube.qDimensions.pushNew()
    .init.qDef()
        .qFieldDefs
            .push("field1")
            .push("field2")
            .push("field3")
            .push("field4");
console.log(cube.stringify(null, 4));        

Console:

{
    "qDimensions": [
        {
            "qAttributeExpressions": [],
            "qAttributeDimensions": [],
            "qDef": {
                "qFieldDefs": [
                    "field1",
                    "field2",
                    "field3",
                    "field4"
                ],
                "qFieldLabels": [],
                "qSortCriterias": [],
                "qNumberPresentations": []
            }
        }
    ],
    "qMeasures": [],
    "qInterColumnSortOrder": [],
    "qInitialDataFetch": [
        {
            "qLeft": 0,
            "qTop": 0,
            "qWidth": 4,
            "qHeight": 50
        }
    ]
}
1.0.0

8 years ago