Skip to content

Commit

Permalink
indent items in csharp arrays (#117)
Browse files Browse the repository at this point in the history
* Add args template for arrays

* indent items in csharp arrays

* move .join logic for arrays to args template
  • Loading branch information
lrlna authored and mcasimir committed Jun 25, 2021
1 parent 1b18430 commit 92e2607
Show file tree
Hide file tree
Showing 17 changed files with 43 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,10 @@ class Visitor extends ECMAScriptVisitor {
return arg !== ',';
});
if (ctx.type.argsTemplate) { // NOTE: not currently being used anywhere.
args = ctx.type.argsTemplate(visitedElements, ctx.indentDepth);
args = visitedElements.map((arg, index) => {
const last = !visitedElements[index + 1];
return ctx.type.argsTemplate(arg, ctx.indentDepth, last);
}).join('');
} else {
args = visitedElements.join(', ');
}
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/bson-transpilers/lib/symbol-table/shelltojava.js

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions packages/bson-transpilers/symbols/basic_types.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ BasicTypes:
<<: *__type
id: "_array"
template: *ArrayTypeTemplate
argsTemplate: *ArrayTypeArgsTemplate
_object: &ObjectType
<<: *__type
id: "_object"
Expand Down
9 changes: 8 additions & 1 deletion packages/bson-transpilers/symbols/csharp/templates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,14 @@ Templates:
return 'new BsonArray()'
}

return `new BsonArray${initialIndent}{${indent}${literal}${initialIndent}}`;
return `new BsonArray${initialIndent}{${literal}${initialIndent}}`;
}
ArrayTypeArgsTemplate: &ArrayTypeArgsTemplate !!js/function >
(arg, depth, last) => {
const indent = '\n' + ' '.repeat(depth);
const arr = arg.split(', new').join(`, ${indent}new`)

return last ? `${indent}${arr}` : `${indent}${arr},`;
}
NullTypeTemplate: &NullTypeTemplate !!js/function >
() => {
Expand Down
1 change: 1 addition & 0 deletions packages/bson-transpilers/symbols/java/templates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ Templates:

return `Arrays.asList(${arr})`;
}
ArrayTypeArgsTemplate: &ArrayTypeArgsTemplate null
NullTypeTemplate: &NullTypeTemplate null
UndefinedTypeTemplate: &UndefinedTypeTemplate !!js/function >
() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Templates:

return `[${indent}${literal}${closingIndent}]`;
}
ArrayTypeArgsTemplate: &ArrayTypeArgsTemplate null
NullTypeTemplate: &NullTypeTemplate !!js/function >
() => {
return 'null';
Expand Down
1 change: 1 addition & 0 deletions packages/bson-transpilers/symbols/python/templates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ Templates:

return `[${indent}${literal}${closingIndent}]`;
}
ArrayTypeArgsTemplate: &ArrayTypeArgsTemplate null
NullTypeTemplate: &NullTypeTemplate !!js/function >
() => {
return 'None';
Expand Down
1 change: 1 addition & 0 deletions packages/bson-transpilers/symbols/shell/templates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Templates:

return `[${indent}${literal}${closingIndent}]`;
}
ArrayTypeArgsTemplate: &ArrayTypeArgsTemplate null
NullTypeTemplate: &NullTypeTemplate !!js/function >
() => {
return 'null';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@
"javascript": "{x: ['1', '2']}",
"python": "{\n 'x': [\n '1', '2'\n ]\n}",
"java": "new Document(\"x\", Arrays.asList(\"1\", \"2\"))",
"csharp": "new BsonDocument(\"x\", new BsonArray\n {\n \"1\", \"2\"\n })",
"csharp": "new BsonDocument(\"x\", new BsonArray\n {\n \"1\",\n \"2\"\n })",
"shell": "{\n x: [\n '1', '2'\n ]\n}"
},
{
Expand Down Expand Up @@ -338,7 +338,7 @@
"javascript": "{ status: 'A', $or: [{ qty: { $lt: 30 } }, { item: { $regex: '^p' } }] }",
"python": "{\n 'status': 'A', \n '$or': [\n {\n 'qty': {\n '$lt': 30\n }\n }, {\n 'item': {\n '$regex': '^p'\n }\n }\n ]\n}",
"java": "new Document(\"status\", \"A\")\n .append(\"$or\", Arrays.asList(new Document(\"qty\", \n new Document(\"$lt\", 30L)), \n new Document(\"item\", \n new Document(\"$regex\", \"^p\"))))",
"csharp": "new BsonDocument\n{\n { \"status\", \"A\" }, \n { \"$or\", new BsonArray\n {\n new BsonDocument(\"qty\", new BsonDocument(\"$lt\", 30)), new BsonDocument(\"item\", new BsonDocument(\"$regex\", \"^p\"))\n } }\n}",
"csharp": "new BsonDocument\n{\n { \"status\", \"A\" }, \n { \"$or\", new BsonArray\n {\n new BsonDocument(\"qty\", \n new BsonDocument(\"$lt\", 30)),\n new BsonDocument(\"item\", \n new BsonDocument(\"$regex\", \"^p\"))\n } }\n}",
"shell": "{\n status: 'A', \n $or: [\n {\n qty: {\n $lt: 30\n }\n }, {\n item: {\n $regex: '^p'\n }\n }\n ]\n}"
}

