-
Notifications
You must be signed in to change notification settings - Fork 434
/
UpdateValues.cs
56 lines (45 loc) · 1.73 KB
/
UpdateValues.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using Squidex.Domain.Apps.Core.Contents;
using Squidex.Domain.Apps.Core.Schemas;
using Squidex.Domain.Apps.Core.Scripting;
using Squidex.Infrastructure.Json.Objects;
namespace Squidex.Domain.Apps.Core.ConvertContent;
public sealed class UpdateValues : IContentValueConverter
{
private readonly ContentData existingData;
private readonly IScriptEngine scriptEngine;
private ScriptVars? vars;
public UpdateValues(ContentData existingData, IScriptEngine scriptEngine)
{
this.existingData = existingData;
this.scriptEngine = scriptEngine;
}
public (bool Remove, JsonValue) ConvertValue(IField field, JsonValue source, IField? parent)
{
if (source.Value is not JsonObject jsonObject)
{
return (false, source);
}
if (jsonObject.TryGetValue("$unset", out var value1) && !Equals(value1.Value, false))
{
return (true, source);
}
if (!jsonObject.TryGetValue("$update", out var value2) || value2.Value is not string update)
{
return (false, source);
}
var options = new ScriptOptions { Readonly = true };
vars ??= new ScriptVars
{
["$data"] = existingData,
["$self"] = jsonObject
};
var result = scriptEngine.Execute(vars, update, options);
return (false, result);
}
}