Skip to content

@dynamicMemberLookup for BackendlessUser #256

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

Merged
merged 1 commit into from
Oct 3, 2023
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
11 changes: 11 additions & 0 deletions Sources/SwiftSDK/UserService/BackendlessUser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import Foundation

@dynamicMemberLookup
@objcMembers public class BackendlessUser: NSObject, Codable {

public var objectId: String?
Expand All @@ -39,6 +40,16 @@ import Foundation

public private(set) var userToken: String?

public subscript(dynamicMember property: String) -> Any? {
get {
properties[property]
}

set {
properties[property] = newValue
}
}

var userProperties = JSON()
public var properties: [String : Any] {
get {
Expand Down
31 changes: 31 additions & 0 deletions Tests/SwiftSDKTests/UserServiceTests/BackendlessUserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,37 @@ class BackendlessUserTests: XCTestCase {
XCTAssertTrue(user.properties["city"] is NSNull)
}

func test10AddPropertyWithDynamicMemberLookup() {
let user = createBackendlessUser()
user.foo = "bar"
XCTAssertNotNil(user.properties["foo"])
XCTAssertFalse(user.properties["foo"] is NSNull)
}

func test11AddPropertiesWithDynamicMemberLookup() {
let user = createBackendlessUser()
user.foo = "bar"
user.foo1 = "bar1"
XCTAssertNotNil(user.properties["foo"])
XCTAssertNotNil(user.properties["foo1"])
XCTAssertFalse(user.properties["foo"] is NSNull)
XCTAssertFalse(user.properties["foo1"] is NSNull)
}

func test12UpdatePropertyWithDynamicMemberLookup() {
let user = createBackendlessUser()
user.age = 55
XCTAssertEqual(user.properties["age"] as? Int, 55)
}

func test13UpdatePropertiesWithDynamicMemberLookup() {
let user = createBackendlessUser()
user.name = "Bob"
user.age = 55
XCTAssertEqual(user.properties["name"] as? String, "Bob")
XCTAssertEqual(user.properties["age"] as? Int, 55)
}

// ********************************************************************************

private func createBackendlessUser() -> BackendlessUser {
Expand Down