All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.aws.smithy.kotlin.runtime.util.Filesystem.kt Maven / Gradle / Ivy

There is a newer version: 0.15.0
Show newest version
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

package aws.smithy.kotlin.runtime.util

/**
 * Abstraction over filesystem
 */
public interface Filesystem {

    /**
     * The delimiter of segments in a path. For example in Linux: /home/user/documents
     * or Windows: C:\Program Files\Notepad.EXE
     */
    val filePathSeparator: String

    /**
     * Read the contents of a file as a [String] or return null on any error.
     *
     * @param path fully qualified path encoded specifically to the target platform's filesystem.
     * @return contents of file or null if error (file does not exist, etc.)
     */
    public suspend fun readFileOrNull(path: String): ByteArray?

    companion object {
        /**
         * Construct a fake filesystem from a mapping of paths to contents
         */
        fun fromMap(data: Map, filePathSeparator: String = "/"): Filesystem = MapFilesystem(data, filePathSeparator)
    }
}

internal class MapFilesystem(
    private val data: Map,
    override val filePathSeparator: String
) : Filesystem {
    override suspend fun readFileOrNull(path: String): ByteArray? = data[path]
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy