[EXaml] Support Dictionary
authorFang Xiaohui <xiaohui.fang@samsung.com>
Wed, 2 Nov 2022 03:31:30 +0000 (11:31 +0800)
committerdongsug-song <35130733+dongsug-song@users.noreply.github.com>
Thu, 10 Nov 2022 01:25:11 +0000 (10:25 +0900)
src/Tizen.NUI.XamlBuild/src/public/EXamlBuild/EXaml/EXamlAddToDictionary.cs [new file with mode: 0755]
src/Tizen.NUI.XamlBuild/src/public/EXamlBuild/EXamlCreateObjectVisitor.cs
src/Tizen.NUI.XamlBuild/src/public/EXamlBuild/EXamlSetPropertiesVisitor.cs
src/Tizen.NUI.XamlBuild/src/public/EXamlBuild/Utility/EXamlOperationType.cs
src/Tizen.NUI.XamlBuild/src/public/XamlBuild/CreateObjectVisitor.cs

diff --git a/src/Tizen.NUI.XamlBuild/src/public/EXamlBuild/EXaml/EXamlAddToDictionary.cs b/src/Tizen.NUI.XamlBuild/src/public/EXamlBuild/EXaml/EXamlAddToDictionary.cs
new file mode 100755 (executable)
index 0000000..adaf926
--- /dev/null
@@ -0,0 +1,74 @@
+/*
+ * Copyright(c) 2022 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+using Mono.Cecil;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Reflection;
+using System.Text;
+using Tizen.NUI.Binding;
+using Tizen.NUI.EXaml.Build.Tasks;
+
+namespace Tizen.NUI.EXaml
+{
+    //use ^^
+    internal class EXamlAddToDictionary : EXamlOperation
+    {
+        internal override string Write()
+        {
+            if (false == Parent.IsValid ||
+                false == (Key as EXamlCreateObject)?.IsValid ||
+                false == (Value as EXamlCreateObject)?.IsValid)
+            {
+                return "";
+            }
+
+            string ret = String.Format("({0} ({1} {2} {3}))\n",
+                         eXamlContext.GetValueString((int)EXamlOperationType.AddToDictionary),
+                         eXamlContext.GetValueString(Parent.Index),
+                         eXamlContext.GetValueString(Key),
+                         eXamlContext.GetValueString(Value));
+            return ret;
+        }
+
+        public EXamlAddToDictionary(EXamlContext context, EXamlCreateObject parent, object key, object value)
+            : base(context)
+        {
+            Parent = parent;
+            Key = key;
+            Value = value;
+
+            eXamlContext.eXamlOperations.Add(this);
+        }
+
+        public EXamlCreateObject Parent
+        {
+            get;
+        }
+
+        public object Key
+        {
+            get;
+        }
+
+        public object Value
+        {
+            get;
+        }
+    }
+}
index 88c5b72..5a4abbb 100755 (executable)
@@ -558,7 +558,7 @@ namespace Tizen.NUI.EXaml.Build.Tasks
                         ret = 0;
                     break;
                 case "System.UInt16":
-                    if (hasValue && short.TryParse(valueString, NumberStyles.Number, CultureInfo.InvariantCulture, out short outushort))
+                    if (hasValue && ushort.TryParse(valueString, NumberStyles.Number, CultureInfo.InvariantCulture, out ushort outushort))
                         ret = outushort;
                     else
                         ret = 0;
@@ -570,7 +570,7 @@ namespace Tizen.NUI.EXaml.Build.Tasks
                         ret = 0;
                     break;
                 case "System.UInt64":
-                    if (hasValue && long.TryParse(valueString, NumberStyles.Number, CultureInfo.InvariantCulture, out long outulong))
+                    if (hasValue && ulong.TryParse(valueString, NumberStyles.Number, CultureInfo.InvariantCulture, out ulong outulong))
                         ret = outulong;
                     else
                         ret = 0;
index 3c4fcba..81088c4 100755 (executable)
@@ -181,6 +181,13 @@ namespace Tizen.NUI.EXaml.Build.Tasks
                     new EXamlAddToResourceDictionary(Context, parentVar, keyName, Context.Values[node]);
                     isAdded = true;
                 }
+
+                if (!isAdded && CanAddToDictionary(parentVar, parentVar.GetType(), node, node, Context))
+                {
+                    var key = (node.Properties[XmlName.xKey] as ValueNode).Value as string;
+                    new EXamlAddToDictionary(Context, parentVar, key, Context.Values[node]);
+                    isAdded = true;
+                }
                 
                 // Collection element, implicit content, or implicit collection element.
                 if (!isAdded && parentVar.GetType().GetMethods(md => md.Name == "Add" && md.Parameters.Count == 1, Module).Any())
@@ -1050,6 +1057,31 @@ namespace Tizen.NUI.EXaml.Build.Tasks
             throw new XamlParseException("resources in ResourceDictionary require a x:Key attribute", lineInfo);
         }
 
+        static bool CanAddToDictionary(EXamlCreateObject parent, TypeReference collectionType, IElementNode node, IXmlLineInfo lineInfo, EXamlContext context)
+        {
+            var typeOfDictionary = collectionType.GetRealTypeOfDictionary();
+
+            if (null != typeOfDictionary)
+            {
+                if ("System.String" == typeOfDictionary.GenericArguments[0].FullName)
+                {
+                    if (node.Properties.ContainsKey(XmlName.xKey))
+                    {
+                        var valueType = node.XmlType.GetTypeReference(XmlTypeExtensions.ModeOfGetType.OnlyGetType, parent.GetType().Module, lineInfo);
+
+                        if ("System.Object" == typeOfDictionary.GenericArguments[1].FullName
+                            ||
+                            valueType.InheritsFromOrImplements(typeOfDictionary.GenericArguments[1]))
+                        {
+                            return true;
+                        }
+                    }
+                }
+            }
+
+            return false;
+        }
+
         static void Add(EXamlCreateObject parent, string propertyName, INode node, IXmlLineInfo iXmlLineInfo, EXamlContext context)
         {
             //Fang: Need to deal
index eaeb567..1ef9aee 100755 (executable)
@@ -48,6 +48,7 @@ namespace Tizen.NUI.EXaml
         RegisterXName,
         GetLongString,
         CreateDPObject,
+        AddToDictionary,
         CreateResourceDictionary,
         MAX
     }
index e670941..689f03f 100755 (executable)
@@ -269,7 +269,8 @@ namespace Tizen.NUI.Xaml.Build.Tasks
                 Context.Body.Variables.Add(vardef);
 
                 ValueNode vnode = null;
-                if (node.CollectionItems.Count == 1 && (vnode = node.CollectionItems.First() as ValueNode) != null &&
+                if (node.CollectionItems.Count == 1 &&
+                   (vnode = node.CollectionItems.First() as ValueNode) != null &&
                     vardef.VariableType.IsValueType)
                 {
                     //<Color>Purple</Color>
@@ -315,6 +316,7 @@ namespace Tizen.NUI.Xaml.Build.Tasks
                     {
                         Context.IL.Emit(OpCodes.Newobj, ctor);
                     }
+
                     Context.IL.Emit(OpCodes.Stloc, vardef);
                 }
                 else if (ctorInfo != null && node.Properties.ContainsKey(XmlName.xArguments) &&