Skip to content

Commit

Permalink
Add options to copy and reformat values
Browse files Browse the repository at this point in the history
  • Loading branch information
cezarypiatek committed Jul 15, 2023
1 parent 743f950 commit 78028ef
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 28 deletions.
1 change: 1 addition & 0 deletions src/WireMockInspector/Styles.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<StreamGeometry x:Key="missing_metadata_regular">M19.7501,2 C20.9927,2 22.0001,3.00736 22.0001,4.25 L22.0001,9.71196 C22.0001,10.5738 21.6578,11.4003 21.0484,12.0098 L21.0222,12.0361 C20.5797,11.7503 20.1003,11.5167 19.5928,11.3442 L19.9876,10.9492 C20.3157,10.6211 20.5001,10.176 20.5001,9.71196 L20.5001,4.25 C20.5001,3.83579 20.1643,3.5 19.7501,3.5 L14.2847,3.5 C13.8202,3.5 13.3748,3.68467 13.0465,4.01333 L4.53436,12.5358 C3.86414,13.2207 3.86923,14.3191 4.54908,14.9977 L9.0103,19.4522 C9.64819,20.0877 10.6535,20.1309 11.3408,19.5826 C11.5052,20.0689 11.7256,20.5295 11.9943,20.9567 C10.7373,21.7569 9.05064,21.6098 7.95104,20.5143 L3.48934,16.0592 C2.21887,14.7913 2.21724,12.7334 3.48556,11.4632 L11.9852,2.95334 C12.5948,2.34297 13.4221,2 14.2847,2 L19.7501,2 Z M17,5.50218 C17.8284,5.50218 18.5,6.17374 18.5,7.00216 C18.5,7.83057 17.8284,8.50213 17,8.50213 C16.1716,8.50213 15.5001,7.83057 15.5001,7.00216 C15.5001,6.17374 16.1716,5.50218 17,5.50218 Z M23,17.5 C23,14.4624 20.5376,12 17.5,12 C14.4624,12 12,14.4624 12,17.5 C12,20.5376 14.4624,23 17.5,23 C20.5376,23 23,20.5376 23,17.5 Z M16.8755,20.5045 C16.8755,20.1596 17.1551,19.88 17.5,19.88 C17.8449,19.88 18.1245,20.1596 18.1245,20.5045 C18.1245,20.8494 17.8449,21.129 17.5,21.129 C17.1551,21.129 16.8755,20.8494 16.8755,20.5045 Z M15.6467,15.9574 C15.6357,14.8205 16.4521,14.0031 17.5,14.0031 C18.5311,14.0031 19.3534,14.8489 19.3534,15.9526 C19.3534,16.5186 19.1682,16.866 18.6905,17.4003 L18.4247,17.6908 L18.3238,17.8063 C18.0765,18.0981 18,18.2684 18,18.5006 C18,18.7767 17.7762,19.0006 17.5,19.0006 C17.2239,19.0006 17,18.7767 17,18.5006 C17,17.9255 17.1868,17.5749 17.6711,17.0333 L17.9365,16.7432 L18.0355,16.63 C18.2782,16.3437 18.3534,16.1769 18.3534,15.9526 C18.3534,15.395 17.9724,15.0031 17.5,15.0031 C17.0063,15.0031 16.6411,15.3688 16.6465901,15.9478 C16.6493,16.2239 16.4276,16.4499 16.1514,16.4526 C15.8753,16.4552 15.6493,16.2335 15.6467,15.9574 Z</StreamGeometry>
<StreamGeometry x:Key="search_regular">M11.5,2.75 C16.3324916,2.75 20.25,6.66750844 20.25,11.5 C20.25,13.6461673 19.4773285,15.6118676 18.1949905,17.1340957 L25.0303301,23.9696699 C25.3232233,24.2625631 25.3232233,24.7374369 25.0303301,25.0303301 C24.7640635,25.2965966 24.3473998,25.3208027 24.0537883,25.1029482 L23.9696699,25.0303301 L17.1340957,18.1949905 C15.6118676,19.4773285 13.6461673,20.25 11.5,20.25 C6.66750844,20.25 2.75,16.3324916 2.75,11.5 C2.75,6.66750844 6.66750844,2.75 11.5,2.75 Z M11.5,4.25 C7.49593556,4.25 4.25,7.49593556 4.25,11.5 C4.25,15.5040644 7.49593556,18.75 11.5,18.75 C15.5040644,18.75 18.75,15.5040644 18.75,11.5 C18.75,7.49593556 15.5040644,4.25 11.5,4.25 Z</StreamGeometry>
<StreamGeometry x:Key="question_circle_regular">M24 4C35.0457 4 44 12.9543 44 24C44 35.0457 35.0457 44 24 44C12.9543 44 4 35.0457 4 24C4 12.9543 12.9543 4 24 4ZM24 6.5C14.335 6.5 6.5 14.335 6.5 24C6.5 33.665 14.335 41.5 24 41.5C33.665 41.5 41.5 33.665 41.5 24C41.5 14.335 33.665 6.5 24 6.5ZM24.25 32C25.0784 32 25.75 32.6716 25.75 33.5C25.75 34.3284 25.0784 35 24.25 35C23.4216 35 22.75 34.3284 22.75 33.5C22.75 32.6716 23.4216 32 24.25 32ZM24.25 13C27.6147 13 30.5 15.8821 30.5 19.2488C30.502 21.3691 29.7314 22.7192 27.8216 24.7772L26.8066 25.8638C25.7842 27.0028 25.3794 27.7252 25.3409 28.5793L25.3379 28.7411L25.3323 28.8689L25.3143 28.9932C25.2018 29.5636 24.7009 29.9957 24.0968 30.0001C23.4065 30.0049 22.8428 29.4493 22.8379 28.7589C22.8251 26.9703 23.5147 25.7467 25.1461 23.9739L26.1734 22.8762C27.5312 21.3837 28.0012 20.503 28 19.25C28 17.2634 26.2346 15.5 24.25 15.5C22.3307 15.5 20.6142 17.1536 20.5055 19.0587L20.4935 19.3778C20.4295 20.0081 19.8972 20.5 19.25 20.5C18.5596 20.5 18 19.9404 18 19.25C18 15.8846 20.8864 13 24.25 13Z</StreamGeometry>
<StreamGeometry x:Key="settings_regular">M14 9.50006C11.5147 9.50006 9.5 11.5148 9.5 14.0001C9.5 16.4853 11.5147 18.5001 14 18.5001C15.3488 18.5001 16.559 17.9066 17.3838 16.9666C18.0787 16.1746 18.5 15.1365 18.5 14.0001C18.5 13.5401 18.431 13.0963 18.3028 12.6784C17.7382 10.8381 16.0253 9.50006 14 9.50006ZM11 14.0001C11 12.3432 12.3431 11.0001 14 11.0001C15.6569 11.0001 17 12.3432 17 14.0001C17 15.6569 15.6569 17.0001 14 17.0001C12.3431 17.0001 11 15.6569 11 14.0001Z M21.7093 22.3948L19.9818 21.6364C19.4876 21.4197 18.9071 21.4515 18.44 21.7219C17.9729 21.9924 17.675 22.4693 17.6157 23.0066L17.408 24.8855C17.3651 25.273 17.084 25.5917 16.7055 25.682C14.9263 26.1061 13.0725 26.1061 11.2933 25.682C10.9148 25.5917 10.6336 25.273 10.5908 24.8855L10.3834 23.0093C10.3225 22.4731 10.0112 21.9976 9.54452 21.7281C9.07783 21.4586 8.51117 21.4269 8.01859 21.6424L6.29071 22.4009C5.93281 22.558 5.51493 22.4718 5.24806 22.1859C4.00474 20.8536 3.07924 19.2561 2.54122 17.5137C2.42533 17.1384 2.55922 16.7307 2.8749 16.4977L4.40219 15.3703C4.83721 15.0501 5.09414 14.5415 5.09414 14.0007C5.09414 13.4598 4.83721 12.9512 4.40162 12.6306L2.87529 11.5051C2.55914 11.272 2.42513 10.8638 2.54142 10.4882C3.08038 8.74734 4.00637 7.15163 5.24971 5.82114C5.51684 5.53528 5.93492 5.44941 6.29276 5.60691L8.01296 6.36404C8.50793 6.58168 9.07696 6.54881 9.54617 6.27415C10.0133 6.00264 10.3244 5.52527 10.3844 4.98794L10.5933 3.11017C10.637 2.71803 10.9245 2.39704 11.3089 2.31138C12.19 2.11504 13.0891 2.01071 14.0131 2.00006C14.9147 2.01047 15.8128 2.11485 16.6928 2.31149C17.077 2.39734 17.3643 2.71823 17.4079 3.11017L17.617 4.98937C17.7116 5.85221 18.4387 6.50572 19.3055 6.50663C19.5385 6.507 19.769 6.45838 19.9843 6.36294L21.7048 5.60568C22.0626 5.44818 22.4807 5.53405 22.7478 5.81991C23.9912 7.1504 24.9172 8.74611 25.4561 10.487C25.5723 10.8623 25.4386 11.2703 25.1228 11.5035L23.5978 12.6297C23.1628 12.95 22.9 13.4586 22.9 13.9994C22.9 14.5403 23.1628 15.0489 23.5988 15.3698L25.1251 16.4965C25.441 16.7296 25.5748 17.1376 25.4586 17.5131C24.9198 19.2536 23.9944 20.8492 22.7517 22.1799C22.4849 22.4657 22.0671 22.5518 21.7093 22.3948ZM16.263 22.1966C16.4982 21.4685 16.9889 20.8288 17.6884 20.4238C18.5702 19.9132 19.6536 19.8547 20.5841 20.2627L21.9281 20.8526C22.791 19.8538 23.4593 18.7013 23.8981 17.4552L22.7095 16.5778L22.7086 16.5771C21.898 15.98 21.4 15.0277 21.4 13.9994C21.4 12.9719 21.8974 12.0195 22.7073 11.4227L22.7085 11.4218L23.8957 10.545C23.4567 9.2988 22.7881 8.14636 21.9248 7.1477L20.5922 7.73425L20.5899 7.73527C20.1844 7.91463 19.7472 8.00722 19.3039 8.00663C17.6715 8.00453 16.3046 6.77431 16.1261 5.15465L16.1259 5.15291L15.9635 3.69304C15.3202 3.57328 14.6677 3.50872 14.013 3.50017C13.3389 3.50891 12.6821 3.57367 12.0377 3.69328L11.8751 5.15452C11.7625 6.16272 11.1793 7.05909 10.3019 7.56986C9.41937 8.0856 8.34453 8.14844 7.40869 7.73694L6.07273 7.14893C5.20949 8.14751 4.54092 9.29983 4.10196 10.5459L5.29181 11.4233C6.11115 12.0269 6.59414 12.9837 6.59414 14.0007C6.59414 15.0173 6.11142 15.9742 5.29237 16.5776L4.10161 17.4566C4.54002 18.7044 5.2085 19.8585 6.07205 20.8587L7.41742 20.2682C8.34745 19.8613 9.41573 19.9215 10.2947 20.4292C11.174 20.937 11.7593 21.832 11.8738 22.84L11.8744 22.8445L12.0362 24.3088C13.3326 24.5638 14.6662 24.5638 15.9626 24.3088L16.1247 22.8418C16.1491 22.6217 16.1955 22.4055 16.263 22.1966Z</StreamGeometry>
</Style.Resources>
</Style>
<Style Selector="TabControl.minor > TabItem">
Expand Down
42 changes: 21 additions & 21 deletions src/WireMockInspector/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public MainWindowViewModel()
Title = model.Title,
Description = model.Title != model.Description? model.Description: null,
UpdatedOn = model.UpdatedAt,
Content = AsMarkdownCode("json", JsonConvert.SerializeObject(model, Formatting.Indented)),
Content = AsMarkdownCode("json", JsonConvert.SerializeObject(model, Formatting.Indented)).AsMarkdownSyntax(),
PartialHitCount = partialHitCount,
PerfectHitCount = perfectHitCount,
HitType = (perfectHitCount, partialHitCount) switch
Expand Down Expand Up @@ -284,7 +284,7 @@ await _settingsManager.UpdateSettings(settings =>
.ObserveOn(RxApp.MainThreadScheduler)
.Subscribe(code =>
{
SelectedMapping.Code = AsMarkdownCode("cs", code);
SelectedMapping.Code = AsMarkdownCode("cs", code).AsMarkdownSyntax();
});
}

