dennisdoomen / dennisdoomen/pathy

[API Proposal]: Length and IsEmptyDirectory

Open
#133 0 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
C#
Stars
27
Forks
5
Avg merge
21h 29m
Merged PRs (30d)
10

Description

### Background and motivation

Two very common questions about a file system entry are not answerable through Pathy today: how big is this file, and does this directory contain anything. Both currently require dropping down to `ToFileInfo()` or `ToDirectoryInfo()` and writing the enumeration by hand. The library already exposes comparable information through `Exists`, `IsFile`, `IsDirectory` and `LastWriteTimeUtc`, so these fit the existing shape.

### API Proposal

```C#
namespace Pathy
{
public readonly struct ChainablePath
{
public long Length { get; }
public bool IsEmptyDirectory { get; }
}
}
```

### API Usage

```C#
var artifact = artifactsDirectory / "Pathy.nupkg";

if (artifact.Length == 0)
{
throw new InvalidOperationException("The packaging step produced an empty file.");
}

if ((ChainablePath.Current / "TestResults").IsEmptyDirectory)
{
Console.WriteLine("No test results were produced.");
}
```

### Alternative Designs

* Name the size property `Size`. `Length` matches `FileInfo.Length`, which is the more familiar name for .NET developers.
* Return `long?` so a missing file yields `null` rather than throwing. Worth discussing, since the existing `LastWriteTimeUtc` sets the precedent for how missing entries are handled.

### Risks

The behaviour for a path that does not exist, or that points at a directory when `Length` is requested, must be consistent with `LastWriteTimeUtc`. `IsEmptyDirectory` on a non-existent path needs a defined answer too. Both properties hit the file system, so they should not be presented as cheap.

Contributor guide

Open the contributing guide

Research direction

Start by inspecting the existing LastWriteTimeUtc behavior and the ToFileInfo() and ToDirectoryInfo() paths mentioned in the issue. Resolve how missing paths, directories queried through Length, and non-existent directories should behave, then define and implement the two proposed ChainablePath properties with matching tests and documentation.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
operating-systems
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.