Skip to content

Commit 2399080

Browse files
muraliQlogicshivanee-p
authored andcommitted
New errors for name / id checks in family constructor (#202)
* New errors for name / id checks in family constructor * Fixed linting issue * changes as per review comments * changes as per review comments
1 parent 983fbe8 commit 2399080

4 files changed

Lines changed: 27 additions & 39 deletions

File tree

handwritten/bigtable/src/family.js

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -48,34 +48,22 @@ class Family {
4848
this.bigtable = table.bigtable;
4949
this.table = table;
5050

51-
const name = Family.formatName_(table.name, id);
52-
53-
this.id = name.split('/').pop();
54-
this.name = name;
55-
}
56-
57-
/**
58-
* Format the Column Family name into the expected proto format.
59-
*
60-
* @private
61-
*
62-
* @param {string} tableName The full formatted table name.
63-
* @param {string} id The column family unique-identifier.
64-
* @returns {string}
65-
*
66-
* @example
67-
* Family.formatName_(
68-
* 'projects/p/zones/z/clusters/c/tables/t',
69-
* 'my-family'
70-
* );
71-
* // 'projects/p/zones/z/clusters/c/tables/t/columnFamilies/my-family'
72-
*/
73-
static formatName_(tableName, id) {
51+
var name;
7452
if (id.includes('/')) {
75-
return id;
53+
if (id.startsWith(`${table.name}/columnFamilies/`)) {
54+
name = id;
55+
} else {
56+
throw new Error(
57+
`Family id '${id}' is not formatted correctly.
58+
Please use the format 'follows' or '${table.name}/columnFamilies/my-family'.`
59+
);
60+
}
61+
} else {
62+
name = `${table.name}/columnFamilies/${id}`;
7663
}
7764

78-
return `${tableName}/columnFamilies/${id}`;
65+
this.name = name;
66+
this.id = name.split('/').pop();
7967
}
8068

8169
/**

handwritten/bigtable/src/table.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ Please use the format 'prezzy' or '${instance.name}/tables/prezzy'.`
278278
return;
279279
}
280280

281-
const family = this.family(resp.name);
281+
const family = this.family(id);
282282
family.metadata = resp;
283283

284284
callback(null, family, resp);

handwritten/bigtable/test/family.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,18 @@ describe('Bigtable/Family', function() {
8585
var family = new Family(TABLE, FAMILY_ID);
8686
assert.strictEqual(family.name, FAMILY_NAME);
8787
});
88-
});
89-
90-
describe('formatName_', function() {
91-
it('should format the column family name', function() {
92-
var formatted = Family.formatName_(TABLE.name, FAMILY_ID);
9388

94-
assert.strictEqual(formatted, FAMILY_NAME);
89+
it('should leave full family names unaltered and localize the id from the name', function() {
90+
var family = new Family(TABLE, FAMILY_NAME);
91+
assert.strictEqual(family.name, FAMILY_NAME);
92+
assert.strictEqual(family.id, FAMILY_ID);
9593
});
9694

97-
it('should not re-format the name', function() {
98-
var formatted = Family.formatName_(TABLE.name, FAMILY_ID);
99-
100-
assert.strictEqual(formatted, FAMILY_NAME);
95+
it('should throw if family id in wrong format', function() {
96+
var id = `/project/bad-project/instances/bad-instance/columnFamiles/${FAMILY_ID}`;
97+
assert.throws(function() {
98+
new Family(TABLE, id);
99+
}, Error);
101100
});
102101
});
103102

handwritten/bigtable/test/table.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ describe('Bigtable/Table', function() {
281281

282282
describe('createFamily', function() {
283283
var COLUMN_ID = 'my-column';
284+
var FAMILY_ID = 'test-family';
284285

285286
it('should throw if a id is not provided', function() {
286287
assert.throws(function() {
@@ -372,12 +373,12 @@ describe('Bigtable/Table', function() {
372373
callback(null, response);
373374
};
374375

375-
table.family = function(name) {
376-
assert.strictEqual(name, response.name);
376+
table.family = function(id) {
377+
assert.strictEqual(id, FAMILY_ID);
377378
return fakeFamily;
378379
};
379380

380-
table.createFamily(COLUMN_ID, function(err, family, apiResponse) {
381+
table.createFamily(FAMILY_ID, function(err, family, apiResponse) {
381382
assert.ifError(err);
382383
assert.strictEqual(family, fakeFamily);
383384
assert.strictEqual(family.metadata, response);

0 commit comments

Comments
 (0)