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

Refactorings in preparation of .NET 9.0 support #16575

Merged
merged 4 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Microsoft.AspNetCore.Mvc.ModelBinding;
using OrchardCore.ContentManagement.Metadata.Models;
using OrchardCore.ContentManagement.Metadata.Settings;
using OrchardCore.Mvc.Utilities;
using OrchardCore.ContentManagement.Utilities;

namespace OrchardCore.ContentTypes.ViewModels;

Expand All @@ -26,7 +26,7 @@ public EditPartViewModel(ContentPartDefinition contentPartDefinition)
[Required]
public string DisplayName
{
get { return !string.IsNullOrWhiteSpace(_displayName) ? _displayName : Name.TrimEnd("Part").CamelFriendly(); }
get { return !string.IsNullOrWhiteSpace(_displayName) ? _displayName : Name.TrimEndString("Part").CamelFriendly(); }
set { _displayName = value; }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public async Task<IActionResult> GetAuthorizedById(string id)
}

[HttpPost]
[Authorize]
public async Task<IActionResult> AddContent(ContentItem contentItem)
{
if (!await _authorizationService.AuthorizeAsync(User, Permissions.DemoAPIAccess))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using GraphQL.Resolvers;
using GraphQL.Types;
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.GraphQL;
using OrchardCore.ContentManagement.GraphQL.Queries.Types;
using OrchardCore.ContentManagement.Metadata.Models;
using OrchardCore.Menu.Models;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public MenuItemsListQueryObjectType()
Name = "MenuItemsListPart";

Field<ListGraphType<MenuItemInterface>>("menuItems")
.Description("The menu items.")
.Description("The menu items.")
.Resolve(context => context.Source.MenuItems);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, Jso

if (!reader.TryGetDateTime(out var value))
{
var stringValue = reader.GetString();
var stringValue = reader.GetString();
if (DateTime.TryParse(stringValue, out value))
{
return value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using OrchardCore.ContentManagement.Metadata.Builders;
using OrchardCore.ContentManagement.Metadata.Models;
using OrchardCore.ContentManagement.Utilities;
Expand Down Expand Up @@ -58,7 +57,7 @@ public static string DisplayName(this ContentPartDefinition part)

if (string.IsNullOrEmpty(displayName))
{
displayName = part.Name.TrimEnd("Part");
displayName = part.Name.TrimEndString("Part");
}

return displayName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,8 @@ public static string ReplaceAll(this string original, IDictionary<string, string
return Regex.Replace(original, pattern, match => replacements[match.Value]);
}

#if NET8_0
[Obsolete("Don't use 'TrimEnd' as this has a different behavior in .NET 9.0. Use 'OrchardCore.ContentManagement.Utilities.TrimEndString' instead.")]
public static string TrimEnd(this string value, string trim = "")
{
if (value == null)
Expand All @@ -356,6 +358,19 @@ public static string TrimEnd(this string value, string trim = "")
? value[..^trim.Length]
: value;
}
#endif

public static string TrimEndString(this string value, string suffix)
Copy link
Member

Choose a reason for hiding this comment

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

Could you please elaborate what are the different behaviors?

In both cases, we can have the same name and move #if .. #elseif .. #endif inside the function, this way no need to have a new method name

Copy link
Member Author

Choose a reason for hiding this comment

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

TrimEnd exists in net9.0, so we need to hide it when we build in net9.0.
And in net9.0 TrimEnd("Part") is equivalent to TrimEnd(new char[] { 'P', 'a', 'r', 't' }) so "MenuListPart" would become "MenuLis" without the t.

Copy link
Member

Choose a reason for hiding this comment

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

Seems a new overload has been added in .NET 9.0

{
if (string.IsNullOrWhiteSpace(value))
{
return value;
}

return value.EndsWith(suffix, StringComparison.Ordinal)
? value[..^suffix.Length]
: value;
}

public static string ReplaceLastOccurrence(this string source, string searchedValue, string replacedValue)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using System;
using GraphQL;
using OrchardCore.ContentManagement.Utilities;

namespace OrchardCore.ContentManagement.GraphQL;
namespace System;

public static class StringExtensions
{
public static string ToFieldName(this string name)
{
return name.TrimEnd("Part").ToCamelCase();
return name.TrimEndString("Part").ToCamelCase();
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using GraphQL.Resolvers;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Linq;
using GraphQL;
using GraphQL.Resolvers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class ZoneTag
public static async ValueTask<Completion> WriteToAsync(
IReadOnlyList<FilterArgument> argumentsList,
IReadOnlyList<Statement> statements,
TextWriter _,
TextWriter _,
TextEncoder encoder,
TemplateContext context)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,8 @@ public static string ReplaceAll(this string original, IDictionary<string, string
return Regex.Replace(original, pattern, match => replacements[match.Value]);
}

#if NET8_0
[Obsolete("Don't use 'TrimEnd' as this has a different behavior in .NET 9.0. Use 'OrchardCore.ContentManagement.Utilities.TrimEndString' instead.")]
public static string TrimEnd(this string rough, string trim = "")
{
if (rough == null)
Expand All @@ -413,6 +415,7 @@ public static string TrimEnd(this string rough, string trim = "")
? rough[..^trim.Length]
: rough;
}
#endif

public static string ReplaceLastOccurrence(this string source, string find, string replace)
{
Expand Down
6 changes: 6 additions & 0 deletions src/docs/releases/2.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -739,3 +739,9 @@ Many type commonly used by modules can be `sealed`, which improves runtime perfo
### Workflow Trimming

The Workflows module now has a `Trimming` feature to automatically clean up old workflow instances. See [the corresponding documentation](../reference/modules/Workflows/README.md#trimming) for details.

### Obsoleting `TrimEnd`

The methods `public static string TrimEnd(this string value, string trim = "")` from `OrchardCore.Mvc.Utilities` and `OrchardCore.ContentManagement.Utilities` are being obsoleted and replaced by
`OrchardCore.ContentManagement.Utilities.TrimEndString(this string value, string suffix)`. This was done to prepare teh code base for the next .NET 9.0 release which has a conflicting method
sebastienros marked this conversation as resolved.
Show resolved Hide resolved
with a different behavior.