public void Insert_array_elements() { var sample = PatchTests.GetSample2(); var patchDocument = new PatchDocument(); var pointer = new JsonPointer("/books/0"); patchDocument.AddOperation(new AddEachOperation() { Path = pointer, Value = new JArray() { new JObject(new[] { new JProperty("author", "James Brown") }), new JObject(new[] { new JProperty("cat", "Garfield") }), new JObject(new[] { new JProperty("producer", "Kingston") }), } }); patchDocument.ApplyTo(sample); var list = sample["books"] as JArray; Assert.Equal(5, list.Count); Assert.Equal(list[0]["author"].ToString(), "James Brown"); Assert.Equal(list[1]["cat"].ToString(), "Garfield"); Assert.Equal(list[2]["producer"].ToString(), "Kingston"); }
public void CreateEmptyPatch() { var sample = GetSample2(); var sampletext = sample.ToString(); var patchDocument = new PatchDocument(); patchDocument.ApplyTo(new JsonNetTargetAdapter(sample)); Assert.Equal(sampletext, sample.ToString()); }
public void Remove_a_property() { var sample = PatchTests.GetSample2(); var patchDocument = new PatchDocument(); var pointer = new JsonPointer("/books/0/author"); patchDocument.AddOperation(new RemoveOperation() { Path = pointer }); patchDocument.ApplyTo(sample); Assert.Throws(typeof(ArgumentException), () => { pointer.Find(sample); }); }
public void Replace_a_property_value_with_a_new_value() { var sample = PatchTests.GetSample2(); var patchDocument = new PatchDocument(); var pointer = new JsonPointer("/books/0/author"); patchDocument.AddOperation(new ReplaceOperation() { Path = pointer, Value = new JValue("Bob Brown") }); patchDocument.ApplyTo(new JsonNetTargetAdapter(sample)); Assert.Equal("Bob Brown", (string)pointer.Find(sample)); }
public void Test_a_value() { var sample = PatchTests.GetSample2(); var patchDocument = new PatchDocument(); var pointer = new JsonPointer("/books/0/author"); patchDocument.AddOperation(new TestOperation() { Path = pointer, Value = new JValue("Billy Burton") }); Assert.Throws(typeof(InvalidOperationException), () => { patchDocument.ApplyTo(sample); }); }
public void Append_array_elements_wrongDataType() { var sample = PatchTests.GetSample2(); var patchDocument = new PatchDocument(); var pointer = new JsonPointer("/books/"); patchDocument.AddOperation(new AddEachOperation() { Path = pointer, Value = new JObject(new[] { new JProperty("producer", "Kingston") }) }); Assert.Throws(typeof(ArgumentException), () => { patchDocument.ApplyTo(sample); }); }
public void Replace_a_property_value_with_an_object() { var sample = PatchTests.GetSample2(); var patchDocument = new PatchDocument(); var pointer = new JsonPointer("/books/0/author"); patchDocument.AddOperation(new ReplaceOperation() { Path = pointer, Value = new JObject(new[] { new JProperty("hello", "world") }) }); patchDocument.ApplyTo(new JsonNetTargetAdapter(sample)); var newPointer = new JsonPointer("/books/0/author/hello"); Assert.Equal("world", (string)newPointer.Find(sample)); }
public void Add_an_array_element() { var sample = PatchTests.GetSample2(); var patchDocument = new PatchDocument(); var pointer = new JsonPointer("/books"); patchDocument.AddOperation(new AddOperation() { Path = pointer, Value = new JObject(new[] { new JProperty("author", "James Brown") }) }); patchDocument.ApplyTo(sample); var list = sample["books"] as JArray; Assert.Equal(3, list.Count); }
public void Copy_array_element() { var sample = PatchTests.GetSample2(); var patchDocument = new PatchDocument(); var frompointer = new JsonPointer("/books/0"); var topointer = new JsonPointer("/books/-"); patchDocument.AddOperation(new CopyOperation() { FromPath = frompointer, Path = topointer }); patchDocument.ApplyTo(sample); var result = new JsonPointer("/books/2").Find(sample); Assert.IsType(typeof(JObject), result); }
public void Remove_an_array_element() { var sample = PatchTests.GetSample2(); var patchDocument = new PatchDocument(); var pointer = new JsonPointer("/books/0"); patchDocument.AddOperation(new RemoveOperation() { Path = pointer }); patchDocument.ApplyTo(sample); Assert.Throws(typeof(PathNotFoundException), () => { var x = pointer.Find("/books/1"); }); }
public void Add_an_non_existing_member_property() // Why isn't this replace? { var sample = PatchTests.GetSample2(); var patchDocument = new PatchDocument(); var pointer = new JsonPointer("/books/0/ISBN"); patchDocument.AddOperation(new AddOperation() { Path = pointer, Value = new JValue("213324234343") }); patchDocument.ApplyTo(sample); var result = (string)pointer.Find(sample); Assert.Equal("213324234343", result); }
public void Move_property() { var sample = PatchTests.GetSample2(); var patchDocument = new PatchDocument(); var frompointer = new JsonPointer("/books/0/author"); var topointer = new JsonPointer("/books/1/author"); patchDocument.AddOperation(new MoveOperation() { FromPath = frompointer, Path = topointer }); patchDocument.ApplyTo(sample); var result = (string)topointer.Find(sample); Assert.Equal("F. Scott Fitzgerald", result); }
public void Append_array_elements_noIndex() { var sample = PatchTests.GetSample2(); var patchDocument = new PatchDocument(); var pointer = new JsonPointer("/books/"); patchDocument.AddOperation(new AddEachOperation() { Path = pointer, Value = new JArray() { new JObject(new[] { new JProperty("author", "James Brown") }), new JObject(new[] { new JProperty("cat", "Garfield") }), new JObject(new[] { new JProperty("producer", "Kingston") }), } }); Assert.Throws(typeof(ArgumentException), () => { patchDocument.ApplyTo(sample); }); }
public void Copy_property() { var sample = PatchTests.GetSample2(); var patchDocument = new PatchDocument(); var frompointer = new JsonPointer("/books/0/ISBN"); var topointer = new JsonPointer("/books/1/ISBN"); patchDocument.AddOperation(new AddOperation() { Path = frompointer, Value = new JValue("21123123") }); patchDocument.AddOperation(new CopyOperation() { FromPath = frompointer, Path = topointer }); patchDocument.ApplyTo(sample); var result = new JsonPointer("/books/1/ISBN").Find(sample); Assert.Equal("21123123", result); }
private void ApplyPatch(int patchIndex, AssetLocation patchSourcefile, JsonPatch jsonPatch, ref int applied, ref int notFound, ref int errorCount) { EnumAppSide targetSide = jsonPatch.Side == null ? jsonPatch.File.Category.SideType : (EnumAppSide)jsonPatch.Side; if (targetSide != EnumAppSide.Universal && jsonPatch.Side != api.Side) { return; } var loc = jsonPatch.File.Clone(); if (jsonPatch.File.Path.EndsWith("*")) { List <IAsset> assets = api.Assets.GetMany(jsonPatch.File.Path.TrimEnd('*'), jsonPatch.File.Domain, false); foreach (var val in assets) { jsonPatch.File = val.Location; ApplyPatch(patchIndex, patchSourcefile, jsonPatch, ref applied, ref notFound, ref errorCount); } jsonPatch.File = loc; return; } if (!loc.Path.EndsWith(".json")) { loc.Path += ".json"; } var asset = api.Assets.TryGet(loc); if (asset == null) { if (jsonPatch.File.Category == null) { api.World.Logger.VerboseDebug("Patch {0} in {1}: File {2} not found. Wrong asset category", patchIndex, patchSourcefile, loc); } else { EnumAppSide catSide = jsonPatch.File.Category.SideType; if (catSide != EnumAppSide.Universal && api.Side != catSide) { api.World.Logger.VerboseDebug("Patch {0} in {1}: File {2} not found. Hint: This asset is usually only loaded {3} side", patchIndex, patchSourcefile, loc, catSide); } else { api.World.Logger.VerboseDebug("Patch {0} in {1}: File {2} not found", patchIndex, patchSourcefile, loc); } } notFound++; return; } Operation op = null; switch (jsonPatch.Op) { case EnumJsonPatchOp.Add: if (jsonPatch.Value == null) { api.World.Logger.Error("Patch {0} in {1} failed probably because it is an add operation and the value property is not set or misspelled", patchIndex, patchSourcefile); errorCount++; return; } op = new AddOperation() { Path = new Tavis.JsonPointer(jsonPatch.Path), Value = jsonPatch.Value.Token }; break; case EnumJsonPatchOp.Remove: op = new RemoveOperation() { Path = new Tavis.JsonPointer(jsonPatch.Path) }; break; case EnumJsonPatchOp.Replace: if (jsonPatch.Value == null) { api.World.Logger.Error("Patch {0} in {1} failed probably because it is a replace operation and the value property is not set or misspelled", patchIndex, patchSourcefile); errorCount++; return; } op = new ReplaceOperation() { Path = new Tavis.JsonPointer(jsonPatch.Path), Value = jsonPatch.Value.Token }; break; case EnumJsonPatchOp.Copy: op = new CopyOperation() { Path = new Tavis.JsonPointer(jsonPatch.Path), FromPath = new JsonPointer(jsonPatch.FromPath) }; break; case EnumJsonPatchOp.Move: op = new MoveOperation() { Path = new Tavis.JsonPointer(jsonPatch.Path), FromPath = new JsonPointer(jsonPatch.FromPath) }; break; } PatchDocument patchdoc = new PatchDocument(op); JToken token = null; try { token = JToken.Parse(asset.ToText()); } catch (Exception e) { api.World.Logger.Error("Patch {0} (target: {3}) in {1} failed probably because the syntax of the value is broken: {2}", patchIndex, patchSourcefile, e, loc); errorCount++; return; } try { patchdoc.ApplyTo(token); } catch (Tavis.PathNotFoundException p) { api.World.Logger.Error("Patch {0} (target: {4}) in {1} failed because supplied path {2} is invalid: {3}", patchIndex, patchSourcefile, jsonPatch.Path, p.Message, loc); errorCount++; return; } catch (Exception e) { api.World.Logger.Error("Patch {0} (target: {3}) in {1} failed, following Exception was thrown: {2}", patchIndex, patchSourcefile, e.Message, loc); errorCount++; return; } string text = token.ToString(); asset.Data = System.Text.Encoding.UTF8.GetBytes(text); applied++; }
private void ApplyPatch(int patchIndex, AssetLocation patchSourcefile, JsonPatch jsonPatch, ref int applied, ref int notFound, ref int errorCount) { if (jsonPatch.SideType != EnumAppSide.Universal && jsonPatch.SideType != api.Side) { return; } var path = jsonPatch.File.Path; if (!path.EndsWith(".json")) { path += ".json"; } var asset = api.Assets.TryGet(path); if (asset == null) { api.World.Logger.VerboseDebug("Patch {0} in {1}: File {2} not found", patchIndex, patchSourcefile, path); notFound++; return; } Operation op = null; switch (jsonPatch.Op) { case EnumJsonPatchOp.Add: if (jsonPatch.Value == null) { api.World.Logger.Error("Patch {0} in {1} failed probably because it is an add operation and the value property is not set or misspelled", patchIndex, patchSourcefile); errorCount++; return; } op = new AddOperation() { Path = new Tavis.JsonPointer(jsonPatch.Path), Value = jsonPatch.Value.Token }; break; case EnumJsonPatchOp.Remove: op = new RemoveOperation() { Path = new Tavis.JsonPointer(jsonPatch.Path) }; break; case EnumJsonPatchOp.Replace: if (jsonPatch.Value == null) { api.World.Logger.Error("Patch {0} in {1} failed probably because it is a replace operation and the value property is not set or misspelled", patchIndex, patchSourcefile); errorCount++; return; } op = new ReplaceOperation() { Path = new Tavis.JsonPointer(jsonPatch.Path), Value = jsonPatch.Value.Token }; break; case EnumJsonPatchOp.Copy: op = new CopyOperation() { Path = new Tavis.JsonPointer(jsonPatch.Path), FromPath = new JsonPointer(jsonPatch.FromPath) }; break; case EnumJsonPatchOp.Move: op = new MoveOperation() { Path = new Tavis.JsonPointer(jsonPatch.Path), FromPath = new JsonPointer(jsonPatch.FromPath) }; break; } PatchDocument patchdoc = new PatchDocument(op); JToken token = null; try { token = JToken.Parse(asset.ToText()); } catch (Exception e) { api.World.Logger.Error("Patch {0} in {1} failed probably because the syntax of the value is broken: {2}", patchIndex, patchSourcefile, e); errorCount++; return; } try { patchdoc.ApplyTo(token); } catch (Tavis.PathNotFoundException p) { api.World.Logger.Error("Patch {0} in {1} failed because supplied path {2} is invalid: {3}", patchIndex, patchSourcefile, jsonPatch.Path, p.Message); errorCount++; return; } catch (Exception e) { api.World.Logger.Error("Patch {0} in {1} failed, following Exception was thrown: {2}", patchIndex, patchSourcefile, e.Message); errorCount++; return; } string text = token.ToString(); asset.Data = System.Text.Encoding.UTF8.GetBytes(text); applied++; }