Skip to content

Commit

Permalink
refactor(ts): enable noImplicitAny on mutation.ts (googleapis#448)
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Cervino authored and JustinBeckwith committed Apr 12, 2019
1 parent 3949356 commit dff4580
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 57 deletions.
39 changes: 22 additions & 17 deletions src/chunktransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
* limitations under the License.
*/
import {Transform, TransformOptions} from 'stream';
import {Mutation} from './mutation';

import {Bytes, Mutation} from './mutation';

export type Value = string|number|boolean|Uint8Array;

Expand Down Expand Up @@ -148,9 +149,10 @@ export class ChunkTransformer extends Transform {
}
}
if (data.lastScannedRowKey && data.lastScannedRowKey.length > 0) {
this.lastRowKey = Mutation.convertFromBytes(data.lastScannedRowKey, {
userOptions: this.options,
});
this.lastRowKey =
Mutation.convertFromBytes(data.lastScannedRowKey as Bytes, {
userOptions: this.options,
});
}
next();
}
Expand Down Expand Up @@ -263,7 +265,7 @@ export class ChunkTransformer extends Transform {
validateRowInProgress(chunk: Chunk): void {
const row = this.row;
if (chunk.rowKey && chunk.rowKey.length) {
const newRowKey = Mutation.convertFromBytes(chunk.rowKey, {
const newRowKey = Mutation.convertFromBytes(chunk.rowKey as Bytes, {
userOptions: this.options,
});
const oldRowKey = row!.key || '';
Expand Down Expand Up @@ -324,7 +326,7 @@ export class ChunkTransformer extends Transform {
* @param {chunks} chunk chunk to process
*/
processNewRow(chunk: Chunk): void {
const newRowKey = Mutation.convertFromBytes(chunk.rowKey!, {
const newRowKey = Mutation.convertFromBytes(chunk.rowKey! as Bytes, {
userOptions: this.options,
}) as string;
this.validateNewRow(chunk, newRowKey);
Expand All @@ -333,12 +335,13 @@ export class ChunkTransformer extends Transform {
row!.key = newRowKey;
row!.data = {} as Data;
this.family = row!.data![chunk.familyName.value] = {} as Family;
const qualifierName = Mutation.convertFromBytes(chunk.qualifier.value, {
userOptions: this.options,
});
const qualifierName =
Mutation.convertFromBytes(chunk.qualifier.value as Bytes, {
userOptions: this.options,
});
this.qualifiers = this.family[qualifierName as {} as string] = [];
this.qualifier = {
value: Mutation.convertFromBytes(chunk.value!, {
value: Mutation.convertFromBytes(chunk.value! as Bytes, {
userOptions: this.options,
isPossibleNumber: true,
}) as string,
Expand Down Expand Up @@ -366,14 +369,15 @@ export class ChunkTransformer extends Transform {
row!.data![chunk.familyName.value] || {};
}
if (chunk.qualifier) {
const qualifierName = Mutation.convertFromBytes(chunk.qualifier.value, {
userOptions: this.options,
}) as string;
const qualifierName =
Mutation.convertFromBytes(chunk.qualifier.value as Bytes, {
userOptions: this.options,
}) as string;
this.qualifiers = this.family![qualifierName] =
this.family![qualifierName] || [];
}
this.qualifier = {
value: Mutation.convertFromBytes(chunk.value!, {
value: Mutation.convertFromBytes(chunk.value! as Bytes, {
userOptions: this.options,
isPossibleNumber: true,
}) as string,
Expand All @@ -394,9 +398,10 @@ export class ChunkTransformer extends Transform {
if (chunk.resetRow) {
return this.reset();
}
const chunkQualifierValue = Mutation.convertFromBytes(chunk.value!, {
userOptions: this.options,
});
const chunkQualifierValue =
Mutation.convertFromBytes(chunk.value! as Bytes, {
userOptions: this.options,
});

if (chunkQualifierValue instanceof Buffer &&
this.qualifier!.value instanceof Buffer) {
Expand Down
116 changes: 82 additions & 34 deletions src/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,50 @@ import arrify = require('arrify');
import * as is from 'is';
import * as Long from 'long';

import {google as btTypes} from '../proto/bigtable';

export type IMutation = btTypes.bigtable.v2.IMutation;
export type IMutateRowRequest = btTypes.bigtable.v2.IMutateRowRequest;
export type ISetCell = btTypes.bigtable.v2.Mutation.ISetCell;

export type Bytes = string|Buffer;
export type Data = Value|Value[]|MutationSettingsObj;
export type JsonObj = {
[k: string]: string|JsonObj
};
export type Value = string|number|boolean;

export interface ParsedColumn {
family: string|null;
qualifier: string|null;
}
export interface ConvertFromBytesOptions {
userOptions?: {decode?: boolean; encoding?: string;};
isPossibleNumber?: boolean;
}
export interface MutationConstructorObj {
key: string;
method: string;
data: Data;
}
export interface MutationSettingsObj {
follows?: ValueObj;
column?: string;
time?: {start: Date|number; end: Date | number;};
}
export interface TimeRange {
[k: string]: number|string|undefined;
startTimestampMicros?: number;
endTimestampMicros?: number;
}
export interface SetCellObj {
[k: string]: string|ISetCell|undefined;
setCell?: ISetCell;
}
export interface ValueObj {
[k: string]: Buffer|Value|ValueObj;
}

/**
* Formats table mutations to be in the expected proto format.
*
Expand All @@ -36,10 +80,10 @@ import * as Long from 'long';
* });
*/
export class Mutation {
key;
method;
data;
constructor(mutation) {
key: string;
method: string;
data: Data;
constructor(mutation: MutationConstructorObj) {
this.key = mutation.key;
this.method = mutation.method;
this.data = mutation.data;
Expand All @@ -58,9 +102,11 @@ export class Mutation {
* @returns {string|number|buffer}
* @private
*/
static convertFromBytes(bytes, options?) {
static convertFromBytes(bytes: Bytes, options?: ConvertFromBytesOptions):
Buffer|Value {
const buf = bytes instanceof Buffer ? bytes : Buffer.from(bytes, 'base64');
if (options && options.isPossibleNumber && buf.length === 8) {
// tslint:disable-next-line no-any
const num = Long.fromBytes(buf as any).toNumber();

if (Number.MIN_SAFE_INTEGER < num && num < Number.MAX_SAFE_INTEGER) {
Expand All @@ -84,17 +130,17 @@ export class Mutation {
* @returns {buffer}
* @private
*/
static convertToBytes(data) {
static convertToBytes(data: Buffer|Data): Buffer|Data {
if (data instanceof Buffer) {
return data;
}

if (is.number(data)) {
return Buffer.from(Long.fromNumber(data).toBytesBE());
return Buffer.from(Long.fromNumber(data as number).toBytesBE());
}

try {
return Buffer.from(data);
return Buffer.from(data as string);
} catch (e) {
return data;
}
Expand All @@ -108,15 +154,15 @@ export class Mutation {
* @returns {object}
* @private
*/
static createTimeRange(start, end) {
const range: any = {};
static createTimeRange(start: Date, end: Date): TimeRange {
const range: TimeRange = {};

if (is.date(start)) {
range.startTimestampMicros = start.getTime() * 1000;
range.startTimestampMicros = (start as Date).getTime() * 1000;
}

if (is.date(end)) {
range.endTimestampMicros = end.getTime() * 1000;
range.endTimestampMicros = (end as Date).getTime() * 1000;
}

return range;
Expand Down Expand Up @@ -154,8 +200,8 @@ export class Mutation {
* // ]
* @private
*/
static encodeSetCell(data) {
const mutations: any[] = [];
static encodeSetCell(data: Data): SetCellObj[] {
const mutations: SetCellObj[] = [];

Object.keys(data).forEach(familyName => {
const family = data[familyName];
Expand All @@ -180,7 +226,7 @@ export class Mutation {
columnQualifier: Mutation.convertToBytes(cellName),
timestampMicros: timestamp,
value: Mutation.convertToBytes(cell.value),
};
} as ISetCell;

mutations.push({setCell});
});
Expand Down Expand Up @@ -227,10 +273,9 @@ export class Mutation {
* // deleteFromRow: {}
* // }
*
* //-
* // It's also possible to specify a time range when deleting specific
* columns.
* //-
* // columns.
*
* Mutation.encodeDelete([
* {
* column: 'follows:gwashington',
Expand All @@ -242,7 +287,7 @@ export class Mutation {
* ]);
* @private
*/
static encodeDelete(data?) {
static encodeDelete(data?: Data|Data[]): IMutation[] {
if (!data) {
return [
{
Expand All @@ -251,14 +296,15 @@ export class Mutation {
];
}

return arrify(data).map(mutation => {
return (arrify(data) as Data[]).map(mutation => {
if (is.string(mutation)) {
mutation = {
column: mutation,
};
} as MutationSettingsObj;
}

const column = Mutation.parseColumnName(mutation.column);
const column =
Mutation.parseColumnName((mutation as MutationSettingsObj).column!);

if (!column.qualifier) {
return {
Expand All @@ -268,17 +314,19 @@ export class Mutation {
};
}

let timeRange;
let timeRange: TimeRange|undefined;

if (mutation.time) {
timeRange =
Mutation.createTimeRange(mutation.time.start, mutation.time.end);
if ((mutation as MutationSettingsObj).time) {
timeRange = Mutation.createTimeRange(
(mutation as MutationSettingsObj).time!.start as Date,
(mutation as MutationSettingsObj).time!.end as Date);
}

return {
deleteFromColumn: {
familyName: column.family,
columnQualifier: Mutation.convertToBytes(column.qualifier),
familyName: column.family!,
columnQualifier: Mutation.convertToBytes(column.qualifier) as
Uint8Array,
timeRange,
},
};
Expand All @@ -292,7 +340,7 @@ export class Mutation {
* @returns {object}
* @private
*/
static parse(mutation) {
static parse(mutation: Mutation): IMutateRowRequest {
if (!(mutation instanceof Mutation)) {
mutation = new Mutation(mutation);
}
Expand All @@ -314,8 +362,8 @@ export class Mutation {
* // }
* @private
*/
static parseColumnName(column) {
const parts = column.split(':');
static parseColumnName(columnName: string): ParsedColumn {
const parts = columnName.split(':');

return {
family: parts[0],
Expand All @@ -329,11 +377,11 @@ export class Mutation {
* @returns {object}
* @private
*/
toProto() {
const mutation: any = {};
toProto(): IMutateRowRequest {
const mutation: IMutateRowRequest = {};

if (this.key) {
mutation.rowKey = Mutation.convertToBytes(this.key);
mutation.rowKey = Mutation.convertToBytes(this.key) as Uint8Array;
}

if (this.method === Mutation.methods.INSERT) {
Expand Down
2 changes: 1 addition & 1 deletion src/row.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ export class Row {
const column = Mutation.parseColumnName(rule.column);
const ruleData: any = {
familyName: column.family,
columnQualifier: Mutation.convertToBytes(column.qualifier),
columnQualifier: Mutation.convertToBytes(column.qualifier!),
};

if (rule.append) {
Expand Down
2 changes: 1 addition & 1 deletion src/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ Please use the format 'prezzy' or '${instance.name}/tables/prezzy'.`);
const lessThan = (lhs, rhs) => {
const lhsBytes = Mutation.convertToBytes(lhs);
const rhsBytes = Mutation.convertToBytes(rhs);
return lhsBytes.compare(rhsBytes) === -1;
return (lhsBytes as Buffer).compare(rhsBytes as Uint8Array) === -1;
};
const greaterThan = (lhs, rhs) => lessThan(rhs, lhs);
const greaterThanOrEqualTo = (lhs, rhs) => !lessThan(rhs, lhs);
Expand Down
9 changes: 5 additions & 4 deletions test/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
import * as assert from 'assert';
import * as Long from 'long';
import * as sn from 'sinon';
import {Mutation} from '../src/mutation.js';

import {IMutateRowRequest, Mutation} from '../src/mutation.js';

const sinon = sn.createSandbox();

Expand Down Expand Up @@ -115,7 +116,7 @@ describe('Bigtable/Mutation', function() {
it('should pack numbers into int64 values', function() {
const num = 10;
const encoded = Mutation.convertToBytes(num);
const decoded = Long.fromBytes(encoded).toNumber();
const decoded = Long.fromBytes(encoded as any).toNumber();

assert.strictEqual(num, decoded);
});
Expand Down Expand Up @@ -372,7 +373,7 @@ describe('Bigtable/Mutation', function() {
describe('parse', function() {
let toProto;
let toProtoCalled = false;
const fakeData = {a: 'a'};
const fakeData = {a: 'a'} as IMutateRowRequest;

before(function() {
toProto = Mutation.prototype.toProto;
Expand All @@ -391,7 +392,7 @@ describe('Bigtable/Mutation', function() {
key: 'a',
method: 'b',
data: 'c',
};
} as Mutation;

const mutation = Mutation.parse(fakeMutationData);

Expand Down

0 comments on commit dff4580

Please sign in to comment.