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

Schema for Expressions #718

Open
wants to merge 47 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
523859b
Replace specs for expressions with shared library
bkeepers Mar 30, 2023
10fa1b4
Move expressions schemas into this repo
bkeepers Mar 30, 2023
a72ae35
Make Ruby 2.6 happy
bkeepers Mar 30, 2023
87c3de4
Use vitest to run tests
bkeepers Mar 31, 2023
0d065b5
Shuffle around somethings in schema.json and rename defs
bkeepers Mar 31, 2023
ee46fab
Add Min/Max functions
bkeepers Mar 31, 2023
6a2e622
Rename url-unfriendly defs
bkeepers Mar 31, 2023
08b03c1
Move expression examples out of test dir
bkeepers Mar 31, 2023
5004b6f
Initial docs for implementing functions
bkeepers Mar 31, 2023
4c35f3e
Run JS lint/tests
bkeepers Mar 31, 2023
c51eda3
Add not about running tests to expression docs
bkeepers Mar 31, 2023
50dc917
Fix README formatting
bkeepers Mar 31, 2023
6262795
Remove use of path
bkeepers Apr 1, 2023
4a10eaf
Simplfy schemas by making implementation cast function arguments to a…
bkeepers Apr 1, 2023
48308dd
Add json_schemer as a dependency
bkeepers Apr 1, 2023
81a1d65
Fix JS lint
bkeepers Apr 1, 2023
b8279c5
Validate schemas in strict mode
bkeepers Apr 2, 2023
270f290
$defs => definitions for draft 7
bkeepers Apr 3, 2023
8f69da4
Build sourcemaps
bkeepers Apr 10, 2023
a498b3a
Add explorer to inspect information about the schema
bkeepers Apr 4, 2023
a2c501a
Add simple js expression model
bkeepers Apr 4, 2023
a3b2076
Add validate method to Expression/Constant
bkeepers Apr 5, 2023
ad07179
Add simple schema class to browse and validate schemas
bkeepers Apr 8, 2023
d96ec78
Add proxying back to schema to resolve refs
bkeepers Apr 9, 2023
d134ae6
Add/Subtract/Multiply/Divide functions
bkeepers Apr 10, 2023
b4e7035
Fix bug where ajv eagerly resolves ref in array items
bkeepers Apr 10, 2023
7c5016b
use exported Schema class where possible
bkeepers Apr 10, 2023
26a08f3
Pair schema with expression and constant
bkeepers Apr 12, 2023
82cf6ac
Add operator keyword to schemas
bkeepers Apr 12, 2023
3f6d241
Allow null as a constant
bkeepers Jul 17, 2023
cfecaf5
Specify json_schemer version number
bkeepers Jul 17, 2023
e3b32c4
JS: Define expression.add(…) to build new expression
bkeepers Jul 19, 2023
0848411
Add parent to expressions, refactor
bkeepers Jul 25, 2023
639d2a7
Use external package for schemas
bkeepers Oct 12, 2023
8aa356c
Merge branch 'main' into expressions-schema
bkeepers Oct 12, 2023
6512d63
Add flipper-expressions-schema as separate gem for now
bkeepers Oct 12, 2023
21a0806
Merge remote-tracking branch 'origin/main' into expressions-schema
bkeepers Oct 12, 2023
9725f56
Ensure schemas are downloaded
bkeepers Oct 12, 2023
8e1bfb1
Ensure node is setup before bundling
bkeepers Oct 12, 2023
7fc02ff
Revert renaming of ci job
bkeepers Oct 12, 2023
15099b9
English properly
bkeepers Oct 12, 2023
6640afc
Don't auth to install npm/gems from github
bkeepers Oct 12, 2023
ed90c55
Try using the gem post_install hook to avoid issue with GitHub Actions
bkeepers Oct 13, 2023
6350f0e
Remove actions/cache, just rely on ruby/node actions for caching
bkeepers Oct 13, 2023
b36dd09
Run npm install before setting up ruby
bkeepers Oct 13, 2023
5470e59
Rename expressions repo
bkeepers Oct 13, 2023
afb896a
Merge remote-tracking branch 'origin/main' into expressions-schema
bkeepers Nov 10, 2023
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
Prev Previous commit
Next Next commit
Add simple js expression model
bkeepers committed Apr 10, 2023

Verified

