Skip to content

Commit

Permalink
Merge pull request #259 from olha-danylova/master
Browse files Browse the repository at this point in the history
BKNDLSS-34036
  • Loading branch information
danylovaolha authored Oct 17, 2023
2 parents 815d0e2 + 8171316 commit 19f236c
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 142 deletions.
4 changes: 2 additions & 2 deletions BackendlessSwift.podspec
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Pod::Spec.new do |s|
s.name = 'BackendlessSwift'
s.module_name = 'Backendless'
s.version = '7.0.3'
s.source = { :git => 'https://github.com/Backendless/Swift-SDK.git', :tag => '7.0.3' }
s.version = '7.0.4'
s.source = { :git => 'https://github.com/Backendless/Swift-SDK.git', :tag => '7.0.4' }
s.license = { :type => 'MIT', :text => 'Copyright (c) 2013-2023 by Backendless Corp' }
s.homepage = 'http://backendless.com'
s.authors = { 'Mark Piller' => '[email protected]', 'Olha Danylova' => '[email protected]' }
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# RELEASE HISTORY

### 7.0.4 / October 17, 2023
* Fixed an issue when excluding some properties of native types

### 7.0.3 / October 12, 2023
* Added an ability to exlude custom class fields from interacting with database:
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class BackendlessRequestManager {
}
else {
if var params = parameters as? [String : Any] {
params = excludeProperties(properties: params)
params = prepareTransactionsWithMappings(properties: params)
for (key, value) in params {
if let dateValue = value as? Date {
params[key] = DataTypesUtils.shared.dateToInt(date: dateValue)
Expand Down Expand Up @@ -196,70 +196,15 @@ class BackendlessRequestManager {
restMethod.removeLast()
}

private func excludeProperties(properties: [String : Any]) -> [String : Any] {
private func prepareTransactionsWithMappings(properties: [String : Any]) -> [String : Any] {
var resultProperties = properties
let restMethodComponents = restMethod.split(separator: "/").map { String($0) }

for (tableName, fields) in Backendless.shared.data.excludeProperties {
var tableName = tableName
let propertyMappings = Mappings.shared.getColumnToPropertyMappings(className: tableName)
if let originalTableName = Mappings.shared.tableToClassMappings.getKey(forValue: tableName) {
tableName = originalTableName
}
if restMethodComponents.contains(tableName) {
for field in fields {
var field = field
if !propertyMappings.isEmpty,
let propertyName = propertyMappings.getKey(forValue: field) {
field = propertyName
}
resultProperties[field] = nil
}
}
}

// Transactions
if resultProperties.keys.contains("operations"),
resultProperties.keys.contains("isolationLevelEnum"),
let operations = properties["operations"] as? [[String : Any]] {
var resultOperations = [[String : Any]]()

for operation in operations {
var operation = operation
if var payload = operation["payload"] as? [String : Any] {
for (tableName, fields) in Backendless.shared.data.excludeProperties {
let propertyMappings = Mappings.shared.getColumnToPropertyMappings(className: tableName)
for field in fields {
var field = field
if !propertyMappings.isEmpty,
let propertyName = propertyMappings.getKey(forValue: field) {
field = propertyName
}
payload[field] = nil
}
}
operation["payload"] = payload
}
// bulk operations
else if let payloads = operation["payload"] as? [[String : Any]] {
var resultPayloads = [[String : Any]]()
for payload in payloads {
var payload = payload
for (tableName, fields) in Backendless.shared.data.excludeProperties {
let propertyMappings = Mappings.shared.getColumnToPropertyMappings(className: tableName)
for field in fields {
var field = field
if !propertyMappings.isEmpty,
let propertyName = propertyMappings.getKey(forValue: field) {
field = propertyName
}
payload[field] = nil
}
}
resultPayloads.append(payload)
}
operation["payload"] = resultPayloads
}
if var tableName = operation["table"] as? String,
let originalTableName = Mappings.shared.tableToClassMappings.getKey(forValue: tableName) {
tableName = originalTableName
Expand All @@ -268,7 +213,6 @@ class BackendlessRequestManager {
resultOperations.append(operation)
}
resultProperties["operations"] = resultOperations

}
return resultProperties
}
Expand Down
90 changes: 50 additions & 40 deletions Sources/SwiftSDK/Persistence/PersistenceHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/

import Foundation
//import UIKit

class PersistenceHelper {

Expand Down Expand Up @@ -173,6 +174,7 @@ class PersistenceHelper {
var entityDictionary = [String: Any]()

let resultClass = type(of: entity) as! NSObject.Type

var outCount : UInt32 = 0
if let properties = class_copyPropertyList(resultClass.self, &outCount) {

Expand All @@ -181,57 +183,65 @@ class PersistenceHelper {

for i : UInt32 in 0..<outCount {
if let key = NSString(cString: property_getName(properties[Int(i)]), encoding: String.Encoding.utf8.rawValue) as String? {
if let value = (entity as! NSObject).value(forKey: key) {
var resultValue = value
if !(value is String), !(value is NSNumber), !(value is NSNull), !(value is BLGeometry) {
if let arrayValue = value as? [Any] {
var resultArray = [Any]()
for arrayVal in arrayValue {
if !(arrayVal is Bool), !(arrayVal is Int), !(arrayVal is Float), !(arrayVal is Double), !(arrayVal is Character), !(arrayVal is String), !(arrayVal is [String : Any]) {
resultArray.append(entityToDictionaryWithClassProperty(entity: arrayVal))
}
else {
resultArray.append(arrayVal)

if let excludeProperties = Backendless.shared.data.excludeProperties[entityClassName],
excludeProperties.contains(key) {
entityDictionary[key] = nil
}

else {
if let value = (entity as! NSObject).value(forKey: key) {
var resultValue = value
if !(value is NSNull), !(value is String), !(value is NSNumber), /*!(value is UIImage),*/ !(value is BLGeometry) {
if let arrayValue = value as? [Any] {
var resultArray = [Any]()
for arrayVal in arrayValue {
if !(arrayVal is Bool), !(arrayVal is Int), !(arrayVal is Float), !(arrayVal is Double), !(arrayVal is Character), !(arrayVal is String), !(arrayVal is [String : Any]) {
resultArray.append(entityToDictionaryWithClassProperty(entity: arrayVal))
}
else {
resultArray.append(arrayVal)
}
}
resultValue = resultArray
}
resultValue = resultArray
}
else if let dictionaryValue = value as? [String : Any] {
var resultDictionary = [String : Any]()
for (key, dictionaryVal) in dictionaryValue {
if !(dictionaryVal is String), !(dictionaryVal is NSNumber), !(dictionaryVal is NSNull),
!(dictionaryVal is [String]), !(dictionaryVal is [NSNumber]) {
resultDictionary[key] = entityToDictionaryWithClassProperty(entity: dictionaryVal)
}
else {
resultDictionary[key] = dictionaryVal
else if let dictionaryValue = value as? [String : Any] {
var resultDictionary = [String : Any]()
for (key, dictionaryVal) in dictionaryValue {
if !(dictionaryVal is String), !(dictionaryVal is NSNumber), !(dictionaryVal is NSNull),
!(dictionaryVal is [String]), !(dictionaryVal is [NSNumber]) {
resultDictionary[key] = entityToDictionaryWithClassProperty(entity: dictionaryVal)
}
else {
resultDictionary[key] = dictionaryVal
}
}
resultValue = resultDictionary
}
else if let dateValue = value as? Date {
resultValue = DataTypesUtils.shared.dateToInt(date: dateValue)
}
else if let backendlessFileValue = value as? BackendlessFile {
resultValue = backendlessFileValue.fileUrl ?? ""
}
else {
resultValue = entityToDictionaryWithClassProperty(entity: value)
}
resultValue = resultDictionary
}
else if let dateValue = value as? Date {
resultValue = DataTypesUtils.shared.dateToInt(date: dateValue)
}
else if let backendlessFileValue = value as? BackendlessFile {
resultValue = backendlessFileValue.fileUrl ?? ""
if let mappedKey = columnToPropertyMappings.getKey(forValue: key) {
entityDictionary[mappedKey] = resultValue
}
else {
resultValue = entityToDictionaryWithClassProperty(entity: value)
entityDictionary[key] = resultValue
}
if let objectId = StoredObjects.shared.getObjectId(forObject: entity as! AnyHashable) {
entityDictionary["objectId"] = objectId
}
}
if let mappedKey = columnToPropertyMappings.getKey(forValue: key) {
entityDictionary[mappedKey] = resultValue
}
else {
entityDictionary[key] = resultValue
}
if let objectId = StoredObjects.shared.getObjectId(forObject: entity as! AnyHashable) {
entityDictionary["objectId"] = objectId
else if (entity as! NSObject).value(forKey: key) == nil {
entityDictionary[key] = NSNull()
}
}
else if (entity as! NSObject).value(forKey: key) == nil {
entityDictionary[key] = NSNull()
}
}
}
}
Expand Down
Loading

0 comments on commit 19f236c

Please sign in to comment.