-
Notifications
You must be signed in to change notification settings - Fork 2
Add DirectoryName type and improve path validation semantics #3
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -107,6 +107,27 @@ public RelativeDirectoryPath AsRelative(AbsoluteDirectoryPath baseDirectory) | |
| return FilePath.Create<FilePath>(combinedPath); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Combines a directory path with a directory name using the '/' operator. | ||
| /// </summary> | ||
| /// <param name="left">The base directory path.</param> | ||
| /// <param name="right">The directory name to append.</param> | ||
| /// <returns>A new <see cref="DirectoryPath"/> representing the combined path.</returns> | ||
| [SuppressMessage("Usage", "CA2225:Operator overloads have named alternates", Justification = "Path combination is the semantic meaning, not mathematical division")] | ||
| public static DirectoryPath operator /(DirectoryPath left, DirectoryName right) | ||
| { | ||
| #if NET6_0_OR_GREATER | ||
| Guard.NotNull(left); | ||
| Guard.NotNull(right); | ||
| #else | ||
| ArgumentNullExceptionPolyfill.ThrowIfNull(left); | ||
| ArgumentNullExceptionPolyfill.ThrowIfNull(right); | ||
| #endif | ||
|
|
||
| string combinedPath = Path.Combine(left.WeakString, right.WeakString); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this use PooledStringBuilder like the other function, or should the other function not use it? (or is both valid) |
||
| return Create<DirectoryPath>(combinedPath); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Combines a directory path with a file name using the '/' operator. | ||
| /// </summary> | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // Copyright (c) ktsu.dev | ||
| // All rights reserved. | ||
| // Licensed under the MIT license. | ||
|
|
||
| namespace ktsu.Semantics.Paths; | ||
|
|
||
| /// <summary> | ||
| /// Interface for directory names | ||
| /// </summary> | ||
| public interface IDirectoryName | ||
| { | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // Copyright (c) ktsu.dev | ||
| // All rights reserved. | ||
| // Licensed under the MIT license. | ||
|
|
||
| namespace ktsu.Semantics.Paths; | ||
|
|
||
| using ktsu.Semantics.Strings; | ||
|
|
||
| /// <summary> | ||
| /// Represents a directory name | ||
| /// </summary> | ||
| [IsValidDirectoryName] | ||
| public sealed record DirectoryName : SemanticString<DirectoryName>, IDirectoryName | ||
| { | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 0 additions & 55 deletions
55
Semantics.Paths/Validation/Attributes/Path/IsFileNameAttribute.cs
This file was deleted.
Oops, something went wrong.
63 changes: 63 additions & 0 deletions
63
Semantics.Paths/Validation/Attributes/Path/IsValidDirectoryNameAttribute.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| // Copyright (c) ktsu.dev | ||
| // All rights reserved. | ||
| // Licensed under the MIT license. | ||
|
|
||
| namespace ktsu.Semantics.Paths; | ||
|
|
||
| using System; | ||
| using System.IO; | ||
| using ktsu.Semantics.Strings; | ||
|
|
||
| /// <summary> | ||
| /// Validates that a path string contains valid directory name characters (no path separators) using span semantics. | ||
| /// </summary> | ||
| [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)] | ||
| public sealed class IsValidDirectoryNameAttribute : NativeSemanticStringValidationAttribute | ||
| { | ||
| /// <summary> | ||
| /// Creates the validation adapter for valid directory name validation. | ||
| /// </summary> | ||
| /// <returns>A validation adapter for valid directory name strings</returns> | ||
| protected override ValidationAdapter CreateValidator() => new ValidDirectoryNameValidator(); | ||
|
|
||
| /// <summary> | ||
| /// validation adapter for valid directory name strings. | ||
| /// </summary> | ||
| private sealed class ValidDirectoryNameValidator : ValidationAdapter | ||
| { | ||
| private static readonly char[] InvalidFileNameChars = Path.GetInvalidFileNameChars(); | ||
|
|
||
| /// <summary> | ||
| /// Validates that a directory name string contains only valid characters and no path separators. | ||
| /// </summary> | ||
| /// <param name="value">The string value to validate</param> | ||
| /// <returns>A validation result indicating success or failure</returns> | ||
| protected override ValidationResult ValidateValue(string value) | ||
| { | ||
| if (string.IsNullOrEmpty(value)) | ||
| { | ||
| return ValidationResult.Success(); | ||
| } | ||
|
|
||
| // Check for invalid filename characters | ||
| #if NETSTANDARD2_0 | ||
| bool hasInvalidChars = value.IndexOfAny(InvalidFileNameChars) != -1; | ||
| #else | ||
| ReadOnlySpan<char> valueSpan = value.AsSpan(); | ||
| bool hasInvalidChars = valueSpan.IndexOfAny(InvalidFileNameChars) != -1; | ||
| #endif | ||
| if (hasInvalidChars) | ||
| { | ||
| return ValidationResult.Failure("The directory name contains invalid characters."); | ||
| } | ||
|
|
||
| // Check for path separators (directory names shouldn't contain path separators) | ||
| if (value.Contains(Path.DirectorySeparatorChar) || value.Contains(Path.AltDirectorySeparatorChar)) | ||
| { | ||
| return ValidationResult.Failure("The directory name contains path separators."); | ||
| } | ||
|
|
||
| return ValidationResult.Success(); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These guards exist here but not in the / overload within AbsoluteDirectoryPath.
Either this is not required here, or it needs to be added to the other location?