diff --git a/docs/docs/collections/dictionaries.md b/docs/docs/collections/dictionaries.md index 6a3d0a89..94a6f884 100644 --- a/docs/docs/collections/dictionaries.md +++ b/docs/docs/collections/dictionaries.md @@ -146,7 +146,7 @@ myDict.forEach(def (key, value) => { }); ``` -### dict.merge(Dict) +### dict.merge(Dict) -> Dict To merge two dicts together we use `.merge`. This operation will take a shallow copy of the dict the `.merge` method was called on and add any items from the dictionary passed into the method. If there are keys that exist in both dictionaries @@ -159,4 +159,20 @@ const dictTwo = {"key3": 4,"key1":0}; const mergedDict = dictOne.merge(dictTwo); mergedDict; //{"key2": 3, "key": 1, "key3": 4, "key1": 0} -``` \ No newline at end of file +``` + +### dict.toObj(Dict, Object) -> Object + +Returns an object created from the given dict and class ref. Dictionary fields that aren't strings are converted to strings and set on the object. To retrieve those fields, use the `.getAttribute(String)` method. + +```cs +class A {} + +const dict = {"key": 1, "key1": 2, "key2": 3}; +const obj = dict.toObj(A()); + +obj; // +obj.key // 1 +obj.key1 // 2 +obj.key2 // 3 +``` diff --git a/src/vm/datatypes/dicts/dict-source.h b/src/vm/datatypes/dicts/dict-source.h index 0e6a6732..06bb6931 100644 --- a/src/vm/datatypes/dicts/dict-source.h +++ b/src/vm/datatypes/dicts/dict-source.h @@ -4,6 +4,10 @@ " *\n" \ " * We should always strive to write methods in C where possible.\n" \ " */\n" \ +"\n" \ +"import Object;\n" \ +"\n" \ +"\n" \ "def forEach(dict, func) {\n" \ " const dictKeys = dict.keys();\n" \ "\n" \ @@ -21,4 +25,17 @@ "\n" \ " return newDict;\n" \ "}\n" \ +"\n" \ +"def toObj(dict, obj) {\n" \ +" dict.forEach(def(k, v) => {\n" \ +" if (type(k) != 'string') {\n" \ +" const fieldName = k.toString();\n" \ +" obj.setAttribute(fieldName, v);\n" \ +" return;\n" \ +" }\n" \ +" obj.setAttribute(k, v);\n" \ +" });\n" \ +"\n" \ +" return obj;\n" \ +"}\n" \ diff --git a/src/vm/datatypes/dicts/dict.du b/src/vm/datatypes/dicts/dict.du index 8fc79237..c0068b91 100644 --- a/src/vm/datatypes/dicts/dict.du +++ b/src/vm/datatypes/dicts/dict.du @@ -20,4 +20,17 @@ def merge(dict, anotherDict) { }); return newDict; -} \ No newline at end of file +} + +def toObj(dict, obj) { + dict.forEach(def(k, v) => { + if (type(k) != 'string') { + const fieldName = k.toString(); + obj.setAttribute(fieldName, v); + return; + } + obj.setAttribute(k, v); + }); + + return obj; +} diff --git a/tests/dicts/import.du b/tests/dicts/import.du index f755da74..671f5f69 100644 --- a/tests/dicts/import.du +++ b/tests/dicts/import.du @@ -15,3 +15,4 @@ import "toString.du"; import "toBool.du"; import "forEach.du"; import "merge.du"; +import "toObj.du"; diff --git a/tests/dicts/toObj.du b/tests/dicts/toObj.du new file mode 100644 index 00000000..ea60dae9 --- /dev/null +++ b/tests/dicts/toObj.du @@ -0,0 +1,34 @@ +/** +* toObj.du +* +* Testing the dict.toObj() method +* +* .toObj() returns an object created from the given dict and class ref. +*/ +from UnitTest import UnitTest; + + +class Test {} + +class TestDictToObj < UnitTest { + testDictToObj() { + const myDict = { + "key": 1, + "key1": true, + true: false, + false: true, + nil: 10, + 10: nil, + 10.5: 10.5 + }; + const obj = myDict.toObj(Test()); + this.assertNotNil(obj); + this.assertEquals(obj.key, 1); + this.assertEquals(obj.getAttribute("true"), false); + this.assertEquals(obj.getAttribute("false"), true); + this.assertEquals(obj.getAttribute("nil"), 10); + this.assertEquals(obj.getAttribute("10.5"), 10.5); + } +} + +TestDictToObj().run(); \ No newline at end of file