Skip to content

Commit

Permalink
Merge pull request #378 from stephenplusplus/spp--datastore-remove-re…
Browse files Browse the repository at this point in the history
…gisterKind

datastore - remove registerKind stub
  • Loading branch information
ryanseys committed Feb 3, 2015
2 parents cdd4138 + e6aeb95 commit cd9487a
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 144 deletions.
118 changes: 0 additions & 118 deletions lib/datastore/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,6 @@

'use strict';

/** @type {object} */
var entityMeta = {};

/** @const {regexp} Regular expression to verify a field name. */
var FIELD_NAME_REGEX = /^[A-Za-z]*$/;

/** @const {regexp} Regular expression to verify a Kind. */
var KIND_REGEX = /^[A-Za-z]*$/;

/** @const {regexp} Regular Expression to verify a namespace. */
var NAMESPACE_REGEX = /^[A-Za-z]*$/;

/** @const {object} Map for query operation -> operation protocol value. */
var OP_TO_OPERATOR = {
'=': 'EQUAL',
Expand All @@ -43,16 +31,6 @@ var OP_TO_OPERATOR = {
'HAS_ANCESTOR': 'HAS_ANCESTOR'
};

/** @const {array} A list of native objects. */
var PRIMITIVE_KINDS = [
Object,
Boolean,
Number,
String,
Date,
Buffer
];

/** @const {object} Conversion map for query sign -> order protocol value. */
var SIGN_TO_ORDER = {
'-': 'DESCENDING',
Expand Down Expand Up @@ -592,102 +570,6 @@ function queryToQueryProto(q) {

module.exports.queryToQueryProto = queryToQueryProto;

/**
* Register a kind and its field metadata globally. In order to perform CRUD
* operations for a kind, you should register it with its field metadata.
*
* @private
*
* @todo Validate namespace, kind, and fieldMeta.
*
* @param {string} namespace - Namespace of the kind.
* @param {string} kind - Name of the kind.
* @param {object} fieldMeta - Metadata information about the fields.
*
* @example
* registerKind('namespace', 'Author', {
* name: { kind: String, indexed: false },
* tags: { kind: String, multi: true }, // an array of string elements.
* favArticles: { kind: KEY, multi: true },
* contact: {
* kind: {
* telephone: { kind: String },
* email: { kind: String }
* }
* }
* });
*/
function registerKind(namespace, kind, fieldMeta) {
// validate the input and put it to the dictionary
namespace = namespace || '';
if (!NAMESPACE_REGEX.test(namespace)) {
throw new Error('Namespaces should match ' + NAMESPACE_REGEX);
}
if (!KIND_REGEX.test(kind)) {
throw new Error('Kinds should match ' + KIND_REGEX);
}

Object.keys(fieldMeta).forEach(function(fieldName) {
validateField(fieldName, fieldMeta[fieldName]);
});

if (!entityMeta[namespace]) {
entityMeta[namespace] = {};
}
entityMeta[namespace][kind] = fieldMeta;
}

module.exports.registerKind = registerKind;

/**
* Get a Kind object.
*
* @private
*
* @param {string} namespace - The namespace of the kind.
* @param {string} kind - The Datstore Kind.
* @return {object}
*/
function getKind(namespace, kind) {
return entityMeta[namespace] && entityMeta[namespace][kind];
}

module.exports.getKind = getKind;

/**
* Validate a field.
*
* @throws Throws an Error if the field doesn't validate.
*
* @param {string} name - Field name.
* @param {object} field - Field metadata object.
* @param {string} field.key - Field key.
* @param {*} field.kind - Field Kind.
*
* @example
* validateField('title', {
* kind: String
* });
* // undefined (no errors thrown.)
*/
function validateField(name, field) {
if (!FIELD_NAME_REGEX.test(name)) {
throw new Error('Field name should match ' + FIELD_NAME_REGEX);
}
if (!field.kind) {
throw new Error('Provide a kind for field ' + name);
}
if (typeof field.kind !== 'object' &&
PRIMITIVE_KINDS.indexOf(field.kind) === -1) {
throw new Error('Unknown kind for field ' + name);
}
if (typeof field.kind === 'object') {
Object.keys(field.key).forEach(function(key) {
validateField(key, field.key[key]);
});
}
}

/**
* Does a value exist?
*
Expand Down
26 changes: 0 additions & 26 deletions test/datastore/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@ var entity = require('../../lib/datastore/entity.js');
var datastore = require('../../lib/datastore');
var ByteBuffer = require('bytebuffer');

var blogPostMetadata = {
title: { kind: String, indexed: true },
tags: { kind: String, multi: true, indexed: true },
publishedAt: { kind: Date },
author: { kind: Object, indexed: true },
isDraft: { kind: Boolean, indexed: true }
};

var entityProto = {
'property': [{
'name': 'linkedTo',
Expand Down Expand Up @@ -121,24 +113,6 @@ var queryFilterProto = {
group_by: []
};

describe('registerKind', function() {
it('should be able to register valid field metadata', function() {
entity.registerKind('namespace', 'kind', blogPostMetadata);
});

it('should set the namespace to "" if zero value or null', function() {
entity.registerKind(null, 'kind', blogPostMetadata);
var meta = entity.getKind('', 'kind');
assert.strictEqual(meta, blogPostMetadata);
});

it('should throw an exception if an invalid kind', function() {
assert.throws(function() {
entity.registerKind(null, '000', blogPostMetadata);
}, /Kinds should match/);
});
});

describe('keyFromKeyProto', function() {
var proto = {
partition_id: { namespace: '', dataset_id: 'datasetId' },
Expand Down

0 comments on commit cd9487a

Please sign in to comment.