8000 Use stricter types for path creation by icidasset · Pull Request #466 · oddsdk/ts-odd · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Use stricter types for path creation #466

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

Merged
merged 7 commits into from
Jan 25, 2023
Merged

Conversation

icidasset
Copy link
Contributor
@icidasset icidasset commented Jan 20, 2023

This restricts the paths that can be used with the file system POSIX interface. For example, fs.write now requires a file path that starts with "private" or "public" and must have at least one more path segment.

fs.write(
  path.file("private", "fileName")
)

The other neat thing about this is when you start writing the following in your editor with the Typescript LSP:

await fs.write(
  Path.file("")
)

It'll suggest "private" or "public" so people don't have to guess anymore what to type in there as the first item.

Further notes

  • Branch has been renamed to RootBranch
  • Introduces the concept of Partitions which are RootBranches that can be used the POSIX file system interface.
  • You can also use the RootBranch enum to provide string values, for the people that prefer to use a constant.
  • I might have unnecessarily restricted some functions, let me know if you find any.
  • My only worry about this is that people won't be able to write these types themselves. That said, I don't think anyone will need to.

Thoughts?

How to write these types by hand

import { Partitioned, PartitionedNonEmpty, Segments, SegmentsNonEmpty } from "webnative/path/index"
import { Partition, Private, Public } from "webnative/path/index"
import { DirectoryPath, FilePath } from "webnative/path/index"

// Regular old path, no restrictions
path.file() as FilePath<Segments> // { file: [] }

// Must have at least one segment
path.file("segment") as FilePath<SegmentsNonEmpty>

// Must use a WNFS partition, can be public or private partition
path.directory("public") as DirectoryPath<Partitioned<Partition>>
8000
>

// Must use the private partition
path.directory("private") as DirectoryPath<Partitioned<Private>>>

// Must use the private partition and have at least one segment besides the partition
path.directory("private", "dir") as DirectoryPath<PartitionedNonEmpty<Private>>>

Closes

Closes #457

@@ -13,6 +13,10 @@
- Adds `program.accountDID(username)` shorthand method to retrieve the root DID of a given account username.
- Adds the file system data functions as shorthand methods.

#### Other

- Introduces stricter types for paths to restrict the paths you can use with various functions and also to guide you a bit more.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've put this here since it shouldn't be a breaking change.
Unless you've been using the file system incorrectly 🙈

@icidasset icidasset requested review from bgins and avivash January 20, 2023 15:31
(): DirectoryPath
(path: DirectoryPath): DirectoryPath
(path: FilePath): FilePath
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot to remove this, leftovers from 0.34

Copy link
Member
@avivash avivash left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

woot! 🚀

Copy link
Member
@bgins bgins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! 🎉

I left a couple of comments around where the types might be too restrictive or not restrictive enough, and one naming thing.

Love how the LSP suggests "public" or "private". Nice added bonus. ❤️

get(path: DistinctivePath): Promise<PuttableUnixTree | File | null>
mv(from: DistinctivePath, to: DistinctivePath): Promise<this>
rm(path: DistinctivePath): Promise<this>
exists(path: DistinctivePath<PartitionedNonEmpty<Partition>>): Promise<boolean>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the public and private partitions guaranteed to exist? Is there any time that a developer would need to check for them? If so, this type may prevent them from doing so.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The partitions are guaranteed to exist yeah.


// Files
write(
path: DistinctivePath,
path: DistinctivePath<PartitionedNonEmpty<Partition>>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be restricted further to a FilePath? Are there cases where it is possible to write to a directory?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can't no. Since we've removed add and only kept write, we had to move the functionality from add to write. You can add symlinks to a directory with write, so we'll need directory and file paths. Actually now that I think about this, this shouldn't be NonEmpty, because you can add symlinks to just private/

icidasset and others added 3 commits January 24, 2023 13:04
Co-authored-by: Brian Ginsburg <7957636+bgins@users.noreply.github.com>
Signed-off-by: Steven Vandevelde <icid.asset@gmail.com>
@icidasset icidasset merged commit e323e8e into main Jan 25, 2023
@icidasset icidasset deleted the icidasset/branched-paths branch January 25, 2023 09:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve path creation
3 participants
0