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

adds changes for upconverting JS value for dom collections #749

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
10 changes: 10 additions & 0 deletions src/dom/Sequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ export function Sequence(ionType: IonType) {
this.push(child);
}
this._setAnnotations(annotations);

return new Proxy(this, {
set: function (target, index, value): boolean {
if (!(value instanceof Value)) {
value = Value.from(value);
}
target[index] = value;
return true; // Indicates that the assignment succeeded
},
});
}

get(...pathElements: PathElement[]): Value | null {
Expand Down
3 changes: 3 additions & 0 deletions src/dom/Struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ export class Struct extends Value(
// All values set by the user are stored in `this._fields` to avoid
// potentially overwriting Struct methods.
set: function (target, name, value): boolean {
if (!(value instanceof Value)) {
value = Value.from(value);
}
target._fields[name] = [value];
return true; // Indicates that the assignment succeeded
},
Expand Down
18 changes: 18 additions & 0 deletions test/dom/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,12 @@ describe("DOM", () => {

let planets: string[] = l.elements().map((s) => s.stringValue()!);
assert.equal(4, planets.length);

// set a JS value into the list that automatically upconverts to dom Value
l[0] = "Saturn";
assert.equal(l[0], "Saturn");
assert.equal(IonTypes.STRING, l[0].getType());
desaikd marked this conversation as resolved.
Show resolved Hide resolved
assert.isTrue(l[0].ionEquals(dom.Value.from("Saturn")));
});

it("load() List as any", () => {
Expand Down Expand Up @@ -405,6 +411,12 @@ describe("DOM", () => {

let planets: string[] = s.elements().map((s) => s.stringValue()!);
assert.equal(4, planets.length);

// set a JS value into the sexpression that automatically upconverts to dom Value
s[0] = "Saturn";
assert.equal(s[0], "Saturn");
assert.equal(IonTypes.STRING, s[0].getType());
assert.isTrue(s[0].ionEquals(dom.Value.from("Saturn")));
});

it("load() SExpression as any", () => {
Expand Down Expand Up @@ -460,6 +472,12 @@ describe("DOM", () => {
}

assert.equal(2, s.fields().length);

// set a field value using a JS value that automatically upconverts to dom Value
s["age"] = 43;
assert.equal(43, s["age"]);
assert.equal(IonTypes.INT, s["age"].getType());
assert.isTrue(s["age"].ionEquals(dom.Value.from(43)));
});

it("load() Struct as any", () => {
Expand Down