This commit was signed with the committer’s verified signature.
gorhill Raymond Hill
commit a2c501ac45eb690d7f0f3299419ce9b507e798c2
12 changes: 12 additions & 0 deletions packages/expressions/lib/constant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Public: A constant value like a "string", number (1, 3.5), or boolean (true, false).
//
// Implements the same interface as Expression
export class Constant {
constructor (value) {
this.value = value
}

get args () {
return [this.value]
}
}
3 changes: 3 additions & 0 deletions packages/expressions/lib/explorer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import schemas, { BaseURI } from '../schemas'
import pointer from 'json-pointer'

export class Explorer {
@@ -40,3 +41,5 @@ export class Explorer {
return this.get('#/definitions/function/properties')
}
}

export default new Explorer(schemas, BaseURI)
37 changes: 37 additions & 0 deletions packages/expressions/lib/expression.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { v4 as uuidv4 } from 'uuid'
import { Constant } from './constant'
import explorer from './explorer'

// Simple model to transform this: `{ All: [{ Boolean: [true] }]`
// into this: `{ id: uuidv4(), name: 'All', args: [{ id: uuidv4(), name: 'Boolean', args: [true] }] }`
export class Expression {
static build (expression) {
if (expression instanceof Expression || expression instanceof Constant) {
return expression
}

if (typeof expression === 'object') {
const name = Object.keys(expression)[0]
const args = expression[name].map(Expression.build)
return new Expression({ name, args })
} else {
return new Constant(expression)
}
}

constructor ({ name, args, id = uuidv4() }) {
Object.assign(this, { name, args, id })
}

clone ({ id = this.id, name = this.name, args = this.args } = {}) {
return new Expression({ id, name, args: args.map(Expression.build) })
}

get value () {
return { [this.name]: this.args.map(arg => arg.value) }
}

get schema () {
return explorer.functions[this.name]
}
}
9 changes: 4 additions & 5 deletions packages/expressions/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import schemas, { BaseURI } from '../schemas'
import { Explorer } from './explorer'

export const explorer = new Explorer(schemas, BaseURI)
export { schemas }
export { default as schemas, BaseURI } from '../schemas'
export { default as examples } from '../examples'
export { default as explorer } from './explorer'
export { default as validate } from './validate'
export { Expression } from './expression'
export { Constant } from './constant'
3 changes: 2 additions & 1 deletion packages/expressions/package.json
Original file line number Diff line number Diff line change
@@ -21,7 +21,8 @@
"dependencies": {
"ajv": "^8.12.0",
"ajv-formats": "^2.1.1",
"json-pointer": "^0.6.2"
"json-pointer": "^0.6.2",
"uuid": "^9.0.0"
},
"devDependencies": {
"standard": "^17.0.0",
32 changes: 32 additions & 0 deletions packages/expressions/test/expression.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { describe, test, expect } from 'vitest'
import { Expression, Constant } from '../lib'

describe('Expression', () => {
describe('build', () => {
test('builds an expression from an object', () => {
const expression = Expression.build({ All: [true] })
expect(expression.name).toEqual('All')
expect(expression.args[0]).toBeInstanceOf(Constant)
expect(expression.args[0].value).toEqual(true)
expect(expression.value).toEqual({ All: [true] })
})
})

describe('clone', () => {
test('returns new expression', () => {
const expression = Expression.build({ All: [true] })
const clone = expression.clone()
expect(clone).not.toBe(expression)
expect(clone.name).toEqual(expression.name)
expect(clone.args).toEqual(expression.args)
expect(clone.id).toEqual(expression.id)
})

test('builds args', () => {
const expression = Expression.build({ All: [] })
const clone = expression.clone({ args: [true] })
expect(clone.args[0]).toBeInstanceOf(Constant)
expect(clone.value).toEqual({ All: [true] })
})
})
})
5 changes: 5 additions & 0 deletions packages/expressions/yarn.lock
Original file line number Diff line number Diff line change
@@ -2005,6 +2005,11 @@ uri-js@^4.2.2:
dependencies:
punycode "^2.1.0"

uuid@^9.0.0:
version "9.0.0"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-9.0.0.tgz#592f550650024a38ceb0c562f2f6aa435761efb5"
integrity sha512-MXcSTerfPa4uqyzStbRoTgt5XIe3x5+42+q1sDuy3R5MDk66URdLMOZe5aPX/SQd+kuYAh0FdP/pO28IkQyTeg==

[email protected]:
version "0.29.8"
resolved "https://registry.yarnpkg.com/vite-node/-/vite-node-0.29.8.tgz#6a1c9d4fb31e7b4e0f825d3a37abe3404e52bd8e"