Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

concat: cs & php &py #4

Merged
merged 20 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions helpers/c#/helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -912,4 +912,44 @@ public static string Slice(object str2, object idx1, object idx2)
return str[start..end];
}
}

public static object arrayConcat(object a, object b)
{
if (a == null && b == null)
{
return null;
}
else if (a == null)
{
return b;
}
else if (b == null)
{
return a;
}

if (a is IList<object> && b is IList<object>)
{
List<object> result = new List<object>((IList<object>)a);
result.AddRange((IList<object>)b);
return result;
}
else if (a is IList<string> && b is IList<string>)
{
List<string> result = new List<string>((IList<string>)a);
result.AddRange((IList<string>)b);
return result;
}
else if (a is IList<Dictionary<string, object>> && b is IList<Dictionary<string, object>>)
{
List<Dictionary<string, object>> result = new List<Dictionary<string, object>>((IList<Dictionary<string, object>>)a);
result.AddRange((IList<Dictionary<string, object>>)b);
return result;
}
else
{
throw new InvalidOperationException("Unsupported types for concatenation.");
}
}

}
24 changes: 24 additions & 0 deletions helpers/go/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -1231,3 +1231,27 @@ func Remove(dict interface{}, key interface{}) {
// Remove the key from the map
delete(castedDict, keyStr)
}

func ArrayConcat(first interface{}, second interface{}) interface{} {
// Ensure both arguments are slices
firstValue := reflect.ValueOf(first)
secondValue := reflect.ValueOf(second)

if firstValue.Kind() != reflect.Slice {
panic("first argument must be a slice")
}
if secondValue.Kind() != reflect.Slice {
panic("second argument must be a slice")
}

// Check if the slices have the same type
if firstValue.Type() != secondValue.Type() {
panic(fmt.Sprintf("slice types do not match: %s != %s", firstValue.Type(), secondValue.Type()))
}

// Concatenate the two slices using reflect.AppendSlice
result := reflect.AppendSlice(firstValue, secondValue)

// Return the concatenated slice as an interface{}
return result.Interface()
}
6 changes: 6 additions & 0 deletions src/baseTranspiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@

let method = undefined;

let parentClass = (ts as any).getAllSuperTypeNodes(node.parent)[0];

Check warning on line 340 in src/baseTranspiler.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Unexpected any. Specify a different type

while (parentClass !== undefined) {
const parentClassType = global.checker.getTypeAtLocation(parentClass);
Expand All @@ -354,13 +354,13 @@
if (ts.isMethodDeclaration(elem)) {

const name = elem.name.getText().trim();
if ((node as any).name.escapedText === name) {

Check warning on line 357 in src/baseTranspiler.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Unexpected any. Specify a different type
method = elem;
}
}
});

parentClass = (ts as any).getAllSuperTypeNodes(parentClassDecl)[0] ?? undefined;

Check warning on line 363 in src/baseTranspiler.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Unexpected any. Specify a different type
}


Expand All @@ -373,7 +373,7 @@
return this.DEFAULT_IDENTATION.repeat(num);
}

getBlockOpen(identation){

Check warning on line 376 in src/baseTranspiler.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'identation' is defined but never used
return this.SPACE_BEFORE_BLOCK_OPENING + this.BLOCK_OPENING_TOKEN + "\n";
}

Expand Down Expand Up @@ -438,11 +438,11 @@
return this.getIden(identation) + `${left} instanceof ${right}`;
}

getCustomOperatorIfAny(left, right, operator) {

Check warning on line 441 in src/baseTranspiler.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'left' is defined but never used

Check warning on line 441 in src/baseTranspiler.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'right' is defined but never used

Check warning on line 441 in src/baseTranspiler.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'operator' is defined but never used
return undefined;
}

printCustomBinaryExpressionIfAny(node, identation) {

Check warning on line 445 in src/baseTranspiler.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'node' is defined but never used

Check warning on line 445 in src/baseTranspiler.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'identation' is defined but never used
return undefined; // stub to override
}

Expand Down Expand Up @@ -490,7 +490,7 @@
return leftVar +" "+ operator + " " + rightVar.trim();
}

transformPropertyAcessExpressionIfNeeded (node) {

Check warning on line 493 in src/baseTranspiler.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'node' is defined but never used
return undefined;
}

Expand Down Expand Up @@ -1097,6 +1097,10 @@
return undefined; // stub
}

printConcatCall(node, identation, name = undefined, parsedArg = undefined) {
return undefined; // stub
}

printToFixedCall(node, identation, name = undefined, parsedArg = undefined) {
return undefined; // stub
}
Expand Down Expand Up @@ -1238,6 +1242,8 @@
return this.printSplitCall(node, identation, name, parsedArg);
case 'toFixed':
return this.printToFixedCall(node, identation, name, parsedArg);
case 'concat':
return this.printConcatCall(node, identation, name, parsedArg);
case 'search':
return this.printSearchCall(node, identation, name, parsedArg);
case 'endsWith':
Expand Down
4 changes: 4 additions & 0 deletions src/csharpTranspiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,10 @@ export class CSharpTranspiler extends BaseTranspiler {
return `((string)${name}).Split(new [] {((string)${parsedArg})}, StringSplitOptions.None).ToList<object>()`;
}

printConcatCall(node, identation, name = undefined, parsedArg = undefined) {
return `arrayConcat(${name}, ${parsedArg})`;
}

printToFixedCall(node, identation, name = undefined, parsedArg = undefined) {
return `toFixed(${name}, ${parsedArg})`;
}
Expand Down
4 changes: 4 additions & 0 deletions src/goTranspiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,10 @@ ${this.getIden(identation)}}`;
return `ToString(${name})`;
}

printConcatCall(node, identation, name = undefined, parsedArg = undefined) {
return `ArrayConcat(${name}, ${parsedArg})`;
}

printToUpperCaseCall(node, identation, name = undefined) {
return `ToUpper(${name})`;
}
Expand Down
4 changes: 4 additions & 0 deletions src/phpTranspiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,10 @@ export class PhpTranspiler extends BaseTranspiler {
return `explode(${parsedArg}, ${name})`;
}

printConcatCall(node: any, identation: any, name?: any, parsedArg?: any) {
return `array_merge(${name}, ${parsedArg})`;
}

printPadEndCall(node, identation, name, parsedArg, parsedArg2) {
return `str_pad(${name}, ${parsedArg}, ${parsedArg2}, STR_PAD_RIGHT)`;
}
Expand Down
4 changes: 4 additions & 0 deletions src/pythonTranspiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ export class PythonTranspiler extends BaseTranspiler {
return `${name}.split(${parsedArg})`;
}

printConcatCall(node: any, identation: any, name?: any, parsedArg?: any) {
return `${name} + ${parsedArg}`;
}

printPopCall(node: any, identation: any, name?: any) {
return `${name}.pop()`;
}
Expand Down
6 changes: 6 additions & 0 deletions tests/csharpTranspiler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,12 @@ describe('csharp transpiling tests', () => {
const output = transpiler.transpileCSharp(ts).content;
expect(output).toBe(csharp);
});
test('should convert concat', () => {
const ts = "y.concat(z)";
const result = "arrayConcat(y, z);";
const output = transpiler.transpileCSharp(ts).content;
expect(output).toBe(result);
});
test('string literal', () => {
const ts = "const x = \"foo, 'single', \\\"double\\\" \\t \\n \\r \\b \\f \";";
const csharp = "object x = \"foo, 'single', \\\"double\\\" \\t \\n \\r \\b \\f \";";
Expand Down
6 changes: 6 additions & 0 deletions tests/goTranspiler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,10 @@ describe('go transpiling tests', () => {
const output = transpiler.transpileGo(ts).content;
expect(output).toBe(go);
});
test('should convert concat', () => {
const ts = "y.concat(z)";
const result = "ArrayConcat(y, z)";
const output = transpiler.transpileGo(ts).content;
expect(output).toBe(result);
});
});
39 changes: 39 additions & 0 deletions tests/integration/cs/TranspilerHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -915,4 +915,43 @@ public static string Slice(object str2, object idx1, object idx2)
return str[start..end];
}
}

public static object arrayConcat(object a, object b)
{
if (a == null && b == null)
{
return null;
}
else if (a == null)
{
return b;
}
else if (b == null)
{
return a;
}

if (a is IList<object> && b is IList<object>)
{
List<object> result = new List<object>((IList<object>)a);
result.AddRange((IList<object>)b);
return result;
}
else if (a is IList<string> && b is IList<string>)
{
List<string> result = new List<string>((IList<string>)a);
result.AddRange((IList<string>)b);
return result;
}
else if (a is IList<Dictionary<string, object>> && b is IList<Dictionary<string, object>>)
{
List<Dictionary<string, object>> result = new List<Dictionary<string, object>>((IList<Dictionary<string, object>>)a);
result.AddRange((IList<Dictionary<string, object>>)b);
return result;
}
else
{
throw new InvalidOperationException("Unsupported types for concatenation.");
}
}
}
25 changes: 25 additions & 0 deletions tests/integration/go/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -1242,3 +1242,28 @@ func Capitalize(s string) string {
// Combine the uppercase first letter with the rest of the string
return firstLetter + s[1:]
}

// ArrayConcat concatenates two arrays in place.
func ArrayConcat(first interface{}, second interface{}) interface{} {
// Ensure both arguments are slices
firstValue := reflect.ValueOf(first)
secondValue := reflect.ValueOf(second)

if firstValue.Kind() != reflect.Slice {
panic("first argument must be a slice")
}
if secondValue.Kind() != reflect.Slice {
panic("second argument must be a slice")
}

// Check if the slices have the same type
if firstValue.Type() != secondValue.Type() {
panic(fmt.Sprintf("slice types do not match: %s != %s", firstValue.Type(), secondValue.Type()))
}

// Concatenate the two slices using reflect.AppendSlice
result := reflect.AppendSlice(firstValue, secondValue)

// Return the concatenated slice as an interface{}
return result.Interface()
}
5 changes: 5 additions & 0 deletions tests/integration/source/transpilable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ class Test {
console.log(dictKeys.length); // should print 1
console.log(dictKeys[0]); // should print "b"

const firstConcat = ["a", "b"];
const secondConcat = ["c", "d"];
const both = firstConcat.concat(secondConcat);
console.log(both.length); // should print 4
console.log(both[2]); // should print "c"
}
}

Expand Down
6 changes: 6 additions & 0 deletions tests/phpTranspiler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,12 @@ describe('php transpiling tests', () => {
const output = transpiler.transpilePhp(ts).content;
expect(output).toBe(php);
});
test('should convert concat', () => {
const ts = "y.concat(z)";
const result = "array_merge($y, $z);";
const output = transpiler.transpilePhp(ts).content;
expect(output).toBe(result);
});
test('string literal', () => {
const ts = "const x = \"foo, 'single', \\\"double\\\" \\t \\n \\r \\b \\f \";";
const php = "$x = 'foo, \\\'single\\\', \"double\" \\t \\n \\r \\b \\f ';";
Expand Down
6 changes: 6 additions & 0 deletions tests/pythonTranspiler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,12 @@ describe('python tests', () => {
const output = transpiler.transpilePython(ts).content;
expect(output).toBe(python);
});
test('should convert concat', () => {
const ts = "y.concat(z)";
const result = "y + z";
const output = transpiler.transpilePython(ts).content;
expect(output).toBe(result);
});
test('should convert search', () => {
const ts = '"abcdxtzyw".search("xt");';
const python = "'abcdxtzyw'.find('xt')";
Expand Down
Loading