Skip to content

Commit

Permalink
feat: improve bool editor
Browse files Browse the repository at this point in the history
  • Loading branch information
rdavisau committed Apr 25, 2019
1 parent c73332b commit c762dc7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/DumpEditable/EditableDumpContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ public partial class EditableDumpContainer<T>
new List<EditorRule>
{
EditorRule.ForEnums(),
EditorRule.ForBool(),
EditorRule.ForTypeWithStringBasedEditor<int>(int.TryParse),
EditorRule.ForTypeWithStringBasedEditor<uint>(uint.TryParse),
EditorRule.ForTypeWithStringBasedEditor<short>(short.TryParse),
Expand All @@ -155,7 +156,6 @@ public partial class EditableDumpContainer<T>
output = input;
return true;
}),
EditorRule.ForTypeWithStringBasedEditor<bool>(bool.TryParse),
EditorRule.ForTypeWithStringBasedEditor<byte>(byte.TryParse),
EditorRule.ForTypeWithStringBasedEditor<sbyte>(sbyte.TryParse),
EditorRule.ForTypeWithStringBasedEditor<char>(char.TryParse),
Expand Down
19 changes: 17 additions & 2 deletions src/DumpEditable/EditorRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ public static EditorRule ForType<T>(Func<object, PropertyInfo, Action, object> g
.Concat(new [] { "]" }))
);

public static EditorRule ForBool() =>
EditorRule.For(
(_, p) => p.PropertyType == typeof(bool) || p.PropertyType == typeof(bool?),
(o, p, c) =>
Util.HorizontalRun(true,
Enumerable.Concat(
new object[] { p.GetValue(o) ?? NullString, "[" },
new bool?[] { true, false, null }
.Where(b => p.PropertyType == typeof(bool?) || b != null)
.Select(v => new Hyperlinq(() => { SetValue(o, p, v); c(); }, $"{(object)v ?? NullString }")))
.Concat(new[] { "]" }))
);

public static EditorRule ForTypeWithStringBasedEditor<T>(ParseFunc<string, T, bool> parseFunc, bool supportNullable = true, bool supportEnumerable = true)
=> new EditorRule
{
Expand All @@ -67,7 +80,7 @@ public static EditorRule ForTypeWithStringBasedEditor<T>(ParseFunc<string, T, bo
isEnumerable = false;

var desc = currVal == null
? "null"
? NullString
: (isEnumerable ? JsonConvert.SerializeObject(currVal) : $"{currVal}");

var change = new Hyperlinq(() =>
Expand All @@ -88,7 +101,7 @@ public static EditorRule ForTypeWithStringBasedEditor<T>(ParseFunc<string, T, bo
return; // can't deserialise
}
}
if (canConvert)
else if (canConvert)
{
SetValue(o,p,output);
}
Expand All @@ -115,5 +128,7 @@ public static void SetValue(object o, PropertyInfo p, object v)
}

public delegate V ParseFunc<T, U, V>(T input, out U output);

private const string NullString = "(null)";
}
}

0 comments on commit c762dc7

Please sign in to comment.