From f4be7ce2eb0f289b97300f7b9498906c720836e7 Mon Sep 17 00:00:00 2001 From: Sahan Hemachandra <45299562+sahanHe@users.noreply.github.com> Date: Tue, 5 Sep 2023 11:18:50 +0530 Subject: [PATCH 1/5] [Automated] Update native jar versions in toml files --- ballerina/Dependencies.toml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ballerina/Dependencies.toml b/ballerina/Dependencies.toml index bbf543b..9907a17 100644 --- a/ballerina/Dependencies.toml +++ b/ballerina/Dependencies.toml @@ -98,6 +98,9 @@ dependencies = [ {org = "ballerina", name = "jballerina.java"}, {org = "ballerina", name = "lang.value"} ] +modules = [ + {org = "ballerina", packageName = "io", moduleName = "io"} +] [[package]] org = "ballerina" @@ -362,6 +365,7 @@ name = "persist.googlesheets" version = "1.2.0" dependencies = [ {org = "ballerina", name = "http"}, + {org = "ballerina", name = "io"}, {org = "ballerina", name = "jballerina.java"}, {org = "ballerina", name = "lang.regexp"}, {org = "ballerina", name = "lang.runtime"}, From 82fc44f92fbc157c75d2cc35900b10719c8dea3e Mon Sep 17 00:00:00 2001 From: Sahan Hemachandra <45299562+sahanHe@users.noreply.github.com> Date: Tue, 5 Sep 2023 11:28:42 +0530 Subject: [PATCH 2/5] [Automated] Update native jar versions in toml files --- ballerina/Dependencies.toml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ballerina/Dependencies.toml b/ballerina/Dependencies.toml index 9907a17..b41a453 100644 --- a/ballerina/Dependencies.toml +++ b/ballerina/Dependencies.toml @@ -98,9 +98,6 @@ dependencies = [ {org = "ballerina", name = "jballerina.java"}, {org = "ballerina", name = "lang.value"} ] -modules = [ - {org = "ballerina", packageName = "io", moduleName = "io"} -] [[package]] org = "ballerina" @@ -223,6 +220,9 @@ dependencies = [ {org = "ballerina", name = "lang.value"}, {org = "ballerina", name = "observe"} ] +modules = [ + {org = "ballerina", packageName = "log", moduleName = "log"} +] [[package]] org = "ballerina" @@ -365,10 +365,10 @@ name = "persist.googlesheets" version = "1.2.0" dependencies = [ {org = "ballerina", name = "http"}, - {org = "ballerina", name = "io"}, {org = "ballerina", name = "jballerina.java"}, {org = "ballerina", name = "lang.regexp"}, {org = "ballerina", name = "lang.runtime"}, + {org = "ballerina", name = "log"}, {org = "ballerina", name = "os"}, {org = "ballerina", name = "persist"}, {org = "ballerina", name = "test"}, From 9c76f024355062d30a7b23e7e294aaa5ebd0c266 Mon Sep 17 00:00:00 2001 From: Sahan Hemachandra <45299562+sahanHe@users.noreply.github.com> Date: Tue, 5 Sep 2023 16:29:04 +0530 Subject: [PATCH 3/5] [Automated] Update native jar versions in toml files --- ballerina/Dependencies.toml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/ballerina/Dependencies.toml b/ballerina/Dependencies.toml index b41a453..bbf543b 100644 --- a/ballerina/Dependencies.toml +++ b/ballerina/Dependencies.toml @@ -220,9 +220,6 @@ dependencies = [ {org = "ballerina", name = "lang.value"}, {org = "ballerina", name = "observe"} ] -modules = [ - {org = "ballerina", packageName = "log", moduleName = "log"} -] [[package]] org = "ballerina" @@ -368,7 +365,6 @@ dependencies = [ {org = "ballerina", name = "jballerina.java"}, {org = "ballerina", name = "lang.regexp"}, {org = "ballerina", name = "lang.runtime"}, - {org = "ballerina", name = "log"}, {org = "ballerina", name = "os"}, {org = "ballerina", name = "persist"}, {org = "ballerina", name = "test"}, From a9f1042cde55fbbb0fc2046dbf56d81adcf5ccd2 Mon Sep 17 00:00:00 2001 From: Sahan Hemachandra <45299562+sahanHe@users.noreply.github.com> Date: Tue, 5 Sep 2023 16:32:25 +0530 Subject: [PATCH 4/5] Update code to fix type cast errors --- ballerina/googlesheets_client.bal | 47 ++++++++++++++++--------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/ballerina/googlesheets_client.bal b/ballerina/googlesheets_client.bal index 7235b52..5ee1a44 100644 --- a/ballerina/googlesheets_client.bal +++ b/ballerina/googlesheets_client.bal @@ -13,12 +13,11 @@ // KIND, either express or implied. See the License for the // specific language governing permissions and limitations // under the License. - -import ballerina/persist; -import ballerina/url; import ballerina/http; -import ballerina/time; import ballerina/lang.regexp; +import ballerina/persist; +import ballerina/time; +import ballerina/url; import ballerinax/googleapis.sheets; type Table record { @@ -160,18 +159,19 @@ public isolated client class GoogleSheetsClient { # + fields - The fields to be retrieved # + include - The associations to be retrieved # + return - A stream of records in the `rowType` type or a `persist:Error` if the operation fails - public isolated function readTableAsStream(typedesc rowType, map typeMap, string[] fields = [], string[] include = []) returns stream|persist:Error { + public isolated function readTableAsStream(typedesc rowType, map typeMap, string[] fields = [], string[] include = []) returns stream|error { string[][] values = []; record {}[] rowTable = []; string|error encodedRange = url:encode(self.range, "UTF-8"); if encodedRange is error { - return error(encodedRange.message()); + return error persist:Error(encodedRange.message()); } string getSheetValuesPath = string `/${self.tableName}!${encodedRange}?dateTimeRenderOption=FORMATTED_STRING&majorDimension=ROWS&valueRenderOption=FORMATTED_VALUE`; json|error response = self.sendRequest(self.httpSheetsClient, getSheetValuesPath); + if response is error { - return response; - } + return error persist:Error(response.message()); + } if response.values !is error { values = self.convertToArray(response); } @@ -182,7 +182,7 @@ public isolated client class GoogleSheetsClient { } string[] columnNames = values[0]; if columnNames.length() != self.dataTypes.length() { - return error("Error: the spreadsheet is not initialised correctly. Number of columns in the sheet does not match with the entity. "); + return error persist:Error("Error: the spreadsheet is not initialised correctly. Number of columns in the sheet does not match with the entity. "); } foreach string[] rowValue in values.slice(1) { record {} rowArray = {}; @@ -190,7 +190,7 @@ public isolated client class GoogleSheetsClient { foreach string columnName in columnNames { string columnNameProcessed = re ` `.replaceAll(re `"`.replaceAll(columnName, ""), ""); if !self.dataTypes.hasKey(columnName) { - return error(string `Error: the spreadsheet is not initialised correctly. Column ${columnName} is not defined in the data types.`); + return error persist:Error(string `Error: the spreadsheet is not initialised correctly. Column ${columnName} is not defined in the data types.`); } string dataType = self.dataTypes.get(columnNameProcessed).toString(); string value = re `"`.replaceAll(rowValue[columnIndex], ""); @@ -210,7 +210,7 @@ public isolated client class GoogleSheetsClient { } else { SheetFieldType|error typedValue = self.dataConverter(value, dataType); if typedValue is error { - return error(typedValue.message()); + return error persist:Error(typedValue.message()); } rowArray[columnName] = typedValue; } @@ -443,25 +443,26 @@ public isolated client class GoogleSheetsClient { } private isolated function civilToString(time:Civil civil) returns string|error { - string civilString = string `${civil.year}-${(civil.month.abs() > 9? civil.month: string `0${civil.month}`)}-${(civil.day.abs() > 9? civil.day: string `0${civil.day}`)}`; - civilString += string `T${(civil.hour.abs() > 9? civil.hour: string `0${civil.hour}`)}:${(civil.minute.abs() > 9? civil.minute: string `0${civil.minute}`)}`; + string civilString = string `${civil.year}-${(civil.month.abs() > 9 ? civil.month : string `0${civil.month}`)}-${(civil.day.abs() > 9 ? civil.day : string `0${civil.day}`)}`; + civilString += string `T${(civil.hour.abs() > 9 ? civil.hour : string `0${civil.hour}`)}:${(civil.minute.abs() > 9 ? civil.minute : string `0${civil.minute}`)}`; if civil.second !is () { time:Seconds seconds = civil.second; - civilString += string `:${(seconds.abs() > (check decimal:fromString("9"))? seconds: string `0${seconds}`)}`; + civilString += string `:${(seconds.abs() > (check decimal:fromString("9")) ? seconds : string `0${seconds}`)}`; } if civil.utcOffset !is () { time:ZoneOffset zoneOffset = civil.utcOffset; - civilString += (zoneOffset.hours >= 0? "+" : "-"); - civilString += string `${zoneOffset.hours.abs() > 9? zoneOffset.hours.abs() : string `0${zoneOffset.hours.abs()}`}`; - civilString += string `:${(zoneOffset.minutes.abs() > 9? zoneOffset.minutes.abs(): string `0${zoneOffset.minutes.abs()}`)}`; + civilString += (zoneOffset.hours >= 0 ? "+" : "-"); + civilString += string `${zoneOffset.hours.abs() > 9 ? zoneOffset.hours.abs() : string `0${zoneOffset.hours.abs()}`}`; + civilString += string `:${(zoneOffset.minutes.abs() > 9 ? zoneOffset.minutes.abs() : string `0${zoneOffset.minutes.abs()}`)}`; time:Seconds? seconds = zoneOffset.seconds; if seconds !is () { - civilString += string `:${(seconds.abs() > 9d? seconds: string `0${seconds.abs()}`)}`; + civilString += string `:${(seconds.abs() > 9d ? seconds : string `0${seconds.abs()}`)}`; } else { civilString += string `:00`; } - } if civil.timeAbbrev !is () { + } + if civil.timeAbbrev !is () { civilString += string `(${civil.timeAbbrev})`; } return civilString; @@ -474,7 +475,7 @@ public isolated client class GoogleSheetsClient { string? timeAbbrev = (); regexp:Span? find = re `\(.*\)`.find(civilString.trim(), 0); if find !is () { - timeAbbrev = civilString.trim().substring(find.startIndex+1, find.endIndex-1); + timeAbbrev = civilString.trim().substring(find.startIndex + 1, find.endIndex - 1); } string[] civilArray = re `T`.split(re `\(.*\)`.replace(civilString.trim(), "")); civilDateString = civilArray[0]; @@ -542,11 +543,11 @@ public isolated client class GoogleSheetsClient { } } - private isolated function sendRequest(http:Client httpClient, string path) returns json | error { + private isolated function sendRequest(http:Client httpClient, string path) returns json|error { http:Response|error httpResponse = httpClient->get(path); if httpResponse is http:Response { int statusCode = httpResponse.statusCode; - json | error jsonResponse = httpResponse.getJsonPayload(); + json|error jsonResponse = httpResponse.getJsonPayload(); if jsonResponse is json { error? validateStatusCodeRes = self.validateStatusCode(jsonResponse, statusCode); if (validateStatusCodeRes is error) { @@ -565,7 +566,7 @@ public isolated client class GoogleSheetsClient { if statusCode != http:STATUS_OK { return self.getSpreadsheetError(response); } - } + } isolated function getSpreadsheetError(json|error errorResponse) returns error { if errorResponse is json { return error(errorResponse.toString()); From d8d422ba2e05990ac690052789a1180f00f96802 Mon Sep 17 00:00:00 2001 From: Sahan Hemachandra <45299562+sahanHe@users.noreply.github.com> Date: Wed, 6 Sep 2023 09:41:10 +0530 Subject: [PATCH 5/5] Update returned errors in the code --- ballerina/googlesheets_client.bal | 46 +++++++++++++++---------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/ballerina/googlesheets_client.bal b/ballerina/googlesheets_client.bal index 5ee1a44..471ea6e 100644 --- a/ballerina/googlesheets_client.bal +++ b/ballerina/googlesheets_client.bal @@ -83,7 +83,7 @@ public isolated client class GoogleSheetsClient { sheets:DeveloperMetadataLookupFilter filter = {locationType: "ROW", metadataKey: self.tableName, metadataValue: metadataValue}; sheets:ValueRange[]|error output = self.googleSheetClient->getRowByDataFilter(self.spreadsheetId, self.sheetId, filter); if output is error { - return error(output.message()); + return error persist:Error(output.message()); } if output.length() > 0 { return persist:getAlreadyExistsError(self.entityName, self.generateKeyRecord(self.keyFields, rowValues)); @@ -94,17 +94,17 @@ public isolated client class GoogleSheetsClient { if dataType == "time:Date" || dataType == "time:TimeOfDay" || dataType == "time:Civil" || dataType == "time:Utc" { SheetTimeType|error timeValue = rowValues.get(key).ensureType(); if timeValue is error { - return error(timeValue.message()); + return error persist:Error(timeValue.message()); } string|error value = self.timeToString(timeValue); if value is error { - return error(value.message()); + return error persist:Error(value.message()); } values.push(value); } else { SheetBasicType|error value = rowValues.get(key).ensureType(); if value is error { - return error(value.message()); + return error persist:Error(value.message()); } values.push(value); } @@ -113,11 +113,11 @@ public isolated client class GoogleSheetsClient { sheets:A1Range a1Range = {sheetName: self.tableName, startIndex: splitedRange[0], endIndex: splitedRange[1]}; sheets:ValueRange|error insertedRow = self.googleSheetClient->appendValue(self.spreadsheetId, values, a1Range, "USER_ENTERED"); if insertedRow is error { - return error(insertedRow.message()); + return error persist:Error(insertedRow.message()); } error? response = self.googleSheetClient->setRowMetaData(self.spreadsheetId, self.sheetId, insertedRow.rowPosition, "DOCUMENT", self.tableName, metadataValue); if response is error { - return error(response.message()); + return error persist:Error(response.message()); } } } @@ -140,7 +140,7 @@ public isolated client class GoogleSheetsClient { do { return check 'object.cloneWithType(rowType); } on fail error e { - return e; + return error persist:Error(e.message()); } } @@ -176,7 +176,7 @@ public isolated client class GoogleSheetsClient { values = self.convertToArray(response); } if values.length() == 0 { - return error("Error: the spreadsheet is not initialised correctly. Spreadsheet is empty."); + return error persist:Error("Error: the spreadsheet is not initialised correctly. Spreadsheet is empty."); } else if values.length() == 1 { return rowTable.toStream(); } @@ -197,7 +197,7 @@ public isolated client class GoogleSheetsClient { if dataType == "time:Date" || dataType == "time:TimeOfDay" || dataType == "time:Civil" || dataType == "time:Utc" { SheetFieldType|error typedValue = self.dataConverter(value, dataType); if typedValue is error { - return error(typedValue.message()); + return error persist:Error(typedValue.message()); } else if typedValue is time:Civil { rowArray[columnName] = typedValue; } else if typedValue is time:Date { @@ -241,20 +241,20 @@ public isolated client class GoogleSheetsClient { sheets:DeveloperMetadataLookupFilter filter = {locationType: "ROW", metadataKey: self.tableName, metadataValue: metadataValue}; sheets:ValueRange[]|error rows = self.googleSheetClient->getRowByDataFilter(self.spreadsheetId, self.sheetId, filter); if rows is error { - return error(rows.message()); + return error persist:Error(rows.message()); } if rows.length() == 0 { return persist:getNotFoundError(self.entityName, key); } else if rows.length() > 1 { - return error(string `Multiple elements found for given key: ${key.toString()}`); + return error persist:Error(string `Multiple elements found for given key: ${key.toString()}`); } foreach string entityKey in entityKeys { if !updateRecord.hasKey(entityKey) && self.keyFields.indexOf(entityKey) != () { if key is map { (int|string|decimal)|error value = key.get(entityKey).ensureType(); if value is error { - return error(value.message()); + return error persist:Error(value.message()); } values.push(value); } else if (key is int|string|decimal|float) { @@ -266,7 +266,7 @@ public isolated client class GoogleSheetsClient { if dataType == "boolean" || dataType == "int" || dataType == "float" || dataType == "decimal" { SheetNumericType|error value = self.valuesFromString(rows[0].values[indexOfKey].toString(), dataType); if value is error { - return error(value.message()); + return error persist:Error(value.message()); } values.push(value); } else { @@ -279,18 +279,18 @@ public isolated client class GoogleSheetsClient { if dataType == "time:Date" || dataType == "time:TimeOfDay" || dataType == "time:Civil" || dataType == "time:Utc" { SheetTimeType|error timeValue = updateRecord.get(entityKey).ensureType(); if timeValue is error { - return error(timeValue.message()); + return error persist:Error(timeValue.message()); } value = self.timeToString(timeValue); if value is error { - return error(value.message()); + return error persist:Error(value.message()); } values.push(value); } else { value = updateRecord.get(entityKey).ensureType(); if value is error { - return error(value.message()); + return error persist:Error(value.message()); } values.push(value); } @@ -298,7 +298,7 @@ public isolated client class GoogleSheetsClient { } error? response = self.googleSheetClient->updateRowByDataFilter(self.spreadsheetId, self.sheetId, filter, values, "USER_ENTERED"); if response is error { - return error(response.message()); + return error persist:Error(response.message()); } } @@ -318,17 +318,17 @@ public isolated client class GoogleSheetsClient { sheets:ValueRange[]|error rows = self.googleSheetClient->getRowByDataFilter(self.spreadsheetId, self.sheetId, filter); if rows is error { if (deleteKey is map) { - return error(string `Not found: ${self.generateKeyArrayString(self.keyFields, deleteKey)}`); + return error persist:Error(string `Not found: ${self.generateKeyArrayString(self.keyFields, deleteKey)}`); } else { - return error(string `Not found: ${deleteKey.toString()}`); + return error persist:Error(string `Not found: ${deleteKey.toString()}`); } } if rows.length() == 0 { - return error("no element found for delete"); + return error persist:Error("no element found for delete"); } error? response = self.googleSheetClient->deleteRowByDataFilter(self.spreadsheetId, self.sheetId, filter); if response is error { - return error(response.message()); + return error persist:Error(response.message()); } } @@ -348,7 +348,7 @@ public isolated client class GoogleSheetsClient { isolated function (record {}, string[]) returns record {}[]|error associationsMethod = self.associationsMethods.get(entity); record {}[]|error relations = associationsMethod('object, relationFields); if relations is error { - return error("unsupported data format"); + return error persist:Error("unsupported data format"); } 'object[entity] = relations; } @@ -438,7 +438,7 @@ public isolated client class GoogleSheetsClient { return string `${timeValue.hour}-${timeValue.minute}-${(timeValue.second).toString()}`; } - return error("Error: unsupported time format"); + return error persist:Error("Error: unsupported time format"); }