Skip to content

Commit

Permalink
Merge pull request #20 from ballerina-platform/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
kaneeldias committed Jun 7, 2023
2 parents a5bbe61 + 02cef0d commit c93cefc
Show file tree
Hide file tree
Showing 16 changed files with 325 additions and 589 deletions.
6 changes: 3 additions & 3 deletions ballerina/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
org = "ballerinax"
name = "persist.googlesheets"
version = "1.0.0"
version = "1.0.1"
authors = ["Ballerina"]
keywords = ["persist", "googlesheets", "experimental"]
repository = "https://github.com/ballerina-platform/module-ballerina-persist"
Expand All @@ -12,5 +12,5 @@ distribution = "2201.6.0"
[[platform.java11.dependency]]
groupId = "io.ballerina.stdlib"
artifactId = "persist.googlesheets-native"
version = "1.0.0"
path = "../native/build/libs/persist.googlesheets-native-1.0.0.jar"
version = "1.0.1"
path = "../native/build/libs/persist.googlesheets-native-1.0.1-SNAPSHOT.jar"
16 changes: 8 additions & 8 deletions ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ version = "0.0.0"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]
modules = [
{org = "ballerina", packageName = "lang.runtime", moduleName = "lang.runtime"}
]

[[package]]
org = "ballerina"
Expand Down Expand Up @@ -253,14 +256,11 @@ dependencies = [
{org = "ballerina", name = "io"},
{org = "ballerina", name = "jballerina.java"}
]
modules = [
{org = "ballerina", packageName = "os", moduleName = "os"}
]

