Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add dict.toObj(Object) method #734

Merged
merged 2 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions docs/docs/collections/dictionaries.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -159,4 +159,20 @@ const dictTwo = {"key3": 4,"key1":0};
const mergedDict = dictOne.merge(dictTwo);

mergedDict; //{"key2": 3, "key": 1, "key3": 4, "key1": 0}
```
```

### 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; // <A instance>
obj.key // 1
obj.key1 // 2
obj.key2 // 3
```
17 changes: 17 additions & 0 deletions src/vm/datatypes/dicts/dict-source.h
Original file line number Diff line number Diff line change
Expand Up @@ -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" \
Expand All @@ -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" \

15 changes: 14 additions & 1 deletion src/vm/datatypes/dicts/dict.du
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,17 @@ def merge(dict, anotherDict) {
});

return newDict;
}
}

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;
}
1 change: 1 addition & 0 deletions tests/dicts/import.du
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ import "toString.du";
import "toBool.du";
import "forEach.du";
import "merge.du";
import "toObj.du";
34 changes: 34 additions & 0 deletions tests/dicts/toObj.du
Original file line number Diff line number Diff line change
@@ -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();
Loading