Skip to content

Dynamic Member Lookup introduced in Swift 4.2, now we can use dot, instead of brackets - #986

Merged
wongzigii merged 11 commits into
SwiftyJSON:xcode10.0from
fredjahanmanesh:DynamicMemberLookupSwift4.2
Sep 30, 2018
Merged

Dynamic Member Lookup introduced in Swift 4.2, now we can use dot, instead of brackets#986
wongzigii merged 11 commits into
SwiftyJSON:xcode10.0from
fredjahanmanesh:DynamicMemberLookupSwift4.2

Conversation

@fredjahanmanesh

@fredjahanmanesh fredjahanmanesh commented Sep 22, 2018

Copy link
Copy Markdown

This proposal introduces a new @dynamicMemberLookup attribute. Types that use it provide "dot" syntax for arbitrary names which are resolved at runtime - in a completely type safe way. This provides syntactic sugar that allows the user to write

introduce-user-defined-dynamic-member-lookup-types

i've added a subscript function with dynamic member, so users can use dot instead of [],

 let name = json.name
 //The same as: let name = json["name"]
 
 let phoneNumber = json.user.contactInfo.phoneNumber
 //The same as: let name = json["user"]["contactInfo"]["phoneNumber"]
  • [yes ] Does this have tests?
  • [ yes] Does this have documentation?
  • [ no] Does this break the public API (Requires major version bump)?
  • [ no] Is this a new feature (Requires minor version bump)?

…that use it provide "dot" syntax for arbitrary names which are resolved at runtime - in a completely type safe way. This provides syntactic sugar that allows the user to write

**[introduce-user-defined-dynamic-member-lookup-types]**(https://github.com/apple/swift-evolution/blob/master/proposals/0195-dynamic-member-lookup.md#introduce-user-defined-dynamic-member-lookup-types)

i've added a subscript function with dynamic member, so users can use dot instead of [],
     ```
let name = json.name
     The same as: let name = json["name"]

     let phoneNumber = json.user.contactInfo.phoneNumber
     The same as: let name = json["user"]["contactInfo"]["phoneNumber"]

```
XCTAssertEqual(json["user", "feeds"], [98833, 23443, 213239, 23232])
}
func testMultilevelDynamicLookUpSetter() {
var json: JSON = ["user": ["id": 987654, "info": ["name": "farshad", "email": "farshadjahanmanesh@gmail.com"], "feeds": [98833, 23443, 213239, 23232]]]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line Length Violation: Line should be 120 characters or less: currently 159 characters (line_length)

XCTAssertTrue(json.z == "zoo")
}
func testMultilevelDynamicLookUpGetter() {
var json: JSON = ["user": ["id": 987654, "info": ["name": "farshad", "email": "farshadjahanmanesh@gmail.com"], "feeds": [98833, 23443, 213239, 23232]]]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line Length Violation: Line should be 120 characters or less: currently 159 characters (line_length)

XCTAssertTrue(json.a == "aoo")
XCTAssertEqual(json.bb, JSON("bpp"))
XCTAssertTrue(json.z == "zoo")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing Whitespace Violation: Lines should not have trailing whitespace. (trailing_whitespace)

Comment thread Source/SwiftyJSON.swift
@fredjahanmanesh

Copy link
Copy Markdown
Author

unknown attribute 'dynamicMemberLookup'

Types that use it provide "dot" syntax for arbitrary names which are resolved at runtime - in a completely type safe way.

https://github.com/apple/swift-evolution/blob/master/proposals/0195-dynamic-member-lookup.md#introduce-user-defined-dynamic-member-lookup-types

am i doing something wrong?

@wongzigii

Copy link
Copy Markdown
Member

I will investigate this awesome evolution in a few days.

@fredjahanmanesh

Copy link
Copy Markdown
Author

retest with new travis image

Comment thread Tests/SwiftyJSONTests/SubscriptTests.swift Outdated
Comment thread Tests/SwiftyJSONTests/SubscriptTests.swift Outdated
@wongzigii

wongzigii commented Sep 26, 2018

Copy link
Copy Markdown
Member

I would prefer to use another swift file to hold these kinds of test cases -- DynamicMemberLookupTests.swift.
What do you think?

Don't forget to add the as-is LICENSE header to the new file.
You can add yourself credit name to the file header like this: https://github.com/SwiftyJSON/SwiftyJSON/blob/master/Tests/MutabilityTests.swift#L3

Comment thread Tests/SwiftyJSONTests/SubscriptTests.swift
}
func testMultilevelDynamicLookUpSetter() {
var json: JSON = ["user": ["id": 987654, "info":
["name": "farshad","email": "farshadjahanmanesh@gmail.com"],

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comma Spacing Violation: There should be no space before and one after any comma. (comma)

@fredjahanmanesh

Copy link
Copy Markdown
Author

I would prefer to use another swift file to hold these kinds of test cases -- DynamicMemberLookupTests.swift.
What do you think?

Don't forget to add the as-is LICENSE header to the new file.
You can add yourself credit name to the file header like this: https://github.com/SwiftyJSON/SwiftyJSON/blob/master/Tests/MutabilityTests.swift#L3

agree, i have created a new test file for dynamic member lookup

Comment thread Tests/SwiftyJSONTests/DynamicMemberLookupTests.swift Outdated
Comment thread Tests/SwiftyJSONTests/DynamicMemberLookupTests.swift

@wongzigii wongzigii left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks !!

@wongzigii
wongzigii merged commit 87d9828 into SwiftyJSON:xcode10.0 Sep 30, 2018
@kagenZhao

Copy link
Copy Markdown

Why is this feature not merged into the main branch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants