-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from Victorvhn/feature/key-case-style
Support for multiple naming conventions
- Loading branch information
Showing
14 changed files
with
226 additions
and
23 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
|
@@ -348,3 +348,5 @@ MigrationBackup/ | |
|
||
# Ionide (cross platform F# VS Code tools) working folder | ||
.ionide/ | ||
|
||
.idea |
This file contains 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 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
16 changes: 16 additions & 0 deletions
16
Uri.Query.String.Composer/Attributes/QueryStringKeyCaseStyleAttribute.cs
This file contains 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,16 @@ | ||
using System; | ||
using UriQueryStringComposer.Enums; | ||
|
||
namespace UriQueryStringComposer.Attributes | ||
{ | ||
[AttributeUsage(AttributeTargets.Property)] | ||
public class QueryStringKeyCaseStyleAttribute : Attribute | ||
{ | ||
public QueryStringKeyCaseStyleAttribute(StringCaseStyle caseStyle) | ||
{ | ||
CaseStyle = caseStyle; | ||
} | ||
|
||
public StringCaseStyle CaseStyle { get; } | ||
} | ||
} |
This file contains 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,13 @@ | ||
# Change Log | ||
|
||
## [2.0.0] - 2023-04-17 | ||
|
||
Support for multiple naming conventions and a new default key case style. | ||
|
||
### Added | ||
- [PR](https://github.com/Victorvhn/uri-query-string-composer/pull/6) | ||
New Attribute to change key case style and a way to change it globally. | ||
|
||
### Changed | ||
- [PR](https://github.com/Victorvhn/uri-query-string-composer/pull/6) | ||
Default key case style changed to CamelCase. |
9 changes: 9 additions & 0 deletions
9
Uri.Query.String.Composer/Configurations/QueryStringComposerOptions.cs
This file contains 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,9 @@ | ||
using UriQueryStringComposer.Enums; | ||
|
||
namespace UriQueryStringComposer.Configurations | ||
{ | ||
public class QueryStringComposerOptions | ||
{ | ||
public StringCaseStyle KeyNameCaseStyle { get; set; } = StringCaseStyle.CamelCase; | ||
} | ||
} |
This file contains 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 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,11 @@ | ||
namespace UriQueryStringComposer.Enums | ||
{ | ||
public enum StringCaseStyle | ||
{ | ||
CamelCase = 1, | ||
PascalCase = 2, | ||
SnakeCase = 3, | ||
KebabCase = 4, | ||
TrainCase = 5 | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
Uri.Query.String.Composer/QueryStringComposer.GetKeyName.cs
This file contains 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,53 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Reflection; | ||
using CaseExtensions; | ||
using UriQueryStringComposer.Attributes; | ||
using UriQueryStringComposer.Enums; | ||
|
||
namespace UriQueryStringComposer | ||
{ | ||
public static partial class QueryStringComposer | ||
{ | ||
private static string GetPropertyKey(MemberInfo property) | ||
{ | ||
var customNameAttribute = property.GetCustomAttribute(typeof(QueryStringKeyNameAttribute)); | ||
|
||
if (customNameAttribute != null) | ||
{ | ||
return ((QueryStringKeyNameAttribute)customNameAttribute).Name; | ||
} | ||
|
||
var stringCaseStyle = GetStringStyle(property); | ||
|
||
return GetStyledKey(property.Name, stringCaseStyle); | ||
} | ||
|
||
private static StringCaseStyle GetStringStyle(MemberInfo property) | ||
{ | ||
var customStringCaseStyleAttribute = property.GetCustomAttribute(typeof(QueryStringKeyCaseStyleAttribute)); | ||
|
||
if (customStringCaseStyleAttribute != null) | ||
{ | ||
var attribute = (QueryStringKeyCaseStyleAttribute) customStringCaseStyleAttribute; | ||
|
||
return attribute.CaseStyle; | ||
} | ||
|
||
return QueryStringComposerConfiguration.Options.KeyNameCaseStyle; | ||
} | ||
|
||
[ExcludeFromCodeCoverage] | ||
private static string GetStyledKey(string key, StringCaseStyle style) | ||
{ | ||
return style switch | ||
{ | ||
StringCaseStyle.CamelCase => key.ToCamelCase(), | ||
StringCaseStyle.PascalCase => key.ToPascalCase(), | ||
StringCaseStyle.SnakeCase => key.ToSnakeCase(), | ||
StringCaseStyle.KebabCase => key.ToKebabCase(), | ||
StringCaseStyle.TrainCase => key.ToTrainCase(), | ||
_ => key | ||
}; | ||
} | ||
} | ||
} |
This file contains 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
13 changes: 13 additions & 0 deletions
13
Uri.Query.String.Composer/QueryStringComposerConfiguration.cs
This file contains 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,13 @@ | ||
using System; | ||
using UriQueryStringComposer.Configurations; | ||
|
||
namespace UriQueryStringComposer | ||
{ | ||
public static class QueryStringComposerConfiguration | ||
{ | ||
public static QueryStringComposerOptions Options { get; } = new QueryStringComposerOptions(); | ||
|
||
public static void Configure(Action<QueryStringComposerOptions> setupAction) => | ||
setupAction(Options); | ||
} | ||
} |
This file contains 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