Skip to content

Commit

Permalink
feat: ResultSet serializaion and deserializaion (#836)
Browse files Browse the repository at this point in the history
* feat: ResultSet serializaion and deserializaion

* format fix
  • Loading branch information
vasilev-alex authored Jul 15, 2020
1 parent 28e8a4a commit 80b8d41
Show file tree
Hide file tree
Showing 8 changed files with 269 additions and 1,727 deletions.
12 changes: 12 additions & 0 deletions docs/Cube.js-Frontend/@cubejs-client-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,10 @@ You can find the examples of using the `pivotConfig` [here](#pivot-config)

**rawData**(): *T[]*

### serialize

**serialize**(): *Object*

### series

**series****SeriesItem**›(**pivotConfig?**: [PivotConfig](#types-pivot-config)): *[Series](#types-series)‹SeriesItem›[]*
Expand Down Expand Up @@ -513,6 +517,14 @@ For example:
]
```

### deserialize

`static` **deserialize****TData**›(**data**: Object, **options?**: Object): *[ResultSet](#result-set)‹TData›*

**Type parameters:**

- **TData**

### getNormalizedPivotConfig

`static` **getNormalizedPivotConfig**(**query**: [Query](#types-query), **pivotConfig?**: Partial‹[PivotConfig](#types-pivot-config)›): *[PivotConfig](#types-pivot-config)*
Expand Down
21 changes: 18 additions & 3 deletions packages/cubejs-client-core/dist/cubejs-client-core.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import { pipe, map, filter, reduce, minBy, maxBy, groupBy, equals, unnest, toPairs, uniq, fromPairs, dropLast } from 'ramda';
import { pipe, map, filter, reduce, minBy, maxBy, groupBy, equals, unnest, toPairs, uniq, fromPairs, dropLast, clone } from 'ramda';
import Moment from 'moment';
import momentRange from 'moment-range';
import 'core-js/modules/es.array.is-array';
Expand Down Expand Up @@ -96,12 +96,14 @@ var LocalDateRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z?$/;
var ResultSet =
/*#__PURE__*/
function () {
function ResultSet(loadResponse, options) {
function ResultSet(loadResponse) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

_classCallCheck(this, ResultSet);

options = options || {};
this.loadResponse = loadResponse;
this.parseDateMeasures = options.parseDateMeasures;
this.options = options;
}

_createClass(ResultSet, [{
Expand Down Expand Up @@ -644,6 +646,13 @@ function () {

return this.backwardCompatibleData;
}
}, {
key: "serialize",
value: function serialize() {
return {
loadResponse: clone(this.loadResponse)
};
}
}], [{
key: "timeDimensionMember",
value: function timeDimensionMember(td) {
Expand Down Expand Up @@ -725,6 +734,12 @@ function () {
value: function measureFromAxis(axisValues) {
return axisValues[axisValues.length - 1];
}
}, {
key: "deserialize",
value: function deserialize(data) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return new ResultSet(data.loadResponse, options);
}
}]);

return ResultSet;
Expand Down
19 changes: 17 additions & 2 deletions packages/cubejs-client-core/dist/cubejs-client-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,14 @@ var LocalDateRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z?$/;
var ResultSet =
/*#__PURE__*/
function () {
function ResultSet(loadResponse, options) {
function ResultSet(loadResponse) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

_classCallCheck(this, ResultSet);

options = options || {};
this.loadResponse = loadResponse;
this.parseDateMeasures = options.parseDateMeasures;
this.options = options;
}

_createClass(ResultSet, [{
Expand Down Expand Up @@ -650,6 +652,13 @@ function () {

return this.backwardCompatibleData;
}
}, {
key: "serialize",
value: function serialize() {
return {
loadResponse: ramda.clone(this.loadResponse)
};
}
}], [{
key: "timeDimensionMember",
value: function timeDimensionMember(td) {
Expand Down Expand Up @@ -731,6 +740,12 @@ function () {
value: function measureFromAxis(axisValues) {
return axisValues[axisValues.length - 1];
}
}, {
key: "deserialize",
value: function deserialize(data) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return new ResultSet(data.loadResponse, options);
}
}]);

return ResultSet;
Expand Down
60 changes: 51 additions & 9 deletions packages/cubejs-client-core/dist/cubejs-client-core.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -3005,7 +3005,7 @@
};
};

function AsyncIterator(generator, PromiseImpl) {
function AsyncIterator(generator) {
function invoke(method, arg, resolve, reject) {
var record = tryCatch(generator[method], generator, arg);

Expand All @@ -3016,14 +3016,14 @@
var value = result.value;

if (value && _typeof(value) === "object" && hasOwn.call(value, "__await")) {
return PromiseImpl.resolve(value.__await).then(function (value) {
return Promise.resolve(value.__await).then(function (value) {
invoke("next", value, resolve, reject);
}, function (err) {
invoke("throw", err, resolve, reject);
});
}

return PromiseImpl.resolve(value).then(function (unwrapped) {
return Promise.resolve(value).then(function (unwrapped) {
// When a yielded Promise is resolved, its final value becomes
// the .value of the Promise<{value,done}> result for the
// current iteration.
Expand All @@ -3041,7 +3041,7 @@

function enqueue(method, arg) {
function callInvokeWithMethodAndArg() {
return new PromiseImpl(function (resolve, reject) {
return new Promise(function (resolve, reject) {
invoke(method, arg, resolve, reject);
});
}
Expand Down Expand Up @@ -3078,9 +3078,8 @@
// AsyncIterator objects; they just return a Promise for the value of
// the final result produced by the iterator.

exports.async = function (innerFn, outerFn, self, tryLocsList, PromiseImpl) {
if (PromiseImpl === void 0) PromiseImpl = Promise;
var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList), PromiseImpl);
exports.async = function (innerFn, outerFn, self, tryLocsList) {
var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList));
return exports.isGeneratorFunction(outerFn) ? iter // If outerFn is a generator, return the full iterator.
: iter.next().then(function (result) {
return result.done ? result.value : iter.next();
Expand Down Expand Up @@ -7408,6 +7407,34 @@
}
}

/**
* Creates a deep copy of the value which may contain (nested) `Array`s and
* `Object`s, `Number`s, `String`s, `Boolean`s and `Date`s. `Function`s are
* assigned by reference rather than copied
*
* Dispatches to a `clone` method if present.
*
* @func
* @memberOf R
* @since v0.1.0
* @category Object
* @sig {*} -> {*}
* @param {*} value The object or array to clone
* @return {*} A deeply cloned copy of `val`
* @example
*
* const objects = [{}, {}, {}];
* const objectsClone = R.clone(objects);
* objects === objectsClone; //=> false
* objects[0] === objectsClone[0]; //=> false
*/

var clone =
/*#__PURE__*/
_curry1(function clone(value) {
return value != null && typeof value.clone === 'function' ? value.clone() : _clone(value, [], [], true);
});

/**
* A function that returns the `!` of its argument. It will return `true` when
* passed false-y value, and `false` when passed a truth-y one.
Expand Down Expand Up @@ -15983,12 +16010,14 @@
var ResultSet =
/*#__PURE__*/
function () {
function ResultSet(loadResponse, options) {
function ResultSet(loadResponse) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

_classCallCheck(this, ResultSet);

options = options || {};
this.loadResponse = loadResponse;
this.parseDateMeasures = options.parseDateMeasures;
this.options = options;
}

_createClass(ResultSet, [{
Expand Down Expand Up @@ -16531,6 +16560,13 @@

return this.backwardCompatibleData;
}
}, {
key: "serialize",
value: function serialize() {
return {
loadResponse: clone(this.loadResponse)
};
}
}], [{
key: "timeDimensionMember",
value: function timeDimensionMember(td) {
Expand Down Expand Up @@ -16612,6 +16648,12 @@
value: function measureFromAxis(axisValues) {
return axisValues[axisValues.length - 1];
}
}, {
key: "deserialize",
value: function deserialize(data) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return new ResultSet(data.loadResponse, options);
}
}]);

return ResultSet;
Expand Down
3 changes: 3 additions & 0 deletions packages/cubejs-client-core/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ declare module '@cubejs-client/core' {
*/
static measureFromAxis(axisValues: string[]): string;
static getNormalizedPivotConfig(query: Query, pivotConfig?: Partial<PivotConfig>): PivotConfig;
static deserialize<TData>(data: Object, options?: Object): ResultSet<TData>;

/**
* Creates a new instance of ResultSet based on [LoadResponse](#load-response) data.
Expand All @@ -241,6 +242,8 @@ declare module '@cubejs-client/core' {
*/
constructor(loadResponse: LoadResponse<T>, options?: Object);

serialize(): Object;

/**
* @hidden
*/
Expand Down
16 changes: 13 additions & 3 deletions packages/cubejs-client-core/src/ResultSet.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
groupBy, pipe, fromPairs, toPairs, uniq, filter, map, unnest, dropLast, equals, reduce, minBy, maxBy
groupBy, pipe, fromPairs, toPairs, uniq, filter, map, unnest, dropLast, equals, reduce, minBy, maxBy, clone
} from 'ramda';
import Moment from 'moment';
import momentRange from 'moment-range';
Expand Down Expand Up @@ -27,10 +27,10 @@ const DateRegex = /^\d\d\d\d-\d\d-\d\d$/;
const LocalDateRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z?$/;

class ResultSet {
constructor(loadResponse, options) {
options = options || {};
constructor(loadResponse, options = {}) {
this.loadResponse = loadResponse;
this.parseDateMeasures = options.parseDateMeasures;
this.options = options;
}

drillDown(drillDownLocator, pivotConfig) {
Expand Down Expand Up @@ -512,6 +512,16 @@ class ResultSet {
}
return this.backwardCompatibleData;
}

serialize() {
return {
loadResponse: clone(this.loadResponse)
};
}

static deserialize(data, options = {}) {
return new ResultSet(data.loadResponse, options);
}
}

export default ResultSet;
Loading

0 comments on commit 80b8d41

Please sign in to comment.