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

feat(functions): add releases:all() #277

Merged
merged 1 commit into from
Mar 6, 2025
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
19 changes: 19 additions & 0 deletions src/evaluator/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,24 @@ sanity['partOfRelease'] = async function (args, scope, execute) {
}
sanity['partOfRelease'].arity = 1

const releases: FunctionSet = {}

// eslint-disable-next-line require-await
releases['all'] = async function (_args, scope) {
const allReleases: string[] = []
for await (const value of scope.source) {
if (getType(value) === 'object') {
const val = await value.get()
if (val && '_type' in val && val._type === 'system.release') {
allReleases.push(val)
}
}
}

return fromJS(allReleases)
}
releases['all'].arity = 0

export type GroqPipeFunction = (
base: Value,
args: ExprNode[],
Expand Down Expand Up @@ -762,4 +780,5 @@ export const namespaces: NamespaceSet = {
sanity,
math,
dateTime,
releases,
}
15 changes: 15 additions & 0 deletions test/evaluate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,21 @@ t.test('Basic parsing', async (t) => {
})
})

t.test('releases-functions', async (t) => {
t.test('releases::all()', async (t) => {
const dataset = [
{_id: '_.releases.summer', _type: 'system.release', title: 'Summer'},
{_id: '_.releases.winter', _type: 'system.release', title: 'Winter'},
{_id: '_.releases.not', _type: 'other', title: 'Not a release'},
]

const tree = parse('{"rels": releases::all()[].title}')
const value = await evaluate(tree, {dataset})
const data = await value.get()
t.same(data, {rels: ['Summer', 'Winter']})
})
})

t.test('Custom dereference function', async (t) => {
const dataset = [
{_id: 'a', name: 'Michael'},
Expand Down