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

Provide APIs to read file into more data types #2923

Merged

Conversation

clintonpi
Copy link
Contributor

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'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.

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.
Copy link
Contributor

@glbrntt glbrntt left a 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) }
Copy link
Contributor

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:

public init(buffer: ByteBuffer, byteTransferStrategy: ByteBuffer.ByteTransferStrategy = .automatic) {
var buffer = buffer
self = buffer.readData(length: buffer.readableBytes, byteTransferStrategy: byteTransferStrategy)!
}

Comment on lines 32 to 35
try await handle.readToEnd(
fromAbsoluteOffset: 0,
maximumSizeAllowed: maximumSizeAllowed
)
Copy link
Contributor

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

Suggested change
try await handle.readToEnd(
fromAbsoluteOffset: 0,
maximumSizeAllowed: maximumSizeAllowed
)
try await handle.readToEnd(maximumSizeAllowed: maximumSizeAllowed)

Comment on lines 34 to 37
try await handle.readToEnd(
fromAbsoluteOffset: 0,
maximumSizeAllowed: maximumSizeAllowed
)
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here re: 0

Suggested change
try await handle.readToEnd(
fromAbsoluteOffset: 0,
maximumSizeAllowed: maximumSizeAllowed
)
try await handle.readToEnd(maximumSizeAllowed: maximumSizeAllowed)

Comment on lines 39 to 41
let path = FilePath(#filePath)

try await fs.withFileHandle(forReadingAndWritingAt: path) { fileHandle in
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Comment on lines 1833 to 1854
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])
}
Copy link
Contributor

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.

@clintonpi clintonpi requested a review from glbrntt October 15, 2024 10:26
Copy link
Contributor

@glbrntt glbrntt left a 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
Copy link
Contributor

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

@clintonpi clintonpi requested a review from glbrntt October 15, 2024 12:26
@glbrntt glbrntt added the 🆕 semver/minor Adds new public API. label Oct 15, 2024
@glbrntt glbrntt enabled auto-merge (squash) October 15, 2024 16:01
@glbrntt glbrntt merged commit cc1c57c into apple:main Oct 21, 2024
30 of 31 checks passed
@clintonpi clintonpi deleted the provide-apis-to-read-file-into-more-data-types branch October 21, 2024 08:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🆕 semver/minor Adds new public API.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants