-
Notifications
You must be signed in to change notification settings - Fork 58
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
Conversation
22112a5
to
579ab3a
Compare
9625443
to
e9e33c6
Compare
579ab3a
to
f528fe1
Compare
f528fe1
to
245fca3
Compare
Is this what you guys had in mind? Should I add some sort of method for easily adding this to an (existing) context? |
There was a problem hiding this 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
)
invalid keys? |
Sources/Parameters.swift
Outdated
return result | ||
} | ||
|
||
private static func parse(item: Parameter, result: StringDict) throws -> StringDict { |
There was a problem hiding this comment.
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
I meant whatever use case the |
Yeah just saw that. 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 😜 |
c13cb84
to
7294fac
Compare
Sources/Parameters.swift
Outdated
var result = StringDict() | ||
for parameter in parameters { | ||
result = try parse(item: parameter, result: result) | ||
} |
There was a problem hiding this comment.
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) }
?
Sources/Parameters.swift
Outdated
var result = StringDict() | ||
for parameter in parameters { | ||
result = try parse(parameter: parameter, result: result) | ||
} |
There was a problem hiding this comment.
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) }
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That french autocorrect 😆
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😂
Sources/Parameters.swift
Outdated
} | ||
|
||
// a valid key is not empty and only alphanumerical | ||
private static func validate(key: String) -> Bool { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. Fine by be
Sources/Parameters.swift
Outdated
var result = StringDict() | ||
for parameter in parameters { | ||
result = try parse(parameter: parameter, result: result) | ||
} |
There was a problem hiding this comment.
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)") |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 😂
There was a problem hiding this comment.
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 🤣
f8ecf4c
to
ae51ca0
Compare
6aafc52
to
5159be2
Compare
Sources/Parameters.swift
Outdated
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) { |
There was a problem hiding this comment.
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)?
Sources/Parameters.swift
Outdated
} | ||
|
||
private static let notAlfanumericsAndDot: CharacterSet = { |
There was a problem hiding this comment.
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 '.' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/alfa/alpha/
Implements these issues:
This parses a list of strings (
Array<String>
) into a dictionary ([String: Any]). You can use it as follows: