Skip to content
This repository has been archived by the owner on Jul 9, 2024. It is now read-only.

Commit

Permalink
refactor(gql): simplify lhsRewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
Shane Wilson committed Jun 11, 2015
1 parent 55105cf commit 19505be
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 82 deletions.
77 changes: 27 additions & 50 deletions app/scripts/components/gql/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ module ngApp.components.gql {
return _.last(this.getValuesOfList(s))
.trim()
.replace(this.GqlTokens.QUOTE, this.GqlTokens.NOTHING)
.replace(this.GqlTokens.LBRACKET, this.GqlTokens.NOTHING)
.replace(this.GqlTokens.LPARENS, this.GqlTokens.NOTHING);
}

Expand Down Expand Up @@ -115,8 +116,9 @@ module ngApp.components.gql {

getComplexParts(s: string, n: number): IParts {
var parts = this.getParts(s.substring(0, n));
parts.needle = this.getNeedleFromList(s.substring(n));

parts.needle = this.getNeedleFromList(s.substring(n));
console.log('here', parts);
return parts;
}

Expand Down Expand Up @@ -266,18 +268,8 @@ module ngApp.components.gql {
});
}

lhsTokenField(left: string): number {
// Fields can happen after a space ' ' or a paren '('
var lLastSpace = left.lastIndexOf(this.GqlTokens.SPACE);
var lLastParen = left.lastIndexOf(this.GqlTokens.LPARENS);
return lLastSpace > lLastParen ? lLastSpace : lLastParen;
}

