Skip to content

Refactor settings arrays to ISet<T> (to ensure unique values and make them easily mutable)#16058

Merged
bergmania merged 3 commits intov16/devfrom
v14/feature/refactor-settings
Dec 2, 2024
Merged

Refactor settings arrays to ISet<T> (to ensure unique values and make them easily mutable)#16058
bergmania merged 3 commits intov16/devfrom
v14/feature/refactor-settings

Conversation

@ronaldbarendse
Copy link
Contributor

Prerequisites

  • I have added steps to test this contribution in the description below

Description

Most configuration/options classes currently bind certain settings to arrays, which is mostly fine when configuring them from the appsettings.json files, but make adding new or removing default items in code a lot harder.

Consider removing TIFF and adding PBM and TGA image file types on the ContentImagingSettings configuration. Even when using the spread element .. to make adding items to the array easier, removing an existing item requires filtering the existing array:

using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Core.Configuration.Models;

public class ImageFileTypesComposer : IComposer
{
    public void Compose(IUmbracoBuilder builder)
        => builder.Services.Configure<ContentImagingSettings>(options =>
        {
            options.ImageFileTypes =
            [
                // Remove TIFF
                ..options.ImageFileTypes.Where(x => x == "tiff"),
                // Add PBM and TGA
                "pbm",
                "tga"
            ];
        });
}

With this PR applied, the ImageFileTypes is now an ISet<string>, which can easily be mutated:

using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Core.Configuration.Models;

public class ImageFileTypesComposer : IComposer
{
    public void Compose(IUmbracoBuilder builder)
        => builder.Services.Configure<ContentImagingSettings>(options =>
        {
            // Remove TIFF
            options.ImageFileTypes.Remove("tiff");

            // Add PBM and TGA
            options.ImageFileTypes.Add("pbm");
            options.ImageFileTypes.Add("tga");
        });
}

As a bonus, this won't add duplicate items to the set 🙌🏻 Additional improvements can be made by refactoring some settings to dictionaries, so they can be configured by unique key (e.g. RTE commands by alias, which would fix issue #14405). Other settings might benefit from being refactored as well, like the reserved paths and URLs (which would fix issue #12965).

@ronaldbarendse ronaldbarendse changed the base branch from v14/dev to v15/dev September 9, 2024 14:45
@ronaldbarendse ronaldbarendse changed the title v14: Refactor settings arrays to ISet<T> (to ensure unique values and make them easily mutable) Refactor settings arrays to ISet<T> (to ensure unique values and make them easily mutable) Sep 9, 2024
@ronaldbarendse ronaldbarendse added category/breaking status/needs-docs Requires new or updated documentation labels Sep 9, 2024
@bergmania
Copy link
Member

I guess we should announce this change, and wait with it for v16, even that it is super nice. It is a breaking change, with no much notice.

@bergmania
Copy link
Member

I created the following announcement
umbraco/Announcements#21

@bergmania bergmania changed the base branch from v15/dev to v16/dev December 2, 2024 14:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

category/breaking release/16.0.0 status/needs-docs Requires new or updated documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments