Skip to content

Commit

Permalink
Minimize breaking changes in API
Browse files Browse the repository at this point in the history
  • Loading branch information
ronaldbarendse committed Jan 25, 2022
1 parent ad7324c commit 66f843d
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 19 deletions.
50 changes: 49 additions & 1 deletion src/ImageSharp.Web/Commands/CommandCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace SixLabors.ImageSharp.Web.Commands
/// <summary>
/// Represents an ordered collection of processing commands.
/// </summary>
public sealed class CommandCollection : KeyedCollection<string, KeyValuePair<string, string>>
public sealed class CommandCollection : KeyedCollection<string, KeyValuePair<string, string>>, IDictionary<string, string>
{
/// <summary>
/// Initializes a new instance of the <see cref="CommandCollection"/> class.
Expand All @@ -25,6 +25,25 @@ private CommandCollection(IEqualityComparer<string> comparer)
{
}

/// <inheritdoc/>
ICollection<string> IDictionary<string, string>.Keys => new List<string>(this.Keys);

/// <inheritdoc/>
ICollection<string> IDictionary<string, string>.Values
{
get
{
ICollection<string> values = new List<string>();

foreach (KeyValuePair<string, string> item in this)
{
values.Add(item.Value);
}

return values;
}
}

/// <summary>
/// Gets an <see cref="IEnumerable{String}"/> representing the keys of the collection.
/// </summary>
Expand Down Expand Up @@ -75,7 +94,36 @@ public IEnumerable<string> Keys
}
}

/// <inheritdoc/>
string IDictionary<string, string>.this[string key]
{
get => this[key];
set => this[key] = value;
}

/// <inheritdoc/>
protected override string GetKeyForItem(KeyValuePair<string, string> item) => item.Key;

/// <inheritdoc/>
public void Add(string key, string value) => this.Add(new(key, value));

/// <inheritdoc/>
bool IDictionary<string, string>.ContainsKey(string key) => this.Contains(key);

/// <inheritdoc/>
bool IDictionary<string, string>.Remove(string key) => this.Remove(key);

/// <inheritdoc/>
bool IDictionary<string, string>.TryGetValue(string key, out string value)
{
if (this.TryGetValue(key, out KeyValuePair<string, string> keyValue))
{
value = keyValue.Value;
return true;
}

value = default;
return false;
}
}
}
12 changes: 10 additions & 2 deletions src/ImageSharp.Web/Middleware/ImageCommandContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0.

using System;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.AspNetCore.Http;
using SixLabors.ImageSharp.Web.Commands;
Expand All @@ -26,7 +28,7 @@ public ImageCommandContext(
CultureInfo culture)
{
this.Context = context;
this.Commands = commands;
this.OrderedCommands = commands;
this.Parser = parser;
this.Culture = culture;
}
Expand All @@ -39,7 +41,13 @@ public ImageCommandContext(
/// <summary>
/// Gets the collection of URI derived processing commands.
/// </summary>
public CommandCollection Commands { get; }
[Obsolete("Use OrderedCommands. This will be removed in a future version.")]
public IDictionary<string, string> Commands => this.OrderedCommands;

/// <summary>
/// Gets the collection of URI derived processing commands.
/// </summary>
public CommandCollection OrderedCommands { get; }

/// <summary>
/// Gets the command parser for parsing URI derived processing commands.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private static readonly IEnumerable<string> ColorCommands
public FormattedImage Process(
FormattedImage image,
ILogger logger,
CommandCollection commands,
IDictionary<string, string> commands,
CommandParser parser,
CultureInfo culture)
{
Expand Down
2 changes: 1 addition & 1 deletion src/ImageSharp.Web/Processors/FormatWebProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public FormatWebProcessor(IOptions<ImageSharpMiddlewareOptions> options)
public FormattedImage Process(
FormattedImage image,
ILogger logger,
CommandCollection commands,
IDictionary<string, string> commands,
CommandParser parser,
CultureInfo culture)
{
Expand Down
2 changes: 1 addition & 1 deletion src/ImageSharp.Web/Processors/IImageWebProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public interface IImageWebProcessor
FormattedImage Process(
FormattedImage image,
ILogger logger,
CommandCollection commands,
IDictionary<string, string> commands,
CommandParser parser,
CultureInfo culture);
}
Expand Down
4 changes: 2 additions & 2 deletions src/ImageSharp.Web/Processors/QualityWebProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ private static readonly IEnumerable<string> QualityCommands
public FormattedImage Process(
FormattedImage image,
ILogger logger,
CommandCollection commands,
IDictionary<string, string> commands,
CommandParser parser,
CultureInfo culture)
{
if (commands.Contains(Quality))
if (commands.ContainsKey(Quality))
{
// The encoders clamp any values so no validation is required.
int quality = parser.ParseValue<int>(commands.GetValueOrDefault(Quality), culture);
Expand Down
18 changes: 9 additions & 9 deletions src/ImageSharp.Web/Processors/ResizeWebProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private static readonly IEnumerable<string> ResizeCommands
public FormattedImage Process(
FormattedImage image,
ILogger logger,
CommandCollection commands,
IDictionary<string, string> commands,
CommandParser parser,
CultureInfo culture)
{
Expand All @@ -84,11 +84,11 @@ public FormattedImage Process(
}

private static ResizeOptions GetResizeOptions(
CommandCollection commands,
IDictionary<string, string> commands,
CommandParser parser,
CultureInfo culture)
{
if (!commands.Contains(Width) && !commands.Contains(Height))
if (!commands.ContainsKey(Width) && !commands.ContainsKey(Height))
{
return null;
}
Expand Down Expand Up @@ -116,7 +116,7 @@ private static ResizeOptions GetResizeOptions(
}

private static Size ParseSize(
CommandCollection commands,
IDictionary<string, string> commands,
CommandParser parser,
CultureInfo culture)
{
Expand All @@ -128,7 +128,7 @@ private static Size ParseSize(
}

private static PointF? GetCenter(
CommandCollection commands,
IDictionary<string, string> commands,
CommandParser parser,
CultureInfo culture)
{
Expand All @@ -143,24 +143,24 @@ private static Size ParseSize(
}

private static ResizeMode GetMode(
CommandCollection commands,
IDictionary<string, string> commands,
CommandParser parser,
CultureInfo culture)
=> parser.ParseValue<ResizeMode>(commands.GetValueOrDefault(Mode), culture);

private static AnchorPositionMode GetAnchor(
CommandCollection commands,
IDictionary<string, string> commands,
CommandParser parser,
CultureInfo culture)
=> parser.ParseValue<AnchorPositionMode>(commands.GetValueOrDefault(Anchor), culture);

private static bool GetCompandMode(
CommandCollection commands,
IDictionary<string, string> commands,
CommandParser parser,
CultureInfo culture)
=> parser.ParseValue<bool>(commands.GetValueOrDefault(Compand), culture);

private static IResampler GetSampler(CommandCollection commands)
private static IResampler GetSampler(IDictionary<string, string> commands)
{
string sampler = commands.GetValueOrDefault(Sampler);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class MockWebProcessor : IImageWebProcessor
public FormattedImage Process(
FormattedImage image,
ILogger logger,
CommandCollection commands,
IDictionary<string, string> commands,
CommandParser parser,
CultureInfo culture)
=> image;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class CacheBusterWebProcessor : IImageWebProcessor
public FormattedImage Process(
FormattedImage image,
ILogger logger,
CommandCollection commands,
IDictionary<string, string> commands,
CommandParser parser,
CultureInfo culture)
=> image;
Expand Down

0 comments on commit 66f843d

Please sign in to comment.