Skip to content

Commit

Permalink
Experiment with path mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
swankjesse committed Apr 16, 2024
1 parent b0f4ebb commit a4f71a1
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
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
}
}
}

0 comments on commit a4f71a1

Please sign in to comment.