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

Walk directories #65

Open
aSemy opened this issue Jun 7, 2023 · 6 comments
Open

Walk directories #65

aSemy opened this issue Jun 7, 2023 · 6 comments
Labels
enhancement New feature or request

Comments

@aSemy
Copy link

aSemy commented Jun 7, 2023

Currently I can only fetch one file directly, but I'd like to be able to fetch all resources within a directory.

Some use-cases:

  • I want to be able to fetch all files in a directory, and filter them (for example, to only get files with a prefix test-a, or only get JSON files
  • I have a very organised directory structure, so that all subdirectories represent a test case with the same 4 files. I want to be able to
    • fetch all directories in commonTest/resources/test-cases/
    • convert each one to a 'test case' class with 4 properties (one for each file)
    • pass each 'test case' to my test

So, I would like to be able to load an arbitrary file-system item, and be able to determine if it is a file or directory (for example, the returned object is in a sealed-interface hierarchy, so I can use a type-check).

@goncalossilva
Copy link
Owner

goncalossilva commented Jun 9, 2023

kotlinx-resources is modelled after Java's resources, so I'm tempted to approach it consistently… Boy, is it ugly. I'm tempted to not include it for this reason, but your second use-case is somewhat compelling. I'm not certain how to design an API that is elegant and consistent under the same design principle. Suggestions welcomed. 🙂 I'll think more about this one, too.

@goncalossilva goncalossilva added the enhancement New feature or request label Jun 9, 2023
@goncalossilva goncalossilva changed the title Feature request: Fetch all resources in a directory Walk directories Jun 9, 2023
@aSemy
Copy link
Author

aSemy commented Jun 12, 2023

I have two ideas:

  1. Code generation

    Define some interfaces to represent the file paths.

    sealed interface TestResource {
      val path: String
    
      interface Directory : TestResource {
        val contents: List<ResourceItem>
      }
    
      interface File: TestResource
    }

    Create a Gradle task that will generate some code for each discovered Directory, along with the file/dir contents.

  2. Network-based filesystem

    Create an implementation of Okio's FileSystem. Using Ktor, create a server and client to send and receive file data. Use a Gradle Build Service to run the Ktor server.

    This is less of an 'idea' and more of a 'total re-write'! But the advantage is that it's totally platform independent.

@lppedd
Copy link

lppedd commented Aug 8, 2023

Vouch for a FileSystem-like API. It doesn't have to be overly complex.
For example java.nio.FileSystem exposes the root path, and from that you can explore sub paths, and check if they're directories or files.

@lppedd
Copy link

lppedd commented Aug 8, 2023

@goncalossilva now that I've experimented with the current API I can share a better feedback.
It seems everything revolves around the Resource class. You create an instance of it, pointing to a path, and it lets you load the content.

I think a more fluent approach would be to provide a FileSystem entry point, from which you can obtain Resource instances. Much like in Java, where a Resource would be a Path.

Encapsulating the file handling code into a file system object would allow to extend it more easily in the future, instead of cluttering an expect class Resource. Plus, a platform-specific FileSystem could be reused not only in tests, but in the main code.

Edit: maybe FileSystem is not a appropriate name, as its scope is much restricted than that. ResourceManager, ResourceSystem are valid alternatives.

@lppedd
Copy link

lppedd commented Sep 13, 2023

@goncalossilva FYI Kotlin/kotlinx-io#213, might be useful for this library's use cases.

@goncalossilva
Copy link
Owner

goncalossilva commented Sep 13, 2023

Nice! Thanks for sharing. It definitely adds to your case above. Consistency is important. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants