Skip to content

Commit

Permalink
[basic form] Allow to set parameters in the form which parents are un…
Browse files Browse the repository at this point in the history
…defined (#1254)
  • Loading branch information
Andres Martinez Gotor authored Oct 28, 2019
1 parent 8b580b2 commit 0aeb411
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
32 changes: 28 additions & 4 deletions dashboard/src/shared/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,12 +348,36 @@ describe("setValue", () => {
result: "foo: bar\nnew: value\n",
},
{
description: "[Not Supported] Adding a new nested value returns an error",
description: "should add a new nested value",
values: "foo: bar",
path: "this.new",
newValue: 1,
result: "foo: bar\nthis:\n new: 1\n",
error: false,
},
{
description: "should add a new deeply nested value",
values: "foo: bar",
path: "this.new.value",
newValue: "1",
result: undefined,
error: true,
newValue: 1,
result: "foo: bar\nthis:\n new:\n value: 1\n",
error: false,
},
{
description: "Adding a value for a path partially defined (null)",
values: "foo: bar\nthis:\n",
path: "this.new.value",
newValue: 1,
result: "foo: bar\nthis:\n new:\n value: 1\n",
error: false,
},
{
description: "Adding a value for a path partially defined (object)",
values: "foo: bar\nthis: {}\n",
path: "this.new.value",
newValue: 1,
result: "foo: bar\nthis: { new: { value: 1 } }\n",
error: false,
},
].forEach(t => {
it(t.description, () => {
Expand Down
35 changes: 34 additions & 1 deletion dashboard/src/shared/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// that are used in this package
import * as AJV from "ajv";
import * as jsonSchema from "json-schema";
import { set } from "lodash";
import * as YAML from "yaml";
import { IBasicFormEnablerParam, IBasicFormParam } from "./types";

Expand Down Expand Up @@ -82,10 +83,42 @@ function orderParams(params: {
return params;
}

function getDefinedPath(allElementsButTheLast: string[], doc: YAML.ast.Document) {
let currentPath: string[] = [];
let foundUndefined = false;
allElementsButTheLast.forEach(p => {
// Iterate over the path until finding an element that is not defined
if (!foundUndefined) {
const pathToEvaluate = currentPath.concat(p);
const elem = (doc as any).getIn(pathToEvaluate);
if (elem === undefined || elem === null) {
foundUndefined = true;
} else {
currentPath = pathToEvaluate;
}
}
});
return currentPath;
}

// setValue modifies the current values (text) based on a path
export function setValue(values: string, path: string, newValue: any) {
const doc = YAML.parseDocument(values);
const splittedPath = path.split(".");
let splittedPath = path.split(".");
// If the path is not defined (the parent nodes are undefined)
// We need to change the path and the value to set to avoid accessing
// the undefined node. For example, if a.b is undefined:
// path: a.b.c, value: 1 ==> path: a.b, value: {c: 1}
// TODO(andresmgot): In the future, this may be implemented in the YAML library itself
// https://github.com/eemeli/yaml/issues/131
const allElementsButTheLast = splittedPath.slice(0, splittedPath.length - 1);
const parentNode = (doc as any).getIn(allElementsButTheLast);
if (parentNode === undefined) {
const definedPath = getDefinedPath(allElementsButTheLast, doc);
const remainingPath = splittedPath.slice(definedPath.length + 1);
newValue = set({}, remainingPath.join("."), newValue);
splittedPath = splittedPath.slice(0, definedPath.length + 1);
}
(doc as any).setIn(splittedPath, newValue);
return doc.toString();
}
Expand Down

0 comments on commit 0aeb411

Please sign in to comment.