Skip to content

Commit 530f77c

Browse files
author
L0ndra
committed
fix bugs in Model
1 parent 382ee1c commit 530f77c

2 files changed

Lines changed: 40 additions & 33 deletions

File tree

src/SolidifyProject.Engine.Infrastructure/Models/PageModel.cs

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,14 @@ public override void Parse()
6969

7070
public void MapDataToModel(ExpandoObject data)
7171
{
72-
MapDataToPageModel(Model, data);
72+
if (Model is ExpandoObject)
73+
{
74+
MapDataToPageModel(Model, data);
75+
}
76+
else
77+
{
78+
Model = getValueFromDataObject(Model, data);
79+
}
7380
}
7481

7582
private void ParseAttributeLine(string line)
@@ -112,6 +119,12 @@ private void ParseAttributeLine(string line)
112119
return;
113120
}
114121

122+
if (MODEL_ATTRIBUTE_PREFIX.Any(x => x.Equals(attributeName, StringComparison.OrdinalIgnoreCase)))
123+
{
124+
Model = attributeValue;
125+
return;
126+
}
127+
115128
if (CUSTOM_ATTRIBUTE_PREFIX_SEPARATOR.Any(x => attributeName.Contains(x)))
116129
{
117130
var customAttributeNames = attributeName.Split(CUSTOM_ATTRIBUTE_PREFIX_SEPARATOR, StringSplitOptions.RemoveEmptyEntries);
@@ -136,42 +149,34 @@ private void ParseAttributeLine(string line)
136149
return;
137150
}
138151

139-
if (CUSTOM_ATTRIBUTE_PREFIX_SEPARATOR.Any(x => attributeName.Contains(x)))
140-
{
141-
var modelAttributeNames = attributeName.Split(CUSTOM_ATTRIBUTE_PREFIX_SEPARATOR, StringSplitOptions.RemoveEmptyEntries);
142-
if (modelAttributeNames.Length >= 2 && MODEL_ATTRIBUTE_PREFIX.Any(x => x.Equals(modelAttributeNames[0], StringComparison.InvariantCultureIgnoreCase)))
143-
{
144-
ParseCustomAttribute(Model, modelAttributeNames.Skip(1), attributeValue);
145-
}
146-
else
147-
{
148-
throw new ArgumentException($"Unknown name format of custom attribute \"{attributeName}\" at line \"{line}\"");
149-
}
150-
151-
return;
152-
}
153152

154153
throw new ArgumentException($"Unknown attribute \"{attributeName}\" at line \"{line}\"");
155154
}
156155

157156
private void ParseCustomAttribute(ExpandoObject obj, IEnumerable<string> attributeNames, string attributeValue)
158157
{
159-
ICollection<KeyValuePair<string, object>> node = obj;
158+
IDictionary<string, object> node = obj;
160159
var currentSection = attributeNames.First();
161-
object currentValue;
162-
160+
163161
if (attributeNames.Count() > 1)
164162
{
165-
var subNode = new ExpandoObject();
166-
currentValue = subNode;
167-
ParseCustomAttribute(subNode, attributeNames.Skip(1), attributeValue);
163+
ExpandoObject currentNode;
164+
if (node.ContainsKey(currentSection))
165+
{
166+
currentNode = node[currentSection] as ExpandoObject;
167+
}
168+
else
169+
{
170+
currentNode = new ExpandoObject();
171+
node.Add(new KeyValuePair<string, object>(currentSection, currentNode));
172+
}
173+
174+
ParseCustomAttribute(currentNode, attributeNames.Skip(1), attributeValue);
168175
}
169176
else
170177
{
171-
currentValue = attributeValue;
178+
node.Add(new KeyValuePair<string, object>(currentSection, attributeValue));
172179
}
173-
174-
node.Add(new KeyValuePair<string, object>(currentSection, currentValue));
175180
}
176181

177182
private void ParseContent(IEnumerable<string> lines)
@@ -182,7 +187,8 @@ private void ParseContent(IEnumerable<string> lines)
182187
private void MapDataToPageModel(ExpandoObject model, ExpandoObject data)
183188
{
184189
IDictionary<string, object> modelDict = model;
185-
foreach (var keyValuePair in model)
190+
Dictionary<string, object> objectToIterate = model.ToDictionary(k => k.Key, v => v.Value);
191+
foreach (var keyValuePair in objectToIterate)
186192
{
187193
if (keyValuePair.Value is ExpandoObject expObject)
188194
{
@@ -207,7 +213,7 @@ private object getValueFromDataObject(string path, ExpandoObject data)
207213
{
208214
if (attributeNames.Length == 1)
209215
{
210-
return null;
216+
return data;
211217
}
212218

213219
attributeNames = attributeNames.Skip(1).ToArray();
@@ -216,14 +222,15 @@ private object getValueFromDataObject(string path, ExpandoObject data)
216222
object value = data;
217223
foreach (var attribute in attributeNames)
218224
{
219-
if (value is IDictionary<string,object> dict)
220-
{
221-
value = dict[attribute];
222-
}
223-
else
225+
IDictionary<string, object> dict = value as IDictionary<string, object>;
226+
227+
if (dict == null)
224228
{
225-
return null;
229+
dict = value.GetType().GetProperties()
230+
.ToDictionary(x => x.Name, x => x.GetValue(value, null));
226231
}
232+
233+
value = dict[attribute];
227234
}
228235

229236
return value;

src/Test/SolidifyProject.Engine.Test.Unit/Infrastructure/Models/PageModelTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ public void ParseModelComplexMultipleItemsTest()
214214

215215
var actualModel = new PageModel { ContentRaw = @"
216216
Model.category.goods: Data.goods
217-
model.category.bannerUrl: Data.banner.url
217+
model.bannerUrl: Data.banner.url
218218
---
219219
"};
220220

0 commit comments

Comments
 (0)