Expand Down Expand Up @@ -487,9 +487,9 @@ private static MappingDetails GetMappingDetails(RequestViewModel req, MappingMod
{
"String" or "FormUrlEncoded" => WrapBodyInMarkdown(req.Raw.Request.Body?? string.Empty),
"Json" => AsMarkdownCode("json", req.Raw.Request.BodyAsJson?.ToString() ?? string.Empty),
"Bytes" => req.Raw.Request.BodyAsBytes?.ToString()?? string.Empty,
"File" => "[FileContent]",
_ => ""
"Bytes" => new Markdown("plaintext", req.Raw.Request.BodyAsBytes?.ToString()?? string.Empty),
"File" => new Markdown("plaintext","[FileContent]"),
_ => new Markdown("plaintext", "")
}
},
Expectations = ExpectationsAsJson(expectations.Request?.Body),
Expand All @@ -506,7 +506,7 @@ private static MappingDetails GetMappingDetails(RequestViewModel req, MappingMod
{
Value = req.Raw.Response?.StatusCode?.ToString()?? string.Empty
},
Expectations = expectations.Response?.StatusCode?.ToString()?? string.Empty
Expectations = ExpectationsAsJson(expectations.Response?.StatusCode?.ToString())
},
new ()
{
Expand All @@ -527,42 +527,42 @@ private static MappingDetails GetMappingDetails(RequestViewModel req, MappingMod
Value = req.Raw.Response?.DetectedBodyType.ToString() switch
{
"String" or "FormUrlEncoded" => WrapBodyInMarkdown( req.Raw.Response.Body?? string.Empty),
"Json" => AsMarkdownCode("json",req.Raw.Response.BodyAsJson?.ToString() ?? string.Empty),
"Bytes" => req.Raw.Response.BodyAsBytes?.ToString()?? string.Empty,
"File" => "[FileContent]",
_ => ""
"Json" => new Markdown("json",req.Raw.Response.BodyAsJson?.ToString() ?? string.Empty),
"Bytes" => new Markdown("plaintext", req.Raw.Response.BodyAsBytes?.ToString()?? string.Empty),
"File" => new Markdown("plaintext","[FileContent]"),
_ => new Markdown("plaintext","[FileContent]"),
}
},
Expectations = expectations.Response switch
{
{Body: {} bodyResponse} => WrapBodyInMarkdown(bodyResponse),
{BodyAsJson: {} bodyAsJson} => AsMarkdownCode("json", bodyAsJson.ToString()!),
{BodyAsBytes: {} bodyAsBytes} => bodyAsBytes.ToString()?? string.Empty,
{BodyAsFile: {} bodyAsFile} => bodyAsFile,
_ => ""
{BodyAsJson: {} bodyAsJson} => new Markdown("json", bodyAsJson.ToString()!),
{BodyAsBytes: {} bodyAsBytes} => new Markdown("plaintext", bodyAsBytes.ToString()?? string.Empty),
{BodyAsFile: {} bodyAsFile} => new Markdown("plaintext",bodyAsFile),
_ => new Markdown("plaintext","[FileContent]")
}
}
}
};
}

private static string WrapBodyInMarkdown(string bodyResponse)
private static Markdown WrapBodyInMarkdown(string bodyResponse)
{
var cleanBody = bodyResponse.Trim();
if (cleanBody.StartsWith("[") || cleanBody.StartsWith("{"))
{
return AsMarkdownCode("json", bodyResponse);
return new Markdown("json", bodyResponse);

}
if (cleanBody.StartsWith("<"))
{
return AsMarkdownCode("xml", bodyResponse);
return new Markdown("xml", bodyResponse);

}
return bodyResponse;
return new Markdown("plaintext", bodyResponse);
}

private static string AsMarkdownCode(string lang, string code) => $"```{lang}\r\n{code}\r\n```";
public static Markdown AsMarkdownCode(string lang, string code) => new Markdown(lang, code);

public string RequestSearchTerm
{
Expand All @@ -574,11 +574,11 @@ public string RequestSearchTerm



private static string ExpectationsAsJson(object? data)
private static Markdown ExpectationsAsJson(object? data)
{
if (data == null)
{
return string.Empty;
return new Markdown("plaintext", string.Empty);
}

return AsMarkdownCode("json", JsonConvert.SerializeObject(data, Formatting.Indented));
Expand Down
120 changes: 116 additions & 4 deletions src/WireMockInspector/ViewModels/MatchDetailsViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,115 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Reactive.Linq;
using System.Windows.Input;
using Avalonia;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using ReactiveUI;

namespace WireMockInspector.ViewModels;

public class MatchDetailsViewModel:ViewModelBase
{
private ActualValue _actualValue;
private Markdown _expectations;
public string RuleName { get; set; }
public bool? Matched { get; set; }
public bool NoExpectations { get; set; }

public ActualValue ActualValue { get; set; }
public string Expectations { get; set; }
public ActualValue ActualValue
{
get => _actualValue;
set => this.RaiseAndSetIfChanged(ref _actualValue, value);
}



public Markdown Expectations
{
get => _expectations;
set => this.RaiseAndSetIfChanged(ref _expectations, value);
}

public ICommand ReformatActualValue { get; set; }
public ICommand CopyActualValue { get; set; }

public ICommand ReformatExpectations{ get; set; }
public ICommand CopyExpectations{ get; set; }

public MatchDetailsViewModel()
{
CopyActualValue = ReactiveCommand.Create(async () =>
{
if (ActualValue is MarkdownActualValue {Value: {rawValue: var raw}})
{
await Application.Current!.Clipboard.SetTextAsync(raw);
}
else if (ActualValue is SimpleActualValue {Value: var simpleValue} )
{
await Application.Current!.Clipboard.SetTextAsync(simpleValue);
}
else if(ActualValue is KeyValueListActualValue{SelectedActualValueGridItem: {} selectedRow})
{
await Application.Current!.Clipboard.SetTextAsync($"{selectedRow.Key}:{selectedRow.Value}");
}
});

CopyExpectations = ReactiveCommand.Create(async () =>
{
if (Expectations is Markdown{rawValue: var value})
{
await Application.Current!.Clipboard.SetTextAsync(value);
}
});

ReformatActualValue = ReactiveCommand.Create(() =>
{
if (this.ActualValue is MarkdownActualValue {Value: {} rawValue} markdown)
{
ActualValue = new MarkdownActualValue()
{
Value = TryToReformat(rawValue)
};
}
},
this.WhenAnyValue(x=>x.ActualValue).Select(x =>
{
return x is MarkdownActualValue {Value: { } va} && IsJsonMarkdown(va);
}));
ReformatExpectations= ReactiveCommand.Create(() =>
{
if (string.IsNullOrWhiteSpace(Expectations.rawValue) == false)
{
Expectations = TryToReformat(Expectations);
}
}, this.WhenAnyValue(x=>x.Expectations).Select(IsJsonMarkdown));
}

private static Markdown TryToReformat(Markdown markdown)
{
if (IsJsonMarkdown(markdown))
{

try
{

var formatted = JToken.Parse(markdown.rawValue).ToString(Formatting.Indented);
return MainWindowViewModel.AsMarkdownCode("json", formatted);
}
catch (Exception e)
{

}
}

return markdown;
}

private static bool IsJsonMarkdown(Markdown rawValue)
{
return rawValue?.lang == "json";
}
}


Expand All @@ -25,11 +125,23 @@ public class SimpleActualValue:ActualValue

public class MarkdownActualValue:ActualValue
{
public string Value { get; set; }
public Markdown Value { get; set; }
public string MarkdownValue { get; set; }
}

public record Markdown(string lang, string rawValue)
{
public string AsMarkdownSyntax()
{
return $"```{lang}\r\n{rawValue}\r\n```";
}

public override string ToString() => AsMarkdownSyntax();
};

public class KeyValueListActualValue:ActualValue
{
public KeyValuePair<string,string> SelectedActualValueGridItem { get; set; }
public IReadOnlyList<KeyValuePair<string,string>> Items { get; set; }
}

Expand Down
Loading

0 comments on commit 78028ef

Please sign in to comment.