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

Parser for list of parameters #8

Merged
merged 14 commits into from
Feb 15, 2017
Merged

Conversation

djbe
Copy link
Member

@djbe djbe commented Jan 15, 2017

Implements these issues:

This parses a list of strings (Array<String>) into a dictionary ([String: Any]). You can use it as follows:

let template = ...load template...
var context = ...generate context...

context["args"] = Parameters.parse(items: items)
let result = try template.render(context)

@djbe djbe force-pushed the feature/parameter-parser branch from 22112a5 to 579ab3a Compare January 16, 2017 17:20
@AliSoftware AliSoftware force-pushed the master branch 2 times, most recently from 9625443 to e9e33c6 Compare January 22, 2017 16:09
@AliSoftware AliSoftware force-pushed the feature/parameter-parser branch from 579ab3a to f528fe1 Compare January 22, 2017 16:46
AliSoftware added a commit that referenced this pull request Jan 22, 2017
@djbe djbe force-pushed the feature/parameter-parser branch from f528fe1 to 245fca3 Compare January 28, 2017 14:56
djbe pushed a commit that referenced this pull request Jan 28, 2017
@djbe
Copy link
Member Author

djbe commented Jan 28, 2017

Is this what you guys had in mind? Should I add some sort of method for easily adding this to an (existing) context?

@djbe djbe requested a review from AliSoftware January 28, 2017 15:03
Copy link
Contributor

@AliSoftware AliSoftware left a comment

Choose a reason for hiding this comment

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

You should add tests for:

  • Invalid syntax & invalid key error cases
  • more than one-level-deep structured keys(foo.bar.baz.qux=1)
  • inconsistent structures mixing keys used both as root for sub keys and as keys for raw values (foo=1 foo.bar=2 foo.bar=3 foo=4)

@djbe
Copy link
Member Author

djbe commented Jan 28, 2017

invalid keys?

return result
}

private static func parse(item: Parameter, result: StringDict) throws -> StringDict {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe rename that private function to splitAndRepeat or similar? Making it have the same name as the public one was confusing about its intend on first read

@AliSoftware
Copy link
Contributor

I meant whatever use case the ParameterError.invalidKey(key: String, value: String) is for.

@djbe
Copy link
Member Author

djbe commented Jan 28, 2017

Yeah just saw that. Honestly can't remember when that'd happen. Which means the code needs comments!

@AliSoftware
Copy link
Contributor

Honestly can't remember when that'd happen. Which means the code needs comments!

And tests for those cases you thought about when writing that but forgot since then… making those cases likely to be forgotten again in the future and leading to regressions 😜

@djbe djbe force-pushed the feature/parameter-parser branch from c13cb84 to 7294fac Compare January 28, 2017 16:29
var result = StringDict()
for parameter in parameters {
result = try parse(item: parameter, result: result)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

parameters.reduce(StringDict()) { try Parse(item: $1, résultats: $0) }?

var result = StringDict()
for parameter in parameters {
result = try parse(parameter: parameter, result: result)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

parameters.reduce(StringDict()) { try Parse(item: $1, résultats: $0) }?

Copy link
Member Author

Choose a reason for hiding this comment

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

That french autocorrect 😆

Copy link
Contributor

Choose a reason for hiding this comment

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

😂

}

// a valid key is not empty and only alphanumerical
private static func validate(key: String) -> Bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this means 1 is a valid key too?

Copy link
Member Author

Choose a reason for hiding this comment

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

Right now, yeah. Shouldn't matter that much, say I want to create a (fake) tuple.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok. Fine by be

var result = StringDict()
for parameter in parameters {
result = try parse(parameter: parameter, result: result)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

😂

} catch ParametersError.invalidSyntax {
// That's the expected exception we want to happen
} catch let error {
XCTFail("Unexpected error occured while parsing: \(error)")
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't there some XCTAssertThrows or similar?

Copy link
Member Author

Choose a reason for hiding this comment

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

No idea, I'll look into it. The structure is just based on the SwiftGenKit (colors) tests

Copy link
Contributor

Choose a reason for hiding this comment

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

That doesn't mean I was right at that time 😂

Copy link
Member Author

Choose a reason for hiding this comment

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

And I'm the testing noob 🤣

@djbe djbe force-pushed the feature/parameter-parser branch 2 times, most recently from f8ecf4c to ae51ca0 Compare January 30, 2017 17:15
djbe pushed a commit that referenced this pull request Jan 30, 2017
@djbe djbe modified the milestone: 1.0.0 Jan 31, 2017
if parts.count == 2 {
return (key: parts[0], value: parts[1])
if parts.count >= 2 {
return (key: parts[0], value: parts.dropFirst().joined(separator: "="))
} else if let part = parts.first, parts.count == 1 && validate(key: part) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Does that mean that foo.bar.baz isn't a valid boolean key? (as validate(key: "foo.bar.baz") will return false)?

}

private static let notAlfanumericsAndDot: CharacterSet = {
Copy link
Contributor

Choose a reason for hiding this comment

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

s/alfa/alpha 😄

@@ -86,6 +90,7 @@ class ParametersTests: XCTestCase {
}

func testInvalidKey() {
// key may only be alfanumeric or '.'
Copy link
Contributor

Choose a reason for hiding this comment

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

s/alfa/alpha/

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

Successfully merging this pull request may close these issues.

3 participants