-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #346 from ballerina-platform/csverror
Fix csv parsing logic
- Loading branch information
Showing
11 changed files
with
108 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,3 +21,9 @@ path = "../native/build/libs/[email protected]@.jar" | |
groupId = "io.ballerinax" | ||
artifactId = "salesforce" | ||
version = "@project.version@" | ||
|
||
[[platform.java17.dependency]] | ||
groupId = "com.opencsv" | ||
artifactId = "opencsv" | ||
version = "@opencsv.version@" | ||
path = "./lib/[email protected]@.jar" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
native/src/main/java/io/ballerinax/salesforce/CsvParserUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved. | ||
* | ||
* WSO2 LLC. licenses this file to you under the Apache License, | ||
* Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package io.ballerinax.salesforce; | ||
|
||
import com.opencsv.CSVReader; | ||
import com.opencsv.exceptions.CsvException; | ||
import io.ballerina.runtime.api.creators.ErrorCreator; | ||
import io.ballerina.runtime.api.creators.TypeCreator; | ||
import io.ballerina.runtime.api.creators.ValueCreator; | ||
import io.ballerina.runtime.api.types.ArrayType; | ||
import io.ballerina.runtime.api.utils.StringUtils; | ||
import io.ballerina.runtime.api.values.BArray; | ||
import io.ballerina.runtime.api.values.BString; | ||
|
||
import java.io.IOException; | ||
import java.io.StringReader; | ||
import java.util.List; | ||
|
||
/** | ||
* This class holds the utility methods involved with parsing csv content. | ||
* | ||
* @since 8.0.1 | ||
*/ | ||
public class CsvParserUtils { | ||
public static Object parseCsvToStringArray(BString csvData) { | ||
try (CSVReader reader = new CSVReader(new StringReader(csvData.getValue()))) { | ||
List<String[]> records = reader.readAll(); | ||
|
||
// Convert each row in records to BArray | ||
BArray[] bArrayData = new BArray[records.size()]; | ||
for (int i = 0; i < records.size(); i++) { | ||
String[] row = records.get(i); | ||
BArray bArrayRow = StringUtils.fromStringArray(row); | ||
bArrayData[i] = bArrayRow; | ||
} | ||
BArray emptyBArray = StringUtils.fromStringArray(new String[0]); | ||
ArrayType stringArrayType = TypeCreator.createArrayType(emptyBArray.getType()); | ||
return ValueCreator.createArrayValue(bArrayData, stringArrayType); // string[][] | ||
} catch (IOException | CsvException e) { | ||
return ErrorCreator.createError(StringUtils.fromString(e.getMessage())); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters