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

Initial APIs for FileSystem extensions #1470

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Experiment with path mapping
  • Loading branch information
swankjesse committed Apr 16, 2024
commit a4f71a1c863d28c98b1b83dffdb45d5b4b8b5a36
32 changes: 31 additions & 1 deletion okio/src/commonMain/kotlin/okio/FileSystemExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,35 @@ package okio
/**
* Marks an object that can be attached to a [FileSystem], and that supplements the file system's
* capabilities.
*
* Implementations must support transforms to input and output paths with [PathMapper]. To simplify
* implementation, use [PathMapper.NONE] by default and use [chain] to combine mappers.
*
* ```kotlin
* class DiskUsageExtension private constructor(
* private val pathMapper: PathMapper,
* ) : FileSystemExtension {
* constructor() : this(PathMapper.NONE)
*
* override fun map(pathMapper: PathMapper): FileSystemExtension {
* return DiskUsageExtension(chain(pathMapper, this.pathMapper))
* }
*
* fun sizeOnDisk(path: Path): Long {
* val mappedPath = pathMapper.onPathParameter(path, "sizeOnDisk", "path")
* return lookUpSizeOnDisk(mappedPath)
* }
*
* fun largestFiles(): Sequence<Path> {
* val largestFiles: Sequence<Path> = lookUpLargestFiles()
* return largestFiles.map {
* pathMapper.onPathResult(it, "largestFiles")
* }
* }
* }
* ```
*/
interface FileSystemExtension
interface FileSystemExtension {
/** Returns a file system of the same type, that applies [pathMapper] to all paths. */
fun map(pathMapper: PathMapper) : FileSystemExtension
}
27 changes: 27 additions & 0 deletions okio/src/commonMain/kotlin/okio/Okio.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,30 @@ inline fun <reified E : FileSystemExtension> FileSystem.extend(extension: E): Fi

/** Returns the extension for [E] if it exists, and null otherwise. */
inline fun <reified E : FileSystemExtension> FileSystem.extension(): E? = extension(E::class)


fun chain(outer: PathMapper, inner: PathMapper): PathMapper {
return object : PathMapper {
override fun onPathParameter(path: Path, functionName: String, parameterName: String): Path {
return inner.onPathParameter(
outer.onPathParameter(
path,
functionName,
parameterName,
),
functionName,
parameterName,
)
}

override fun onPathResult(path: Path, functionName: String): Path {
return outer.onPathResult(
inner.onPathResult(
path,
functionName,
),
functionName,
)
}
}
}
13 changes: 13 additions & 0 deletions okio/src/commonMain/kotlin/okio/PathMapper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package okio

interface PathMapper {
fun onPathParameter(path: Path, functionName: String, parameterName: String): Path
fun onPathResult(path: Path, functionName: String): Path

companion object {
val NONE = object : PathMapper {
override fun onPathParameter(path: Path, functionName: String, parameterName: String) = path
override fun onPathResult(path: Path, functionName: String) = path
}
}
}
Loading