lhsRewrite(left: string, mode: Mode): string {
var lLastToken: number = mode === Mode.Field ?
this.lhsTokenField(left) :
left.lastIndexOf(this.GqlTokens.SPACE);
return left.substring(0, lLastToken + 1);
lhsRewrite(left: string, needleLength: number): string {
return left.substring(0, left.length - needleLength)
}

rhsRewrite(right: string): string {
Expand All @@ -286,26 +278,11 @@ module ngApp.components.gql {
return right.substring(tokenIndex);
}

lhsRewriteQuoted(left: string): string {
var lLastQuote = left.lastIndexOf(this.GqlTokens.QUOTE);
return left.substring(0, lLastQuote);
}

rhsRewriteQuoted(right: string): string {
var rFirstSpace = right.search(/[a-z]"/i);
return right.substring(rFirstSpace + 2);
}

lhsRewriteList(left: string): string {
var lastBracket = left.lastIndexOf(this.GqlTokens.LBRACKET);
if (lastBracket === -1) {
return left.substring(0, left.lastIndexOf(this.GqlTokens.SPACE)) + " [";
}
var lastComma = left.lastIndexOf(this.GqlTokens.COMMA);
var lastToken = lastComma > lastBracket ? lastComma : lastBracket;
return left.substring(0, lastToken + 1);
}

rhsRewriteList(right: string): string {
var rFirstBracket = right.indexOf(this.GqlTokens.RBRACKET);
var rFirstComma = right.indexOf(this.GqlTokens.COMMA);
Expand Down Expand Up @@ -348,56 +325,57 @@ module ngApp.components.gql {
$scope.right = $scope.query.substring(index);
var left = $scope.left;
var right = $scope.right;
var parts = GqlService.getParts(left);
$scope.parts = GqlService.getParts(left);

if ($scope.Error && _.some($scope.Error.expected, (e): boolean => {
return [T.IN, T.AND].indexOf(e.value.toString()) !== -1;
})) {
$scope.mode = Mode.Op;

$scope.ddItems = GqlService.parseGrammarError(parts.needle, $scope.Error)
$scope.ddItems = GqlService.parseGrammarError($scope.parts.needle, $scope.Error)
} else if ($scope.Error && _.some($scope.Error.expected, (e): boolean => {
return [T.MISSING].indexOf(e.value.toString()) !== -1;
})) {
$scope.mode = Mode.Unquoted;

$scope.ddItems = GqlService.parseGrammarError(parts.needle, $scope.Error)
$scope.ddItems = GqlService.parseGrammarError($scope.parts.needle, $scope.Error)
} else {
if ([T.IN, T.NOT + T.SPACE + T.IN].indexOf(parts.op) !== -1 ||
if ([T.IN, T.NOT + T.SPACE + T.IN].indexOf($scope.parts.op) !== -1 ||
GqlService.isUnbalanced(left, T.LBRACKET, T.RBRACKET)) {
// in_list_of_values
$scope.mode = Mode.List;
var ret: { parts: IParts; listValues: string[] } = GqlService.parseList(left, right);
GqlService.ajaxList(ret.parts, ret.listValues).then((d) => {
var ret: { parts: IParts; listValues: string[] } = GqlService.parseList(left, right);
$scope.parts = ret.parts;
GqlService.ajaxList($scope.parts, ret.listValues).then((d) => {
$scope.ddItems = d;
});
} else if (GqlService.isCountOdd(left, T.QUOTE)) {
//in_quoted_string
$scope.mode = Mode.Quoted;
parts = GqlService.parseQuoted(left);
GqlService.ajaxQuoted(parts).then((d) => {
$scope.parts = GqlService.parseQuoted(left);
GqlService.ajaxQuoted($scope.parts).then((d) => {
$scope.ddItems = d;
});
} else {
if ((parts.needle && !parts.op) || [T.AND, T.OR].indexOf(parts.op) !== -1) {
if (($scope.parts.needle && !$scope.parts.op) || [T.AND, T.OR].indexOf($scope.parts.op) !== -1) {
// is_field_string
$scope.mode = Mode.Field;

$scope.ddItems = _.take(_.filter(mapping, (m: IDdItem) => {
return (
m &&
m.full &&
GqlService.contains(m.full.toString(), parts.needle.replace(T.LPARENS, T.NOTHING)) &&
GqlService.contains(m.full.toString(), $scope.parts.needle.replace(T.LPARENS, T.NOTHING)) &&
GqlService.clean(m.full.toString())
);
}), 10);
} else if ([T.EQ, T.NE].indexOf(parts.op) !== -1) {
} else if ([T.EQ, T.NE].indexOf($scope.parts.op) !== -1) {
// is_value_string is_unquoted_string
$scope.mode = Mode.Unquoted;

GqlService.ajaxRequest(parts.field).then((d)=> {
GqlService.ajaxRequest($scope.parts.field).then((d)=> {
$scope.ddItems = _.take(_.filter(d, (m) => {
return m && m.full && GqlService.contains(m.full.toString(), parts.needle) && GqlService.clean(m.full.toString());
return m && m.full && GqlService.contains(m.full.toString(), $scope.parts.needle) && GqlService.clean(m.full.toString());
}), 10);
});
}
Expand Down Expand Up @@ -485,7 +463,7 @@ module ngApp.components.gql {

$scope.enter = function(item: IDdItem): void {
item = item || $scope.ddItems[$scope.active];

var needleLength = $scope.parts.needle.length;
// Quote the value if it has a space so the parse can handle it
if (GqlService.isQuoted(item.full)) item.full = T.QUOTE + item.full + T.QUOTE;

Expand All @@ -494,21 +472,22 @@ module ngApp.components.gql {

var left = $scope.left;
var right = $scope.right;

console.log('her?E?RE');
if ([Mode.Field, Mode.Op, Mode.Unquoted].indexOf($scope.mode) !== -1) {
var newLeft = GqlService.lhsRewrite(left, $scope.mode);
var newLeft = GqlService.lhsRewrite(left, needleLength);
var newRight = GqlService.rhsRewrite(right);

$scope.query = newLeft + item.full + newRight;
GqlService.setPos(element[0], (newLeft + item.full).length);
} else if ($scope.mode === Mode.Quoted) {
var newLeft = GqlService.lhsRewriteQuoted(left);
var newLeft = GqlService.lhsRewrite(left, needleLength + 1);
var newRight = GqlService.rhsRewriteQuoted(right);

$scope.query = newLeft + item.full + newRight;
GqlService.setPos(element[0], (newLeft + item.full).length);
} else if ($scope.mode === Mode.List) {
var newLeft = GqlService.lhsRewriteList(left);
if (GqlService.isCountOdd(left, T.QUOTE)) needleLength++;
var newLeft = GqlService.lhsRewrite(left, needleLength);
var newRight = GqlService.rhsRewriteList(right);

$scope.query = newLeft + item.full + newRight;
Expand Down Expand Up @@ -606,11 +585,8 @@ module ngApp.components.gql {
parseQuoted(left: string): IParts;
ajaxQuoted(parts: IParts): ng.IPromise<IDdItem[]>;
ajaxRequest(field: string): ng.IPromise<IDdItem[]>;
lhsRewrite(left: string, mode: Mode): string;
lhsRewrite(left: string, needleLength: number): string;
rhsRewrite(left: string): string;
lhsTokenField(left: string): number;
lhsRewriteQuoted(left: string): string;
lhsRewriteList(left: string): string;
rhsRewriteQuoted(left: string): string;
rhsRewriteList(left: string): string;
isQuoted(s: string | number): boolean;
Expand Down Expand Up @@ -671,6 +647,7 @@ module ngApp.components.gql {

interface IGqlScope extends ng.IScope {
mode: Mode;
parts: IParts;
mapping: IDdItem[];
active: number;
onChange(): void;
Expand Down
36 changes: 4 additions & 32 deletions app/scripts/components/gql/tests/gql.service.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,24 +327,17 @@ describe("GQL Parser", function() {
expect(ret.listValues).to.eql(["one","two","four","five"]);
}));
});
describe("lhsTokenField", function () {
it("get position of token used to split string", inject(function (GqlService) {
expect(GqlService.lhsTokenField("f1 = ")).to.eq(4);
expect(GqlService.lhsTokenField("f1 = va")).to.eq(4);
expect(GqlService.lhsTokenField("f1 = val and f2 = va")).to.eq(17);
}));
});
describe("lhsRewrite", function () {
it("get left hand side query", inject(function (GqlService) {
expect(GqlService.lhsRewrite("f1 = ", 2)).to.eq("f1 = ");
expect(GqlService.lhsRewrite("(f1 = ", 2)).to.eq("(f1 = ");
expect(GqlService.lhsRewrite("f1 = ", 0)).to.eq("f1 = ");
expect(GqlService.lhsRewrite("(f1 = ", 0)).to.eq("(f1 = ");
expect(GqlService.lhsRewrite("f1 = va", 2)).to.eq("f1 = ");
expect(GqlService.lhsRewrite("access = pr", 2)).to.eq("access = ");
expect(GqlService.lhsRewrite("f1 = val and f2 = va", 2)).to.eq("f1 = val and f2 = ");
}));
it("get left hand side query when field", inject(function (GqlService) {
expect(GqlService.lhsRewrite("f1 is b and a", 0)).to.eq("f1 is b and ");
expect(GqlService.lhsRewrite("f2 is b and (a", 0)).to.eq("f2 is b and (");
expect(GqlService.lhsRewrite("f1 is b and a", 1)).to.eq("f1 is b and ");
expect(GqlService.lhsRewrite("f2 is b and (a", 1)).to.eq("f2 is b and (");
expect(GqlService.lhsRewrite("f3 is b and (a", 2)).to.eq("f3 is b and ");
}));
});
Expand All @@ -356,32 +349,11 @@ describe("GQL Parser", function() {
expect(GqlService.rhsRewrite("otected")).to.eq("");
}));
});
describe("lhsRewriteQuoted", function () {
it("get left hand side query when quoted string", inject(function (GqlService) {
expect(GqlService.lhsRewriteQuoted('a is "value val')).to.eq("a is ");
}));
});
describe("rhsRewriteQuoted", function () {
it("get right hand side query when quoted string", inject(function (GqlService) {
expect(GqlService.rhsRewriteQuoted('lue value" and a is b')).to.eq(" and a is b");
}));
});
describe("lhsRewriteList", function () {
it("get left hand side query when list", inject(function (GqlService) {
expect(GqlService.lhsRewriteList('a in [val')).to.eq("a in [");
expect(GqlService.lhsRewriteList('a in [value, val')).to.eq("a in [value,");
expect(GqlService.lhsRewriteList('a in [value, "val1 val')).to.eq("a in [value,");
}));
it("work with not in", inject(function (GqlService) {
expect(GqlService.lhsRewriteList('a not in [val')).to.eq("a not in [");
expect(GqlService.lhsRewriteList('a not in [value, val')).to.eq("a not in [value,");
expect(GqlService.lhsRewriteList('a not in [value, "val1 val')).to.eq("a not in [value,");
}));
it("[OICR-925] add missing opening bracket", inject(function (GqlService) {
expect(GqlService.lhsRewriteList('a in val')).to.eq("a in [");
expect(GqlService.lhsRewriteList('a not in val')).to.eq("a not in [");
}));
});
describe("rhsRewriteList", function () {
it("get right hand side query when list", inject(function (GqlService) {
expect(GqlService.rhsRewriteList('ue, value] and a is b')).to.eq(", value] and a is b");
Expand Down

0 comments on commit 19505be

Please sign in to comment.