Skip to content
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

[style] Expose computed vars for operators + arguments of expressions #307

Merged
merged 5 commits into from
Apr 30, 2021

Conversation

nishant-karajgikar
Copy link
Contributor

@nishant-karajgikar nishant-karajgikar commented Apr 28, 2021

PRs must be submitted under the terms of our Contributor License Agreement CLA.

Fixes: #299

Pull request checklist:

  • Briefly describe the changes in this PR.
  • Write tests for all new functionality. If tests were not written, please explain why.
  • Document any changes to public APIs.
  • Apply changelog label ('breaking change', 'bug 🪲', 'build', 'docs', 'feature 🍏', 'performance ⚡', 'testing 💯') or use the label 'skip changelog'
  • Add an entry inside this element for inclusion in the mapbox-maps-ios changelog: <changelog> Expose the operator and arguments of an expression </changelog>.

Summary of changes

  • Exposes computed vars to allow for introspection of the operator and arguments for an expression.
  • Support string based arrays as valid expression argument

@nishant-karajgikar nishant-karajgikar changed the title [style][wip] Expose computed vars for operators + arguments of expressions [style] Expose computed vars for operators + arguments of expressions Apr 28, 2021
@nishant-karajgikar nishant-karajgikar force-pushed the nishantk/expressions-public-elements branch from 34f013b to 8c715be Compare April 28, 2021 21:16
@nishant-karajgikar nishant-karajgikar added the feature 🍏 When working on a new feature or feature enhancement label Apr 28, 2021
@nishant-karajgikar nishant-karajgikar force-pushed the nishantk/expressions-public-elements branch from 8c715be to 8dbe459 Compare April 28, 2021 21:18
/// The operator of this expression
public var `operator`: Operator {
guard let first = elements.first, case Element.operator(let op) = first else {
fatalError("First element of the expression is not an operator.")
Copy link
Contributor

Choose a reason for hiding this comment

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

If it’s possible to create a malformed expression like this, consider adding a test that this error is thrown, so it doesn’t accidentally fall out during a refactor down the road.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah it's not actually possible to do that.. I'm just fatally erroring to avoid making this return an optional.

}

/// Initialize an expression with an operator and arguments
public init(operator op: Operator, arguments: [Argument] = [.null]) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Does the default of [.null] cause the arguments getter to return a non-empty array?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was an oversight. It's been refactored out.

@@ -112,6 +136,7 @@ public struct Expression: Codable, CustomStringConvertible, Equatable {
case string(String)
case boolean(Bool)
case numberArray([Double])
case stringArray([String])
Copy link
Contributor

Choose a reason for hiding this comment

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

Does the style specification allow for other types to occur inside a literal array, such as an array of booleans or objects, or even heterogeneous arrays (which are allowed in JSON)? What if, instead of separate cases for each type that could occur inside an array, we have just a single array case that takes the evaluated type as an additional associated value?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, i've considered doing this... It's just that modeling those kind of heterogenous arrays is hard. Open to suggestions here.

@nishant-karajgikar nishant-karajgikar force-pushed the nishantk/expressions-public-elements branch from e3121c4 to 9be721b Compare April 29, 2021 14:46
}
}

extension Array: ExpressionArgumentConvertible where Element == Double {
Copy link
Contributor

Choose a reason for hiding this comment

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

Does string array need to be here too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yep it does! Good catch.

Comment on lines +16 to +19
XCTAssertEqual(sumExp.operator, .sum)
XCTAssertEqual(sumExp.arguments.count, 2)
XCTAssertEqual(sumExp.arguments[0], .number(10))
XCTAssertEqual(sumExp.arguments[1], .number(12))
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice

Copy link
Contributor

@julianrex julianrex left a comment

Choose a reason for hiding this comment

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

Just the one question, but refactoring looks good

@nishant-karajgikar nishant-karajgikar force-pushed the nishantk/expressions-public-elements branch from faad6f4 to 5c5388e Compare April 29, 2021 17:25
@nishant-karajgikar nishant-karajgikar merged commit b644658 into main Apr 30, 2021
@nishant-karajgikar nishant-karajgikar deleted the nishantk/expressions-public-elements branch April 30, 2021 17:57
jmkiley pushed a commit that referenced this pull request May 3, 2021
…#307)

* Expose computed vars for operators + arguments of expressions

* Support string based arrays as valid expression argument

* Refactor ExpressionBuilder

* review comment

* cleanup

Remove check for iOS 11

Update LogoView to make class and properties internal

Update OrnamentsManager.options documentation

Updated the changelog

Remove options parameter from MapConfig.setupOrnaments()

Updated changelog to note that MapView.update no longer used for ornaments
MaximAlien pushed a commit that referenced this pull request May 4, 2021
…#307)

* Expose computed vars for operators + arguments of expressions

* Support string based arrays as valid expression argument

* Refactor ExpressionBuilder

* review comment

* cleanup
jmkiley pushed a commit that referenced this pull request May 5, 2021
…#307)

* Expose computed vars for operators + arguments of expressions

* Support string based arrays as valid expression argument

* Refactor ExpressionBuilder

* review comment

* cleanup

Remove check for iOS 11

Update LogoView to make class and properties internal

Update OrnamentsManager.options documentation

Updated the changelog

Remove options parameter from MapConfig.setupOrnaments()

Updated changelog to note that MapView.update no longer used for ornaments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature 🍏 When working on a new feature or feature enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expression should have public accessor for individual elements
3 participants