[[package]]
org = "ballerina"
name = "persist"
version = "1.0.0"
version = "1.0.1"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]
Expand Down Expand Up @@ -302,7 +302,7 @@ modules = [
[[package]]
org = "ballerina"
name = "time"
version = "2.2.4"
version = "2.2.5"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]
Expand All @@ -313,7 +313,7 @@ modules = [
[[package]]
org = "ballerina"
name = "url"
version = "2.2.3"
version = "2.2.4"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]
Expand Down Expand Up @@ -352,11 +352,11 @@ modules = [
[[package]]
org = "ballerinax"
name = "persist.googlesheets"
version = "1.0.0"
version = "1.0.1"
dependencies = [
{org = "ballerina", name = "http"},
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "os"},
{org = "ballerina", name = "lang.runtime"},
{org = "ballerina", name = "persist"},
{org = "ballerina", name = "test"},
{org = "ballerina", name = "time"},
Expand Down
46 changes: 27 additions & 19 deletions ballerina/googlesheets_client.bal
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public isolated client class GoogleSheetsClient {
private final map<string> & readonly dataTypes;
private final string[] & readonly keyFields;
private isolated function (string[]) returns stream<record {}, persist:Error?>|persist:Error query;
private isolated function (anydata) returns record {}|persist:NotFoundError queryOne;
private isolated function (anydata) returns record {}|persist:Error queryOne;
private final (map<isolated function (record {}, string[]) returns record {}[]|persist:Error>) & readonly associationsMethods;

# Initializes the `GSheetClient`.
Expand Down Expand Up @@ -86,25 +86,25 @@ public isolated client class GoogleSheetsClient {
return <persist:Error>error(output.message());
}
if output.length() > 0 {
return <persist:AlreadyExistsError>error(string `Duplicate key: ${self.generateKeyArrayString(self.keyFields, rowValues)}`);
return persist:getAlreadyExistsError(self.entityName, self.generateKeyRecord(self.keyFields, rowValues));
}
SheetBasicType[] values = [];
foreach string key in fieldMetadataKeys {
string dataType = self.dataTypes.get(key).toString();
if dataType == "time:Date" || dataType == "time:TimeOfDay" ||dataType == "time:Civil" || dataType == "time:Utc" {
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 <persist:Error> error(timeValue.message());
return <persist:Error>error(timeValue.message());
}
string|error value = self.timeToString(timeValue);
if value is error {
return <persist:Error> error(value.message());
return <persist:Error>error(value.message());
}
values.push(value);
} else {
SheetBasicType|error value = rowValues.get(key).ensureType();
if value is error {
return <persist:Error> error(value.message());
return <persist:Error>error(value.message());
}
values.push(value);
}
Expand All @@ -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 <persist:Error> error (insertedRow.message());
return <persist:Error>error(insertedRow.message());
}
error? response = self.googleSheetClient->setRowMetaData(self.spreadsheetId, self.sheetId, insertedRow.rowPosition, "DOCUMENT", self.tableName, metadataValue);
if response is error {
return <persist:Error> error (response.message());
return <persist:Error>error(response.message());
}
}
}
Expand Down Expand Up @@ -188,7 +188,7 @@ public isolated client class GoogleSheetsClient {
string columnName = re ` `.replaceAll(re `"`.replaceAll(columnNames[i], ""), "");
string value = re `"`.replaceAll(rowValue, "");
string dataType = self.dataTypes.get(columnName).toString();
if dataType == "time:Date" || dataType == "time:TimeOfDay" ||dataType == "time:Civil" || dataType == "time:Utc" {
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 <persist:Error>error(typedValue.message());
Expand Down Expand Up @@ -239,12 +239,9 @@ public isolated client class GoogleSheetsClient {
if rows is error {
return <persist:Error>error(rows.message());
}

if rows.length() == 0 {
if key is map<anydata> {
return <persist:NotFoundError>error(string `Not found: ${self.generateKeyArrayString(self.keyFields, key)}`);
} else {
return <persist:NotFoundError>error(string `Not found: ${key.toString()}`);
}
return persist:getNotFoundError(self.entityName, key);
} else if rows.length() > 1 {
return <persist:Error>error(string `Multiple elements found for given key: ${key.toString()}`);
}
Expand Down Expand Up @@ -274,11 +271,13 @@ public isolated client class GoogleSheetsClient {
} else {
SheetBasicType|error value;
string dataType = self.dataTypes.get(entityKey).toString();

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 <persist:Error>error(timeValue.message());
}

value = self.timeToString(timeValue);
if value is error {
return <persist:Error>error(value.message());
Expand All @@ -296,6 +295,7 @@ public isolated client class GoogleSheetsClient {
error? response = self.googleSheetClient->updateRowByDataFilter(self.spreadsheetId, self.sheetId, filter, values, "USER_ENTERED");
if response is error {
return <persist:Error>error(response.message());

}
}

Expand Down Expand Up @@ -363,9 +363,9 @@ public isolated client class GoogleSheetsClient {
private isolated function dataConverter(json value, string dataType) returns SheetFieldType|error {
if dataType == "int" {
return int:fromString(value.toString());
} else if dataType == "time:Date" || dataType == "time:TimeOfDay" ||dataType == "time:Civil" || dataType == "time:Utc" {
} else if dataType == "time:Date" || dataType == "time:TimeOfDay" || dataType == "time:Civil" || dataType == "time:Utc" {
return self.stringToTime(value.toString(), dataType);
} else if dataType == "string"||dataType == "ENUM" {
} else if dataType == "string" || dataType == "ENUM" {
return value.toString();
} else if dataType == "decimal" {
return decimal:fromString(value.toString());
Expand Down Expand Up @@ -400,6 +400,14 @@ public isolated client class GoogleSheetsClient {
return string `[${metadataValue}]`;
}

private isolated function generateKeyRecord(string[] keyFields, map<anydata> rowValues) returns record {} {
record {} keyRecord = {};
foreach string key in keyFields {
keyRecord[key] = rowValues[key];
}
return keyRecord;
}

private isolated function removeUnwantedFields(record {} 'object, string[] fields) {
foreach string keyField in self.keyFields {
if fields.indexOf(keyField) is () {
Expand All @@ -413,7 +421,7 @@ public isolated client class GoogleSheetsClient {
if timeValue is time:Civil {
return time:civilToString(timeValue);
}

if timeValue is time:Utc {
return time:utcToString(timeValue);
}
Expand Down Expand Up @@ -454,11 +462,11 @@ public isolated client class GoogleSheetsClient {

private isolated function stringToTime(string timeValue, string dataType) returns SheetTimeType|error {
if dataType == "time:TimeOfDay" {
string[] timeValues = re `-`.split(timeValue);
string[] timeValues = re `-`.split(timeValue);
time:TimeOfDay output = {hour: check int:fromString(timeValues[0]), minute: check int:fromString(timeValues[1]), second: check decimal:fromString(timeValues[2])};
return output;
} else if dataType == "time:Date" {
string[] timeValues = re `-`.split(timeValue);
string[] timeValues = re `-`.split(timeValue);
time:Date output = {day: check int:fromString(timeValues[0]), month: check int:fromString(timeValues[1]), year: check int:fromString(timeValues[2])};
return output;
} else if dataType == "time:Civil" {
Expand Down
2 changes: 1 addition & 1 deletion ballerina/metadata_types.bal
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public type SheetMetadata record {|
string range;
map<string> dataTypes;
isolated function (string[]) returns stream<record {}, persist:Error?>|persist:Error query;
isolated function (anydata) returns record {}|persist:NotFoundError queryOne;
isolated function (anydata) returns record {}|persist:Error queryOne;
map<isolated function (record {}, string[]) returns record {}[]|persist:Error> associationsMethods;
|};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
// AUTO-GENERATED FILE. DO NOT MODIFY.

// This file is an auto-generated file by Ballerina persistence layer for model.
// It should not be modified by hand.
// Copyright (c) 2023 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.

import ballerina/persist;
import ballerina/jballerina.java;
import ballerinax/googleapis.sheets;
import ballerina/http;
import ballerina/persist;
import ballerinax/googleapis.sheets;

const ORDER_ITEM_EXTENDED = "orderitemextendeds";

Expand Down Expand Up @@ -135,7 +146,7 @@ public isolated client class GoogleSheetsRainierClientAllDataType {
return outputArray.toStream();
}

private isolated function queryOneOrderitemextendeds(anydata key) returns record {}|persist:NotFoundError {
private isolated function queryOneOrderitemextendeds(anydata key) returns record {}|persist:Error {
stream<OrderItemExtended, persist:Error?> orderitemextendedsStream = self.queryOrderitemextendedsStream();
error? unionResult = from record {} 'object in orderitemextendedsStream
where persist:getKey('object, ["orderId", "itemId"]) == key
Expand All @@ -145,9 +156,9 @@ public isolated client class GoogleSheetsRainierClientAllDataType {
};
};
if unionResult is error {
return <persist:NotFoundError>error(unionResult.message());
return error persist:Error(unionResult.message());
}
return <persist:NotFoundError>error("Invalid key: " + key.toString());
return persist:getNotFoundError("OrderItemExtended", key);
}

private isolated function queryOrderitemextendedsStream(OrderItemExtendedTargetType targetType = <>) returns stream<targetType, persist:Error?> = @java:Method {
Expand Down
Loading

0 comments on commit c93cefc

Please sign in to comment.