@eclipse-ditto/ditto-javascript-client-dom
Version:
DOM implementation of Eclipse Ditto JavaScript API to be used in browsers.
189 lines • 5.44 kB
JavaScript
/*!
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
import { EntityModel, EntityWithId, IndexedEntityModel } from './model';
/**
* Representation of a Thing
*/
export class Thing extends EntityWithId {
constructor(_thingId, _policyId, _attributes, _features, __revision, __modified, _definition, __metadata, __created) {
super();
this._thingId = _thingId;
this._policyId = _policyId;
this._attributes = _attributes;
this._features = _features;
this.__revision = __revision;
this.__modified = __modified;
this._definition = _definition;
this.__metadata = __metadata;
this.__created = __created;
}
/**
* Parses a Thing.
*
* @param o - The object to parse.
* @returns The Thing
*/
static fromObject(o) {
if (o === undefined) {
return o;
}
// @ts-ignore
return new Thing(o['thingId'], o['policyId'], o['attributes'], Features.fromObject(o['features']), o['_revision'], o['_modified'], o['definition'], Metadata.fromObject(o['_metadata']), o['_created']);
}
static empty() {
return new Thing('', '', undefined, undefined, 0, '', undefined, undefined, undefined);
}
toObject() {
const featuresObj = Features.toObject(this.features);
return EntityModel.buildObject(new Map([
['thingId', this.thingId],
['policyId', this.policyId],
['attributes', this.attributes],
['features', featuresObj],
['_revision', this._revision],
['_modified', this._modified],
['definition', this._definition],
['_created', this.__created]
]));
}
get thingId() {
return this._thingId;
}
get id() {
return this._thingId;
}
get policyId() {
return this._policyId;
}
get attributes() {
return this._attributes;
}
get features() {
return this._features;
}
get _modified() {
return this.__modified;
}
get _revision() {
return this.__revision;
}
get _metadata() {
return this.__metadata;
}
get namespace() {
return this.separateNamespaceAndThingId().namespace;
}
get name() {
return this.separateNamespaceAndThingId().name;
}
get definition() {
return this._definition;
}
get _created() {
return this.__created;
}
separateNamespaceAndThingId() {
const indexOfFirstColon = this.thingId.indexOf(':');
if (indexOfFirstColon >= 0) {
const namespace = this.thingId.substring(0, indexOfFirstColon);
const name = this.thingId.length === indexOfFirstColon ? '' : this.thingId.substring(indexOfFirstColon + 1);
return { namespace, name };
}
return { namespace: '', name: this.thingId };
}
}
Thing.NAMESPACE_SEPARATION_REGEX = /([^:]*):(.*)/;
/**
* Representation of Features
*/
export class Features extends IndexedEntityModel {
/**
* Parses Features.
*
* @param o - The object to parse.
* @returns The Features
*/
static fromObject(o) {
if (o === undefined) {
return o;
}
return IndexedEntityModel.fromPlainObject(o, Feature.fromObject);
}
}
export class Metadata extends EntityModel {
constructor(_attributes, _features) {
super();
this._attributes = _attributes;
this._features = _features;
}
get attributes() {
return this._attributes;
}
get features() {
return this._features;
}
static fromObject(o) {
if (o === undefined) {
return o;
}
return new Metadata(o.attributes, Features.fromObject(o.features));
}
toObject() {
const features = this.features ? Features.toObject(this.features) : undefined;
return EntityModel.buildObject(new Map([
['features', features],
['attributes', this.attributes]
]));
}
}
/**
* Representation of a Feature
*/
export class Feature extends EntityWithId {
constructor(_id, _definition, _properties) {
super();
this._id = _id;
this._definition = _definition;
this._properties = _properties;
}
/**
* Parses a Feature.
*
* @param o - The object to parse.
* @param key - The key of the new Feature.
* @returns The Feature
*/
static fromObject(o, key) {
if (o === undefined) {
return o;
}
// @ts-ignore
return new Feature(key, o['definition'], o['properties']);
}
toObject() {
return EntityModel.buildObject(new Map([
['definition', this.definition],
['properties', this.properties]
]));
}
get id() {
return this._id;
}
get definition() {
return this._definition;
}
get properties() {
return this._properties;
}
}
//# sourceMappingURL=things.model.js.map