-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathUpdateModel.cs
More file actions
139 lines (124 loc) · 4.33 KB
/
Copy pathUpdateModel.cs
File metadata and controls
139 lines (124 loc) · 4.33 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using System.Collections;
using System.Reflection;
using BlocklyNet.Core.Blocks;
using BlocklyNet.Core.Blocks.Lists;
using BlocklyNet.Core.Model;
using BlocklyNet.Extensions.Builder;
namespace BlocklyNet.Extensions;
/// <summary>
/// Update a single property of a model.
/// </summary>
[CustomBlock(
"update_model_property",
"",
@"{
""message0"": ""UpdateModelProperty %1 %2 %3 %4 %5 %6 %7 %8"",
""args0"": [
{
""type"": ""field_variable"",
""name"": ""VAR"",
""text"": ""Variable""
},
{
""type"": ""input_dummy""
},
{
""type"": ""field_label_serializable"",
""name"": ""PATH"",
""text"": ""Path to value""
},
{
""type"": ""input_value"",
""name"": ""PATH"",
""check"": ""String""
},
{
""type"": ""field_label_serializable"",
""name"": ""INDEXES"",
""text"": ""Indexes in path""
},
{
""type"": ""input_value"",
""name"": ""INDEXES"",
""check"": [""Array(Number)"", ""Array""]
},
{
""type"": ""field_label_serializable"",
""name"": ""VALUE"",
""text"": ""Value""
},
{
""type"": ""input_value"",
""name"": ""VALUE""
}
],
""previousStatement"": null,
""nextStatement"": null,
""colour"": ""#107159"",
""tooltip"": ""Update a single field of a model variable"",
""helpUrl"": """"
}",
@"{
""inputs"": {
""PATH"": {
""shadow"": {
""type"": ""text"",
""fields"": {
""TEXT"": """"
}
}
}
}
}"
)]
public class UpdateModelProperty : Block
{
/// <inheritdoc/>
protected override async Task<object?> EvaluateAsync(Context context)
{
var varName = Fields["VAR"];
var data = context.Variables.TryGetValue(varName, out var local) ? local : context.GetRootContext().Variables[varName];
var value = await Values.EvaluateAsync("VALUE", context);
var path = await Values.EvaluateAsync<string>("PATH", context) ?? "";
var rawIndexes = await Values.EvaluateAsync<IEnumerable>("INDEXES", context, false);
var indexes = rawIndexes?.Cast<object>().ToArray() ?? [];
var parts = path.Split(".");
var i = 0;
foreach (var part in parts.Take(parts.Length - 1))
{
var isArray = part.EndsWith("[]");
var field = isArray ? part[..^2] : part;
/* Read from a string dictionary - e.g. some ExpandoObject. */
if (data is IDictionary<string, object> stringSource)
data = stringSource[field];
/* Read from generic dictionary. */
else if (data is IDictionary source)
{
/* Check key type. */
var keyType = source.GetType().GetGenericArguments()[0];
data = source[keyType.IsEnum ? Enum.Parse(keyType, field) : field];
}
/* Read from .NET instance. */
else
data = data!.GetType().InvokeMember(field, BindingFlags.GetProperty | BindingFlags.GetField, null, data, null)!;
/* Resolve index. */
if (isArray)
data = new ListWrapper(data)[Convert.ToInt32(indexes[i++]) - 1];
}
var leaf = parts[^1];
/* Write to a string dictionary - e.g. some ExpandoObject. */
if (data is IDictionary<string, object> stringTarget)
stringTarget[leaf] = value!;
/* Write to generic dictionary. */
else if (data is IDictionary target)
{
/* Check key type. */
var keyType = target.GetType().GetGenericArguments()[0];
target[keyType.IsEnum ? Enum.Parse(keyType, leaf) : leaf] = value;
}
/* Write to .NET instance. */
else
data!.GetType().InvokeMember(leaf, BindingFlags.SetProperty | BindingFlags.SetField, null, data, [value]);
return await base.EvaluateAsync(context);
}
}