Skip to content

Commit

Permalink
Merge pull request #14 from KeanuPang/feature/update_parsing_values
Browse files Browse the repository at this point in the history
Trimming trailing whitespace for values; remove escape double quotes
  • Loading branch information
thebarndog authored May 24, 2022
2 parents 4b4a19d + 1521a89 commit 84cc90f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
11 changes: 8 additions & 3 deletions Sources/Environment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public struct Environment {
} else if let integerValue = Int(stringValue) {
self = .integer(integerValue)
} else {
self = .string(stringValue)
// replace escape double quotes
self = .string(stringValue.trimmingCharacters(in: .init(charactersIn: "\"")).replacingOccurrences(of: "\\\"", with: "\""))
}
}

Expand Down Expand Up @@ -180,12 +181,16 @@ public struct Environment {
// we loop over all the entries in the file which are already separated by a newline
var values: OrderedDictionary<String, Value> = .init()
for line in lines {
// ignore comments
if line.starts(with: "#") {
continue
}
// split by the delimeter
let substrings = line.split(separator: Self.delimeter)
// make sure we can grab two and only two string values
guard
let key = substrings.first,
let value = substrings.last,
let key = substrings.first?.trimmingCharacters(in: .whitespacesAndNewlines),
let value = substrings.last?.trimmingCharacters(in: .whitespacesAndNewlines),
substrings.count == 2,
!key.isEmpty,
!value.isEmpty else {
Expand Down
3 changes: 3 additions & 0 deletions Tests/DotenvTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ final class DotenvTests: XCTestCase {
print(env.values)
XCTAssertEqual(env.apiKey, .string("some-value"))
XCTAssertEqual(env.buildNumber, .integer(5))
XCTAssertEqual(env.identifier, .string("com.app.example"))
XCTAssertEqual(env.mailTemplate, .string("The \"Quoted\" Title"))
XCTAssertEqual(env.dbPassphrase, .string("1qaz?#@\"' wsx$"))
XCTAssertNil(env.nonExistentValue)
}

Expand Down
3 changes: 3 additions & 0 deletions Tests/Resources/fixture.env
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
API_KEY=some-value
BUILD_NUMBER=5
IDENTIFIER="com.app.example"
MAIL_TEMPLATE="The "Quoted" Title"
DB_PASSPHRASE="1qaz?#@"' wsx$"

0 comments on commit 84cc90f

Please sign in to comment.