generated from Exabyte-io/template-definitions
-
Notifications
You must be signed in to change notification settings - Fork 0
chore/SOF-6428: build dependency tree #40
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
1285b4e
chore: add draft for function to build dependency tree
azech-hqs d58f787
chore: adjust dependency builder to support dataSelector node property
azech-hqs 50daeb9
chore: add utility function for calling map on a tree
azech-hqs 8541e6a
test: add test for buildDependencies
azech-hqs 1fcb894
chore: add mapTree to utils index
azech-hqs 7f65cd4
chore: add function to determine schema type
azech-hqs 5d67204
chore: add function for building schema with dependency
azech-hqs 84c15ae
refactor: buildDependencyCase function
azech-hqs 5725422
test: getSchemaWithDependencies
azech-hqs dc4dce3
test: typeofSchema
azech-hqs e987abc
test: mapTree
azech-hqs 5f2925b
docs: add docstring for getSchemaWithDependencies
azech-hqs 0fe0548
test: adding enum dynamically to main schema
azech-hqs 2003cba
chore: add getSchemaWithDependencies to index
azech-hqs 9baa4eb
chore: use single lodash imports
azech-hqs bbed111
revert: single lodash imports
azech-hqs 5113e42
Merge branch 'main' into chore/SOF-6428
azech-hqs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| /** | ||
| * @summary Return nodes with `fn` function applied to each node. | ||
| * Note that the function `fn` must take a node as an argument and must return a node object. | ||
| * @param {Object[]} nodes - Array of nodes | ||
| * @param {Function} fn - function to be applied to each node | ||
| * @returns {Object[]} - Result of map | ||
| */ | ||
| export function mapTree(nodes, fn) { | ||
| return nodes.map((node) => { | ||
| const mappedNode = fn(node); | ||
| if (node?.children?.length) { | ||
| mappedNode.children = mapTree(node.children, fn); | ||
| } | ||
| return mappedNode; | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| import { expect } from "chai"; | ||
|
|
||
| import { buildDependencies, getSchemaWithDependencies, typeofSchema } from "../src/utils/schemas"; | ||
|
|
||
| describe("RJSF schema", () => { | ||
| const TREE = { | ||
| path: "/dft", | ||
| dataSelector: { key: "type", value: "data.type.slug", name: "data.type.name" }, | ||
| data: { | ||
| type: { | ||
| slug: "dft", | ||
| name: "Density Functional Theory", | ||
| }, | ||
| }, | ||
| children: [ | ||
| { | ||
| path: "/dft/lda", | ||
| dataSelector: { | ||
| key: "subtype", | ||
| value: "data.subtype.slug", | ||
| name: "data.subtype.name", | ||
| }, | ||
| data: { | ||
| subtype: { | ||
| slug: "lda", | ||
| name: "LDA", | ||
| }, | ||
| }, | ||
| children: [ | ||
| { | ||
| path: "/dft/lda/svwn", | ||
| dataSelector: { | ||
| key: "functional", | ||
| value: "data.functional.slug", | ||
| name: "data.functional.name", | ||
| }, | ||
| data: { | ||
| functional: { | ||
| slug: "svwn", | ||
| name: "SVWN", | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| path: "/dft/lda/pz", | ||
| dataSelector: { | ||
| key: "functional", | ||
| value: "data.functional.slug", | ||
| name: "data.functional.name", | ||
| }, | ||
| data: { | ||
| functional: { | ||
| slug: "pz", | ||
| name: "PZ", | ||
| }, | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| path: "/dft/gga", | ||
| dataSelector: { | ||
| key: "subtype", | ||
| value: "data.subtype.slug", | ||
| name: "data.subtype.name", | ||
| }, | ||
| data: { | ||
| subtype: { | ||
| slug: "gga", | ||
| name: "GGA", | ||
| }, | ||
| }, | ||
| children: [ | ||
| { | ||
| path: "/dft/gga/pbe", | ||
| dataSelector: { | ||
| key: "functional", | ||
| value: "data.functional.slug", | ||
| name: "data.functional.name", | ||
| }, | ||
| data: { | ||
| functional: { | ||
| slug: "pbe", | ||
| name: "PBE", | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| path: "/dft/gga/pw91", | ||
| dataSelector: { | ||
| key: "functional", | ||
| value: "data.functional.slug", | ||
| name: "data.functional.name", | ||
| }, | ||
| data: { | ||
| functional: { | ||
| slug: "pw91", | ||
| name: "PW91", | ||
| }, | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }; | ||
| const DFT_SCHEMA = { | ||
| type: "object", | ||
| properties: { | ||
| type: { | ||
| type: "string", | ||
| }, | ||
| subtype: { | ||
| type: "string", | ||
| }, | ||
| functional: { | ||
| type: "string", | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| it("dependencies block can be created from tree", () => { | ||
| const dependencies = buildDependencies([TREE]); | ||
|
|
||
| const [dftCase] = dependencies.dependencies.type.oneOf; | ||
| expect(dftCase.properties.subtype.enum).to.have.ordered.members(["lda", "gga"]); | ||
| expect(dftCase.properties.subtype.enumNames).to.have.ordered.members(["LDA", "GGA"]); | ||
|
|
||
| const [ldaCase, ggaCase] = dftCase.dependencies.subtype.oneOf; | ||
| expect(ldaCase.properties.subtype.enum).to.have.length(1); | ||
| expect(ldaCase.properties.functional.enum).to.have.ordered.members(["svwn", "pz"]); | ||
| expect(ldaCase.properties.functional.enumNames).to.have.ordered.members(["SVWN", "PZ"]); | ||
| expect(ldaCase).to.not.have.property("dependencies"); | ||
|
|
||
| expect(ggaCase.properties.subtype.enum).to.have.length(1); | ||
| expect(ggaCase.properties.functional.enum).to.have.ordered.members(["pbe", "pw91"]); | ||
| expect(ggaCase.properties.functional.enumNames).to.have.ordered.members(["PBE", "PW91"]); | ||
| expect(ggaCase).to.not.have.property("dependencies"); | ||
| }); | ||
|
|
||
| it("can be created with dependencies from schema", () => { | ||
| const rjsfSchema = getSchemaWithDependencies({ | ||
| schema: DFT_SCHEMA, | ||
| nodes: [TREE], | ||
| }); | ||
| expect(rjsfSchema.type).to.be.eql(DFT_SCHEMA.type); | ||
| expect(rjsfSchema.properties).to.be.eql(DFT_SCHEMA.properties); | ||
| expect(rjsfSchema).to.have.property("dependencies"); | ||
| }); | ||
|
|
||
| it("enum and enumNames can be added to schema properties", () => { | ||
| const rjsfSchema = getSchemaWithDependencies({ | ||
| schema: DFT_SCHEMA, | ||
| nodes: [TREE], | ||
| modifyProperties: true, | ||
| }); | ||
| // console.log(JSON.stringify(rjsfSchema, null, 4)); | ||
| expect(rjsfSchema.type).to.be.eql(DFT_SCHEMA.type); | ||
| expect(rjsfSchema.properties.type).to.have.property("enum"); | ||
| expect(rjsfSchema.properties.type.enum).to.be.eql(["dft"]); | ||
| expect(rjsfSchema.properties.type).to.have.property("enumNames"); | ||
| expect(rjsfSchema.properties.type.enumNames).to.be.eql(["Density Functional Theory"]); | ||
| expect(rjsfSchema).to.have.property("dependencies"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("Schema utility", () => { | ||
| const schemas = [ | ||
| ["string", { type: "string" }], | ||
| ["integer", { type: "integer" }], | ||
| ["number", { type: "number" }], | ||
| ["object", { type: "object" }], | ||
| ["array", { type: "array" }], | ||
| ]; | ||
| const objSchemaNoType = { properties: { name: { type: "string" } } }; | ||
| const arraySchemaNoType = { items: { type: "number" } }; | ||
| it("type can be determined correctly", () => { | ||
| schemas.forEach(([type, schema]) => { | ||
| const currentType = typeofSchema(schema); | ||
| expect(currentType).to.be.equal(type); | ||
| }); | ||
| expect(typeofSchema(objSchemaNoType)).to.be.equal("object"); | ||
| expect(typeofSchema(arraySchemaNoType)).to.be.equal("array"); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import { expect } from "chai"; | ||
|
|
||
| import { mapTree } from "../src/utils"; | ||
|
|
||
| describe("Tree data structure", () => { | ||
| const TREE = { | ||
| path: "/A", | ||
| children: [ | ||
| { | ||
| path: "/A/B", | ||
| children: [ | ||
| { | ||
| path: "/A/B/C", | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| path: "/A/D", | ||
| }, | ||
| ], | ||
| }; | ||
| it("map", () => { | ||
| const [mappedTree] = mapTree([TREE], (node) => { | ||
| return { ...node, foo: "bar" }; | ||
| }); | ||
| expect(mappedTree).to.have.property("foo", "bar"); | ||
| expect(mappedTree.children[0]).to.have.property("foo", "bar"); | ||
| expect(mappedTree.children[0].children[0]).to.have.property("foo", "bar"); | ||
| expect(mappedTree.children[1]).to.have.property("foo", "bar"); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's better to load only lodash functions that we actually need in the module, not a whole lodash library.
https://stackoverflow.com/questions/35250500/correct-way-to-import-lodash
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good point! I adjusted it for all
lodashimports in the repoThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@k0stik - the idea is good, provided that readability doesn't suffer, but let's deal with it in a separate task, pls see #40 (comment)