-
Notifications
You must be signed in to change notification settings - Fork 652
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
Provide APIs to read file into more data types #2923
Provide APIs to read file into more data types #2923
Conversation
Motivation: As requested in issues [apple#2875](apple#2875) and [apple#2876](apple#2876), it would be convenient to be able to read the contents of a file into more data types such as `Array`, `ArraySlice` & Foundation's `Data`. Modifications: - Extend `Array`, `ArraySlice` & `Data` to be initialisable with the contents of a file. Result: The contents of a file can be read into more data types.
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.
Looks good but a few things need fixing up before we can merge this.
) | ||
} | ||
|
||
self = byteBuffer.withUnsafeReadableBytes { Self($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.
The NIOFoundationCompat module provides an extension on Data
which you should use instead:
swift-nio/Sources/NIOFoundationCompat/ByteBuffer-foundation.swift
Lines 403 to 406 in f7dc3f5
public init(buffer: ByteBuffer, byteTransferStrategy: ByteBuffer.ByteTransferStrategy = .automatic) { | |
var buffer = buffer | |
self = buffer.readData(length: buffer.readableBytes, byteTransferStrategy: byteTransferStrategy)! | |
} |
try await handle.readToEnd( | ||
fromAbsoluteOffset: 0, | ||
maximumSizeAllowed: maximumSizeAllowed | ||
) |
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.
0 is the default, so no need to specify it here
try await handle.readToEnd( | |
fromAbsoluteOffset: 0, | |
maximumSizeAllowed: maximumSizeAllowed | |
) | |
try await handle.readToEnd(maximumSizeAllowed: maximumSizeAllowed) |
try await handle.readToEnd( | ||
fromAbsoluteOffset: 0, | ||
maximumSizeAllowed: maximumSizeAllowed | ||
) |
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.
Same here re: 0
try await handle.readToEnd( | |
fromAbsoluteOffset: 0, | |
maximumSizeAllowed: maximumSizeAllowed | |
) | |
try await handle.readToEnd(maximumSizeAllowed: maximumSizeAllowed) |
let path = FilePath(#filePath) | ||
|
||
try await fs.withFileHandle(forReadingAndWritingAt: path) { fileHandle in |
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.
Writing into this source file is a bad idea (think about the local development experience of running this test). Reading is fine, writing less so.
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.
True, I missed that. I worked with temporaryFilePath()
locally but swapped it with FilePath(#filePath)
just before committing. FilePath(#filePath)
fails for me locally due to permission issues so I was depending on the CI tests and didn't re-test locally after swapping.
let path = FilePath(#filePath) | ||
|
||
try await self.fs.withFileHandle(forReadingAndWritingAt: path) { fileHandle in | ||
_ = try await fileHandle.write(contentsOf: [0, 1, 2], toAbsoluteOffset: 0) | ||
} | ||
|
||
let contents = try await Array(contentsOf: path, maximumSizeAllowed: .bytes(1024)) | ||
|
||
XCTAssertEqual(contents, [0, 1, 2]) | ||
} | ||
|
||
func testReadIntoArraySlice() async throws { | ||
let path = FilePath(#filePath) | ||
|
||
try await self.fs.withFileHandle(forReadingAndWritingAt: path) { fileHandle in | ||
_ = try await fileHandle.write(contentsOf: [0, 1, 2], toAbsoluteOffset: 0) | ||
} | ||
|
||
let contents = try await ArraySlice(contentsOf: path, maximumSizeAllowed: .bytes(1024)) | ||
|
||
XCTAssertEqual(contents, [0, 1, 2]) | ||
} |
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.
Same comments apply here as well: please don't write into this source file.
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.
One nit, but looks good otherwise.
contentsOf path: FilePath, | ||
maximumSizeAllowed: ByteCount, | ||
fileSystem: some FileSystemProtocol, | ||
byteTransferStrategy: ByteBuffer.ByteTransferStrategy = .automatic |
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.
We don't think we need to expose this, we can just use the default behaviour
Motivation:
As requested in issues #2875 and #2876, it would be convenient to be able to read the contents of a file into more data types such as
Array
,ArraySlice
& Foundation'sData
.Modifications:
Array
,ArraySlice
&Data
to be initialisable with the contents of a file.Result:
The contents of a file can be read into more data types.