Skip to content

Commit

Permalink
Misc code cleanup and code style enforcement
Browse files Browse the repository at this point in the history
  • Loading branch information
abjerner committed Dec 5, 2022
1 parent a2f864d commit 6836c4c
Show file tree
Hide file tree
Showing 11 changed files with 21 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ public override bool ConvertControlValue(GridControl control, JToken token, out

protected virtual IGridControlValue ParseGridControlMediaValue(GridControl control, JObject json) {

GridControlMediaValue value = new GridControlMediaValue(control, json);
GridControlMediaValue value = new(control, json);

if (value.Id > 0 && _umbracoContextAccessor.TryGetUmbracoContext(out IUmbracoContext context)) {
value.PublishedImage = context.Media.GetById(value.Id);
value.PublishedImage = context.Media?.GetById(value.Id);
}

return value;
Expand Down
12 changes: 6 additions & 6 deletions src/Skybrud.Umbraco.GridData/Factories/DefaultGridFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,22 @@ public DefaultGridFactory(ILogger<DefaultGridFactory> logger, IGridConfig gridCo

/// <inheritdoc />
public virtual GridDataModel CreateGridModel(IPublishedElement owner, IPublishedPropertyType propertyType, JObject json, bool preview) {
return new(owner, propertyType, json, this);
return new GridDataModel(owner, propertyType, json, this);
}

/// <inheritdoc />
public virtual GridSection CreateGridSection(JObject json, GridDataModel grid) {
return new(json, grid, this);
return new GridSection(json, grid, this);
}

/// <inheritdoc />
public virtual GridRow CreateGridRow(JObject json, GridSection section) {
return new(json, section, this);
return new GridRow(json, section, this);
}

/// <inheritdoc />
public virtual GridArea CreateGridArea(JObject json, GridRow row) {
return new(json, row, this);
return new GridArea(json, row, this);
}

/// <inheritdoc />
Expand All @@ -63,7 +63,7 @@ public virtual GridControl CreateGridControl(JObject json, GridArea area) {
GridEditor editor = json.GetObject("editor", CreateGridEditor);

// Initialize a new Grid control
GridControl control = new GridControl(json, area);
GridControl control = new(json, area);

// Make sure to set the editor before we parse the control value
control.Editor = editor;
Expand All @@ -72,7 +72,7 @@ public virtual GridControl CreateGridControl(JObject json, GridArea area) {
control.Value = ParseGridControlValue(control);

// Get the type of the editor config (it may not have a config)
Type configType = control.Editor.Config?.GetType();
Type configType = control.Editor?.Config?.GetType();

// Determine the value type
Type valueType = null;
Expand Down
2 changes: 1 addition & 1 deletion src/Skybrud.Umbraco.GridData/Factories/IGridFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public interface IGridFactory {

/// <summary>
/// Returns a new <see cref="GridDataModel"/> from the specified <paramref name="json"/> object.
///
///
/// <paramref name="owner"/> and <paramref name="propertyType"/> may be specified if the grid value comes directly from a property value. If either aren't available, it's fine to specify <c>null</c> for both of them.
/// </summary>
/// <param name="owner">An instance of <see cref="IPublishedElement"/> representing the owner holding the grid value.</param>
Expand Down
2 changes: 1 addition & 1 deletion src/Skybrud.Umbraco.GridData/Models/GridArea.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public GridArea(JObject json, GridRow row, IGridFactory factory) : base(json) {
Grid = json.GetInt32("grid");
AllowAll = json.GetBoolean("allowAll");
Allowed = json.GetStringArray("allowed");
Controls = json.GetArray("controls", x => factory.CreateGridControl(x, this)) ?? new GridControl[0];
Controls = json.GetArray("controls", x => factory.CreateGridControl(x, this)) ?? Array.Empty<GridControl>();

// Update "PreviousControl" and "NextControl" properties
for (int i = 1; i < Controls.Count; i++) {
Expand Down
2 changes: 1 addition & 1 deletion src/Skybrud.Umbraco.GridData/Models/GridControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public class GridControl : GridJsonObject {
/// Gets whether the control and it's value is valid.
/// </summary>
[JsonIgnore]
public bool IsValid => Value != null && Value.IsValid;
public bool IsValid => Value is { IsValid: true };

#endregion

Expand Down
8 changes: 4 additions & 4 deletions src/Skybrud.Umbraco.GridData/Models/GridDataModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public bool IsValid {
public string name => Name;

/// <summary>
/// Gets the underlying JSON array for the <c>sections</c> property.
/// Gets the underlying JSON array for the <c>sections</c> property.
/// </summary>
[Obsolete]
[JsonIgnore]
Expand Down Expand Up @@ -105,7 +105,7 @@ public GridDataModel(IPublishedElement owner, IPublishedPropertyType propertyTyp
#region Member methods

/// <summary>
/// Gets an array of all nested controls.
/// Gets an array of all nested controls.
/// </summary>
public GridControl[] GetAllControls() {
return (
Expand All @@ -118,15 +118,15 @@ select control
}

/// <summary>
/// Gets an array of all nested controls with the specified editor <paramref name="alias"/>.
/// Gets an array of all nested controls with the specified editor <paramref name="alias"/>.
/// </summary>
/// <param name="alias">The editor alias of controls to be returned.</param>
public GridControl[] GetAllControls(string alias) {
return GetAllControls(x => x.Editor.Alias == alias);
}

/// <summary>
/// Gets an array of all nested controls matching the specified <paramref name="predicate"/>.
/// Gets an array of all nested controls matching the specified <paramref name="predicate"/>.
/// </summary>
/// <param name="predicate">The predicate (callback function) used for comparison.</param>
public GridControl[] GetAllControls(Func<GridControl, bool> predicate) {
Expand Down
2 changes: 1 addition & 1 deletion src/Skybrud.Umbraco.GridData/Models/GridDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ IEnumerator IEnumerable.GetEnumerator() {
public static GridDictionary Parse(JObject obj) {

// Initialize an empty dictionary
Dictionary<string, string> config = new Dictionary<string, string>();
Dictionary<string, string> config = new();

// Add all properties to the dictionary
if (obj != null) {
Expand Down
4 changes: 2 additions & 2 deletions src/Skybrud.Umbraco.GridData/Models/GridElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public abstract class GridElement : GridJsonObject {
/// <summary>
/// Gets whetehr the element has one or more config values.
/// </summary>
public bool HasConfig => Config != null && Config.Count > 0;
public bool HasConfig => Config is { Count: > 0 };

/// <summary>
/// Gets a dictionary representing the styles of the element.
Expand All @@ -30,7 +30,7 @@ public abstract class GridElement : GridJsonObject {
/// <summary>
/// Gets whetehr the element has one or more style values.
/// </summary>
public bool HasStyles => Styles != null && Styles.Count > 0;
public bool HasStyles => Styles is { Count: > 0 };

/// <summary>
/// Gets whether at least one control within the element is valid.
Expand Down
2 changes: 1 addition & 1 deletion src/Skybrud.Umbraco.GridData/Models/GridRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public GridRow(JObject json, GridSection section, IGridFactory factory) : base(j
Label = json.GetString("label");
Name = json.GetString("name");

Areas = json.GetArray("areas", x => factory.CreateGridArea(x, this)) ?? new GridArea[0];
Areas = json.GetArray("areas", x => factory.CreateGridArea(x, this)) ?? Array.Empty<GridArea>();

// Update "PreviousArea" and "NextArea" properties
for (int i = 1; i < Areas.Count; i++) {
Expand Down
2 changes: 1 addition & 1 deletion src/Skybrud.Umbraco.GridData/Models/GridSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public GridSection(JObject json, GridDataModel grid, IGridFactory factory) : bas
Model = grid;
Grid = json.GetInt32("grid");
Name = grid.Name;
Rows = json.GetArray("rows", x => factory.CreateGridRow(x, this)) ?? new GridRow[0];
Rows = json.GetArray("rows", x => factory.CreateGridRow(x, this)) ?? Array.Empty<GridRow>();

// Update "PreviousRow" and "NextRow" properties
for (int i = 1; i < Rows.Count; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class GridControlMacroValue : GridControlValueBase {
public GridControlMacroValue(GridControl control, JObject obj) : base(control, obj) {
Syntax = obj.GetString("syntax");
MacroAlias = obj.GetString("macroAlias");
Parameters = obj.GetObject("macroParamsDictionary").ToObject<Dictionary<string, object>>();
Parameters = obj.GetObject("macroParamsDictionary")?.ToObject<Dictionary<string, object>>();
}

#endregion
Expand Down

0 comments on commit 6836c4c

Please sign in to comment.