Browse Source

Allow setting nested values in scripts.

pull/815/head
Sebastian 4 years ago
parent
commit
87cb951db2
  1. 16
      backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/ContentWrapper/ContentFieldProperty.cs
  2. 4
      backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/ContentWrapper/JsonMapper.cs
  3. 61
      backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/Scripting/ContentDataObjectTests.cs

16
backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/ContentWrapper/ContentFieldProperty.cs

@ -1,10 +1,11 @@
// ========================================================================== // ==========================================================================
// Squidex Headless CMS // Squidex Headless CMS
// ========================================================================== // ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt) // Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license. // All rights reserved. Licensed under the MIT license.
// ========================================================================== // ==========================================================================
using System.Diagnostics;
using Jint.Native; using Jint.Native;
using Squidex.Infrastructure.Json.Objects; using Squidex.Infrastructure.Json.Objects;
@ -13,10 +14,11 @@ namespace Squidex.Domain.Apps.Core.Scripting.ContentWrapper
public sealed class ContentFieldProperty : CustomProperty public sealed class ContentFieldProperty : CustomProperty
{ {
private readonly ContentFieldObject contentField; private readonly ContentFieldObject contentField;
private IJsonValue? contentValue; private IJsonValue contentValue;
private JsValue? value; private JsValue? value;
private bool isChanged; private bool isChanged;
[DebuggerHidden]
protected override JsValue? CustomValue protected override JsValue? CustomValue
{ {
get get
@ -33,11 +35,13 @@ namespace Squidex.Domain.Apps.Core.Scripting.ContentWrapper
} }
set set
{ {
if (!Equals(this.value, value)) var newContentValue = JsonMapper.Map(value);
if (!Equals(contentValue, newContentValue))
{ {
this.value = value; this.value = value;
contentValue = null; contentValue = newContentValue;
contentField.MarkChanged(); contentField.MarkChanged();
isChanged = true; isChanged = true;
@ -58,7 +62,7 @@ namespace Squidex.Domain.Apps.Core.Scripting.ContentWrapper
public ContentFieldProperty(ContentFieldObject contentField, IJsonValue? contentValue = null) public ContentFieldProperty(ContentFieldObject contentField, IJsonValue? contentValue = null)
{ {
this.contentField = contentField; this.contentField = contentField;
this.contentValue = contentValue; this.contentValue = contentValue ?? JsonValue.Null;
} }
} }
} }

4
backend/src/Squidex.Domain.Apps.Core.Operations/Scripting/ContentWrapper/JsonMapper.cs

@ -59,11 +59,9 @@ namespace Squidex.Domain.Apps.Core.Scripting.ContentWrapper
foreach (var (key, value) in obj) foreach (var (key, value) in obj)
{ {
target.FastAddProperty(key, Map(value, engine), false, true, true); target.FastAddProperty(key, Map(value, engine), true, true, true);
} }
target.PreventExtensions();
return target; return target;
} }

61
backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/Scripting/ContentDataObjectTests.cs

@ -173,21 +173,63 @@ namespace Squidex.Domain.Apps.Core.Operations.Scripting
{ {
var original = var original =
new ContentData() new ContentData()
.AddField("number", .AddField("geo",
new ContentFieldData() new ContentFieldData()
.AddInvariant(JsonValue.Object().Add("lat", 1.0))); .AddInvariant(JsonValue.Object().Add("lat", 1.0)));
var expected = var expected =
new ContentData() new ContentData()
.AddField("number", .AddField("geo",
new ContentFieldData() new ContentFieldData()
.AddInvariant(JsonValue.Object().Add("lat", 1.0).Add("lon", 4.0))); .AddInvariant(JsonValue.Object().Add("lat", 1.0).Add("lon", 4.0)));
var result = ExecuteScript(original, @"data.number.iv = { lat: data.number.iv.lat, lon: data.number.iv.lat + 3 }"); var result = ExecuteScript(original, @"data.geo.iv = { lat: data.geo.iv.lat, lon: data.geo.iv.lat + 3 }");
Assert.Equal(expected, result);
}
[Fact]
public void Should_update_data_if_changing_nested_field()
{
var original =
new ContentData()
.AddField("geo",
new ContentFieldData()
.AddInvariant(JsonValue.Object().Add("lat", 1.0)));
var expected =
new ContentData()
.AddField("geo",
new ContentFieldData()
.AddInvariant(JsonValue.Object().Add("lat", 2.0).Add("lon", 4.0)));
var result = ExecuteScript(original, @"
var nested = data.geo.iv;
nested.lat = 2;
nested.lon = 4;
data.geo.iv = nested");
Assert.Equal(expected, result); Assert.Equal(expected, result);
} }
[Fact]
public void Should_not_update_data_if_not_changing_nested_field()
{
var original =
new ContentData()
.AddField("geo",
new ContentFieldData()
.AddInvariant(JsonValue.Object().Add("lat", 2.0).Add("lon", 4.0)));
var result = ExecuteScript(original, @"
var nested = data.geo.iv;
nested.lat = 2;
nested.lon = 4;
data.geo.iv = nested");
Assert.Same(original, result);
}
[Fact] [Fact]
public void Should_throw_if_defining_property_for_field() public void Should_throw_if_defining_property_for_field()
{ {
@ -258,19 +300,6 @@ namespace Squidex.Domain.Apps.Core.Operations.Scripting
Assert.Equal(new[] { "1", "2", "3", "4" }, result); Assert.Equal(new[] { "1", "2", "3", "4" }, result);
} }
[Fact]
public void Should_throw_exceptions_if_changing_objects()
{
var original =
new ContentData()
.AddField("obj",
new ContentFieldData()
.AddInvariant(JsonValue.Object().Add("readonly", 1)));
Assert.Throws<JavaScriptException>(() => ExecuteScript(original, "data.obj.iv.invalid = 1"));
Assert.Throws<JavaScriptException>(() => ExecuteScript(original, "data.obj.iv.readonly = 2"));
}
[Fact] [Fact]
public void Should_not_throw_exceptions_if_changing_arrays() public void Should_not_throw_exceptions_if_changing_arrays()
{ {

Loading…
Cancel
Save