Expand All @@ -349,39 +349,39 @@
"javascript": "['1', '2']",
"python": "[\n '1', '2'\n]",
"java": "Arrays.asList(\"1\", \"2\")",
"csharp": "new BsonArray\n{\n \"1\", \"2\"\n}",
"csharp": "new BsonArray\n{\n \"1\",\n \"2\"\n}",
"shell": "[\n '1', '2'\n]"
},
{
"description": "array with trailing comma",
"javascript": "['1', '2',]",
"python": "[\n '1', '2'\n]",
"java": "Arrays.asList(\"1\", \"2\")",
"csharp": "new BsonArray\n{\n \"1\", \"2\"\n}",
"csharp": "new BsonArray\n{\n \"1\",\n \"2\"\n}",
"shell": "[\n '1', '2'\n]"
},
{
"description": "Array with subdoc",
"javascript": "['1', { settings: 'http2' }]",
"python": "[\n '1', {\n 'settings': 'http2'\n }\n]",
"java": "Arrays.asList(\"1\", \n new Document(\"settings\", \"http2\"))",
"csharp": "new BsonArray\n{\n \"1\", new BsonDocument(\"settings\", \"http2\")\n}",
"csharp": "new BsonArray\n{\n \"1\",\n new BsonDocument(\"settings\", \"http2\")\n}",
"shell": "[\n '1', {\n settings: 'http2'\n }\n]"
},
{
"description": "nested array with nested subdoc",
"javascript": "{\"pipeline\": [ { $match: { $expr: { \"$eq\": [ \"$manager\", \"$$me\" ] } } }, { $project: { managers : 0 } }, { $sort: { startQuarter: 1, notes:1, job_code: 1 } } ]}" ,
"python": "{\n 'pipeline': [\n {\n '$match': {\n '$expr': {\n '$eq': [\n '$manager', '$$me'\n ]\n }\n }\n }, {\n '$project': {\n 'managers': 0\n }\n }, {\n '$sort': {\n 'startQuarter': 1, \n 'notes': 1, \n 'job_code': 1\n }\n }\n ]\n}",
"java": "new Document(\"pipeline\", Arrays.asList(new Document(\"$match\", \n new Document(\"$expr\", \n new Document(\"$eq\", Arrays.asList(\"$manager\", \"$$me\")))), \n new Document(\"$project\", \n new Document(\"managers\", 0L)), \n new Document(\"$sort\", \n new Document(\"startQuarter\", 1L)\n .append(\"notes\", 1L)\n .append(\"job_code\", 1L))))",
"csharp": "new BsonDocument(\"pipeline\", new BsonArray\n {\n new BsonDocument(\"$match\", new BsonDocument(\"$expr\", new BsonDocument(\"$eq\", new BsonArray\n {\n \"$manager\", \"$$me\"\n }))), new BsonDocument(\"$project\", new BsonDocument(\"managers\", 0)), new BsonDocument(\"$sort\", new BsonDocument\n {\n { \"startQuarter\", 1 }, \n { \"notes\", 1 }, \n { \"job_code\", 1 }\n })\n })",
"csharp": "new BsonDocument(\"pipeline\", new BsonArray\n {\n new BsonDocument(\"$match\", \n new BsonDocument(\"$expr\", \n new BsonDocument(\"$eq\", \n new BsonArray\n {\n \"$manager\",\n \"$$me\"\n }))),\n new BsonDocument(\"$project\", \n new BsonDocument(\"managers\", 0)),\n new BsonDocument(\"$sort\", \n new BsonDocument\n {\n { \"startQuarter\", 1 }, \n { \"notes\", 1 }, \n { \"job_code\", 1 }\n })\n })",
"shell": "{\n \"pipeline\": [\n {\n $match: {\n $expr: {\n \"$eq\": [\n \"$manager\", \"$$me\"\n ]\n }\n }\n }, {\n $project: {\n managers: 0\n }\n }, {\n $sort: {\n startQuarter: 1, \n notes: 1, \n job_code: 1\n }\n }\n ]\n}"
},
{
"description": "Array with subarray",
"javascript": "['1', ['2', '3']]",
"python": "[\n '1', [\n '2', '3'\n ]\n]",
"java": "Arrays.asList(\"1\", Arrays.asList(\"2\", \"3\"))",
"csharp": "new BsonArray\n{\n \"1\", new BsonArray\n {\n \"2\", \"3\"\n }\n}",
"csharp": "new BsonArray\n{\n \"1\",\n new BsonArray\n {\n \"2\",\n \"3\"\n }\n}",
"shell": "[\n '1', [\n '2', '3'\n ]\n]"
},
{
Expand All @@ -399,7 +399,7 @@
"javascript": "[,'1', '2',]",
"python": "[\n None, '1', '2'\n]",
"java": "Arrays.asList(new BsonUndefined(), \"1\", \"2\")",
"csharp": "new BsonArray\n{\n BsonUndefined.Value, \"1\", \"2\"\n}",
"csharp": "new BsonArray\n{\n BsonUndefined.Value,\n \"1\",\n \"2\"\n}",
"shell": "[\n undefined, '1', '2'\n]"
},
{
Expand All @@ -415,15 +415,15 @@
"javascript": "[,,]",
"python": "[\n None, None\n]",
"java": "Arrays.asList(new BsonUndefined(), \n new BsonUndefined())",
"csharp": "new BsonArray\n{\n BsonUndefined.Value, BsonUndefined.Value\n}",
"csharp": "new BsonArray\n{\n BsonUndefined.Value,\n BsonUndefined.Value\n}",
"shell": "[\n undefined, undefined\n]"
},
{
"description": "Array with elision in the middle",
"javascript": "['1',,,,'2']",
"python": "[\n '1', None, None, None, '2'\n]",
"java": "Arrays.asList(\"1\", \n new BsonUndefined(), \n new BsonUndefined(), \n new BsonUndefined(), \"2\")",
"csharp": "new BsonArray\n{\n \"1\", BsonUndefined.Value, BsonUndefined.Value, BsonUndefined.Value, \"2\"\n}",
"csharp": "new BsonArray\n{\n \"1\",\n BsonUndefined.Value,\n BsonUndefined.Value,\n BsonUndefined.Value,\n \"2\"\n}",
"shell": "[\n '1', undefined, undefined, undefined, '2'\n]"
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@
"javascript": "{\n x: [\n 1, 2\n ]\n}",
"python": "{\n 'x': [\n 1, 2\n ]\n}",
"java": "new Document(\"x\", Arrays.asList(1L, 2L))",
"csharp": "new BsonDocument(\"x\", new BsonArray\n {\n 1, 2\n })",
"csharp": "new BsonDocument(\"x\", new BsonArray\n {\n 1,\n 2\n })",
"shell": "{x: [1,2]}"
},
{
Expand Down Expand Up @@ -298,31 +298,31 @@
"javascript": "[\n 1, 2\n]",
"python": "[\n 1, 2\n]",
"java": "Arrays.asList(1L, 2L)",
"csharp": "new BsonArray\n{\n 1, 2\n}",
"csharp": "new BsonArray\n{\n 1,\n 2\n}",
"shell": "[1, 2]"
},
{
"description": "array with trailing comma",
"javascript": "[\n 1, 2\n]",
"python": "[\n 1, 2\n]",
"java": "Arrays.asList(1L, 2L)",
"csharp": "new BsonArray\n{\n 1, 2\n}",
"csharp": "new BsonArray\n{\n 1,\n 2\n}",
"shell": "[1, 2,]"
},
{
"description": "Array with subdoc",
"javascript": "[\n 1, {\n settings: 'http2'\n }\n]",
"python": "[\n 1, {\n 'settings': 'http2'\n }\n]",
"java": "Arrays.asList(1L, \n new Document(\"settings\", \"http2\"))",
"csharp": "new BsonArray\n{\n 1, new BsonDocument(\"settings\", \"http2\")\n}",
"csharp": "new BsonArray\n{\n 1,\n new BsonDocument(\"settings\", \"http2\")\n}",
"shell": "[1, { settings: 'http2' }]"
},
{
"description": "Array with subarray",
"javascript": "[\n 1, [\n 2, 3\n ]\n]",
"python": "[\n 1, [\n 2, 3\n ]\n]",
"java": "Arrays.asList(1L, Arrays.asList(2L, 3L))",
"csharp": "new BsonArray\n{\n 1, new BsonArray\n {\n 2, 3\n }\n}",
"csharp": "new BsonArray\n{\n 1,\n new BsonArray\n {\n 2,\n 3\n }\n}",
"shell": "[1, [2, 3]]"
},
{
Expand All @@ -340,7 +340,7 @@
"javascript": "[\n undefined, 1, 2\n]",
"python": "[\n None, 1, 2\n]",
"java": "Arrays.asList(new BsonUndefined(), 1L, 2L)",
"csharp": "new BsonArray\n{\n BsonUndefined.Value, 1, 2\n}",
"csharp": "new BsonArray\n{\n BsonUndefined.Value,\n 1,\n 2\n}",
"shell": "[,1, 2,]"
},
{
Expand All @@ -356,15 +356,15 @@
"javascript": "[\n undefined, undefined\n]",
"python": "[\n None, None\n]",
"java": "Arrays.asList(new BsonUndefined(), \n new BsonUndefined())",
"csharp": "new BsonArray\n{\n BsonUndefined.Value, BsonUndefined.Value\n}",
"csharp": "new BsonArray\n{\n BsonUndefined.Value,\n BsonUndefined.Value\n}",
"shell": "[,,]"
},
{
"description": "Array with elision in the middle",
"javascript": "[\n 1, undefined, undefined, undefined, 2\n]",
"python": "[\n 1, None, None, None, 2\n]",
"java": "Arrays.asList(1L, \n new BsonUndefined(), \n new BsonUndefined(), \n new BsonUndefined(), 2L)",
"csharp": "new BsonArray\n{\n 1, BsonUndefined.Value, BsonUndefined.Value, BsonUndefined.Value, 2\n}",
"csharp": "new BsonArray\n{\n 1,\n BsonUndefined.Value,\n BsonUndefined.Value,\n BsonUndefined.Value,\n 2\n}",
"shell": "[1,,,,2]"
}
]
Expand Down

0 comments on commit 92e2607

Please sign in to comment.