Skip to content

Commit

Permalink
Adding WellKnown types and using codec for serializers
Browse files Browse the repository at this point in the history
excelwebzone committed Aug 25, 2016
1 parent 37bcaee commit beac75a
Showing 60 changed files with 1,532 additions and 187 deletions.
18 changes: 10 additions & 8 deletions dist/pbj.min.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -4,7 +4,9 @@
"description": "Pbj library for es6",
"license": "Apache-2.0",
"dependencies": {
"gdbots-common": "git://github.com/gdbots/common-js.git#master"
"gdbots-common": "git://github.com/gdbots/common-js.git#master",
"bignumber.js": "~2.3",
"uuid": "~2.0"
},
"devDependencies": {
"chai": "~3.5",
89 changes: 89 additions & 0 deletions src/codec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
'use strict';

import DynamicField from 'gdbots/pbj/well-known/dynamic-field';
import GeoPoint from 'gdbots/pbj/well-known/geo-point';
import Message from 'gdbots/pbj/message';
import MessageRef from 'gdbots/pbj/message-ref';

export default class Codec
{
/**
* @param Message message
* @param Field field
*
* @return mixed
*/
encodeMessage(message, field) {
throw message.toArray();
}

/**
* @param mixed value
* @param Field field
*
* @return Message
*/
decodeMessage(value, field) {
return Message.fromArray(value);
}

/**
* @param MessageRef messageRef
* @param Field field
*
* @return mixed
*/
encodeMessageRef(messageRef, field) {
return messageRef.toArray();
}

/**
* @param mixed value
* @param Field field
*
* @return MessageRef
*/
decodeMessageRef(value, field) {
return MessageRef.fromArray(value);
}

/**
* @param GeoPoint geoPoint
* @param Field field
*
* @return mixed
*/
encodeGeoPoint(geoPoint, field) {
return [geoPoint.getLongitude(), geoPoint.getLatitude()];
}

/**
* @param mixed value
* @param Field field
*
* @return GeoPoint
*/
decodeGeoPoint(value, field) {
return new GeoPoint(value[1], value[0]);
}

/**
* @param DynamicField dynamicField
* @param Field field
*
* @return mixed
*/
encodeDynamicField(dynamicField, field) {
dynamicField.toArray();
}

/**
* @param mixed value
* @param Field field
*
* @return DynamicField
*/
decodeDynamicField(value, field) {
DynamicField.fromArray(value);
}
}
23 changes: 23 additions & 0 deletions src/enum/dynamic-field-kind.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

import Enum from 'gdbots/common/enum';
import SystemUtils from 'gdbots/common/util/system-utils';

/**
* @method static DynamicFieldKind BOOL_VAL()
* @method static DynamicFieldKind DATE_VAL()
* @method static DynamicFieldKind FLOAT_VAL()
* @method static DynamicFieldKind INT_VAL()
* @method static DynamicFieldKind STRING_VAL()
* @method static DynamicFieldKind TEXT_VAL()
*/
export default class DynamicFieldKind extends SystemUtils.mixinClass(Enum) {}

DynamicFieldKind.initEnum({
BOOL_VAL: 'bool_val',
DATE_VAL: 'date_val',
FLOAT_VAL: 'float_val',
INT_VAL: 'int_val',
STRING_VAL: 'string_val',
TEXT_VAL: 'text_val'
});
2 changes: 2 additions & 0 deletions src/enum/type-name.js
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ import SystemUtils from 'gdbots/common/util/system-utils';
* @method static TypeName DATE()
* @method static TypeName DATE_TIME()
* @method static TypeName DECIMAL()
* @method static TypeName DYNAMIC_FIELD()
* @method static TypeName FLOAT()
* @method static TypeName GEO_POINT()
* @method static TypeName IDENTIFIER()
@@ -46,6 +47,7 @@ TypeName.initEnum({
DATE: 'date',
DATE_TIME: 'date-time',
DECIMAL: 'decimal',
DYNAMIC_FIELD: 'dynamic-field',
FLOAT: 'float',
GEO_POINT: 'geo-point',
IDENTIFIER: 'identifier',
4 changes: 2 additions & 2 deletions src/exception/decode-value-failed.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

import SystemUtils from 'gdbots/common/util/system-utils';
import GdbotsPbjException from 'gdbots/pbj/exception/gdbots-pbj-exception';
import InvalidArgumentException from 'gdbots/pbj/exception/invalid-argument-exception';

/**
* Holds private properties
@@ -10,7 +10,7 @@ import GdbotsPbjException from 'gdbots/pbj/exception/gdbots-pbj-exception';
*/
let privateProps = new WeakMap();

export default class DecodeValueFailed extends SystemUtils.mixinClass(GdbotsPbjException)
export default class DecodeValueFailed extends SystemUtils.mixinClass(InvalidArgumentException)
{
/**
* @param mixed value
4 changes: 2 additions & 2 deletions src/exception/encode-value-failed.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

import SystemUtils from 'gdbots/common/util/system-utils';
import GdbotsPbjException from 'gdbots/pbj/exception/gdbots-pbj-exception';
import InvalidArgumentException from 'gdbots/pbj/exception/invalid-argument-exception';

/**
* Holds private properties
@@ -10,7 +10,7 @@ import GdbotsPbjException from 'gdbots/pbj/exception/gdbots-pbj-exception';
*/
let privateProps = new WeakMap();

export default class EncodeValueFailed extends SystemUtils.mixinClass(GdbotsPbjException)
export default class EncodeValueFailed extends SystemUtils.mixinClass(InvalidArgumentException)
{
/**
* @param mixed value
6 changes: 6 additions & 0 deletions src/exception/invalid-argument-exception.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

import SystemUtils from 'gdbots/common/util/system-utils';
import GdbotsPbjException from 'gdbots/pbj/exception/gdbots-pbj-exception';

export default class InvalidArgumentException extends SystemUtils.mixinClass(GdbotsPbjException) {}
4 changes: 2 additions & 2 deletions src/exception/invalid-schema-curie.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

import SystemUtils from 'gdbots/common/util/system-utils';
import GdbotsPbjException from 'gdbots/pbj/exception/gdbots-pbj-exception';
import InvalidArgumentException from 'gdbots/pbj/exception/invalid-argument-exception';

export default class InvalidSchemaCurie extends SystemUtils.mixinClass(GdbotsPbjException) {}
export default class InvalidSchemaCurie extends SystemUtils.mixinClass(InvalidArgumentException) {}
4 changes: 2 additions & 2 deletions src/exception/invalid-schema-id.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

import SystemUtils from 'gdbots/common/util/system-utils';
import GdbotsPbjException from 'gdbots/pbj/exception/gdbots-pbj-exception';
import InvalidArgumentException from 'gdbots/pbj/exception/invalid-argument-exception';

export default class InvalidSchemaId extends SystemUtils.mixinClass(GdbotsPbjException) {}
export default class InvalidSchemaId extends SystemUtils.mixinClass(InvalidArgumentException) {}
6 changes: 6 additions & 0 deletions src/exception/invalid-schema-q-name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

import SystemUtils from 'gdbots/common/util/system-utils';
import InvalidArgumentException from 'gdbots/pbj/exception/invalid-argument-exception';

export default class InvalidSchemaQName extends SystemUtils.mixinClass(InvalidArgumentException) {}
4 changes: 2 additions & 2 deletions src/exception/invalid-schema-version.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

import SystemUtils from 'gdbots/common/util/system-utils';
import GdbotsPbjException from 'gdbots/pbj/exception/gdbots-pbj-exception';
import InvalidArgumentException from 'gdbots/pbj/exception/invalid-argument-exception';

export default class InvalidSchemaVersion extends SystemUtils.mixinClass(GdbotsPbjException) {}
export default class InvalidSchemaVersion extends SystemUtils.mixinClass(InvalidArgumentException) {}
6 changes: 4 additions & 2 deletions src/message-ref.js
Original file line number Diff line number Diff line change
@@ -3,6 +3,8 @@
import SystemUtils from 'gdbots/common/util/system-utils';
import FromArray from 'gdbots/common/from-array';
import ToArray from 'gdbots/common/to-array';
import InvalidArgumentException from 'gdbots/pbj/exception/invalid-argument-exception';
import LogicException from 'gdbots/pbj/exception/logic-exception';
import SchemaCurie from 'gdbots/pbj/schema-curie';

/**
@@ -58,7 +60,7 @@ export default class MessageRef extends SystemUtils.mixinClass(null, FromArray,
}

if (privateProps.get(this).curie.isMixin()) {
throw new Error('Mixins cannot be used in a MessageRef.');
throw new LogicException('Mixins cannot be used in a MessageRef.');
}
}

@@ -73,7 +75,7 @@ export default class MessageRef extends SystemUtils.mixinClass(null, FromArray,
return new this(SchemaCurie.fromString(data.curie), id, tag);
}

throw new Error('Payload must be a MessageRef type.');
throw new InvalidArgumentException('Payload must be a MessageRef type.');
}

/**
9 changes: 9 additions & 0 deletions src/message-resolver.js
Original file line number Diff line number Diff line change
@@ -56,6 +56,15 @@ export default class MessageResolver
return _resolvedMixins;
}

/**
* Returns all of the registed schemas.
*
* @var Message[]
*/
static all() {
return _messages;
}

/**
* Returns the Message to be used for the provided schema id.
*
15 changes: 14 additions & 1 deletion src/schema-curie.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

import InvalidSchemaCurie from 'gdbots/pbj/exception/invalid-schema-curie';
import SchemaQName from 'gdbots/pbj/schema-q-name';

/** @var array */
let _instances = {};
@@ -56,8 +57,13 @@ export default class SchemaCurie
message: message,

/** @var string */
curie: vendor + ':' + packageName + ':' + category + ':' + message
curie: vendor + ':' + packageName + ':' + category + ':' + message,

/** @var SchemaQName */
qname: null
});

privateProps.get(this).qname = SchemaQName.fromCurie(this);
}

/**
@@ -142,4 +148,11 @@ export default class SchemaCurie
isMixin() {
return 'mixin' === privateProps.get(this).category;
}

/**
* @return SchemaQName
*/
getQName() {
return privateProps.get(this).qname;
}
}
Loading

0 comments on commit beac75a

Please sign in to comment.