Skip to content

Commit

Permalink
feat(core): implement a way to extract used refs
Browse files Browse the repository at this point in the history
  • Loading branch information
binier committed Aug 2, 2020
1 parent 4a4ea08 commit 74a4515
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 2 deletions.
5 changes: 5 additions & 0 deletions packages/core/src/args/args-builder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Transformer } from '../utils';
import { OperatorBuilder } from '../operator';
import { Args, ArgsData } from './args';
import { extractRefs } from '../ref';

export interface ArgsBuilderData extends Omit<ArgsData, 'func'> {
func?: OperatorBuilder;
Expand All @@ -25,6 +26,10 @@ export class ArgsBuilder {
get offset() { return this.args.offset; }
get after() { return this.args.after; }

refs() {
return extractRefs(this.args);
}

length() {
return Object.values(this.args)
.filter(x => x)
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/directive/directive-builder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Transformer } from '../utils';
import { LogicalOperatorBuilder, OperatorBuilder } from '../operator';
import { DirectiveArgs, Directive } from './directive';
import { extractRefs } from '../ref';

export interface DirectiveBuilderArgs {
filter: LogicalOperatorBuilder | OperatorBuilder;
Expand All @@ -14,6 +15,10 @@ export class DirectiveBuilder<T extends keyof DirectiveBuilderArgs = any> {
private args: DirectiveBuilderArgs[T]
) { }

refs() {
return extractRefs(this.args);
}

build(transform: Transformer<DirectiveBuilderArgs[T], DirectiveArgs[T]>) {
return new Directive(this.name, transform(this.args));
}
Expand Down
12 changes: 12 additions & 0 deletions packages/core/src/edge/edge-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { ParamBuilder, paramNameGen, ParamNameGen } from '../param';
import { Edge } from './edge';
import { capitalize, RawProjection, Projection } from './common';
import { DirectiveBuilder } from '../directive';
import { Ref } from '../ref';

type OpBuilders = OperatorBuilder | LogicalOperatorBuilder;

Expand Down Expand Up @@ -195,6 +196,17 @@ export class EdgeBuilder {
return key;
}

/** @internal */
refs(): Ref[] {
return Object.values(this.edges)
.filter(x => x instanceof EdgeBuilder)
.reduce((r, x: EdgeBuilder) => r.concat(x.refs()), [])
.concat(this.args.refs())
.concat(Object.values(this.directives)
.reduce((r, x) => r.concat(x.refs()),
[]));
}

protected buildEdgeArgs(field: string, nameGen?: NameGenerators) {
nameGen = Object.assign(defaultNameGen(), nameGen);

Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/operator/logical-operator-builder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { LogicalOperator, LogicalOperatorArgs } from './logical-operator';
import { Transformer } from '../utils';
import { OperatorBuilder } from './operator-builder';
import { extractRefs } from '../ref';

export interface LogicalOperatorBuilderArgs
extends Omit<LogicalOperatorArgs, 'operators'>
Expand All @@ -16,6 +17,10 @@ type BuildTransformer = Transformer<
export class LogicalOperatorBuilder {
constructor(private args: LogicalOperatorBuilderArgs) { }

refs() {
return extractRefs(this.args.operators);
}

build(transform: BuildTransformer) {
return new LogicalOperator(transform(this.args));
}
Expand Down
8 changes: 7 additions & 1 deletion packages/core/src/operator/operator-builder.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Operator, OperatorArgs, OpValue, OpArg } from './operator';
import { Transformer } from '../utils';
import { ParamBuilder } from '../param';
import { Ref } from '../ref';

export type OpBuilderValue = ParamBuilder | OpValue;
export type OpBuilderValue = ParamBuilder | Ref | OpValue;
export type OpBuilderArg = ParamBuilder | OpArg;

export interface OperatorBuilderArgs
Expand All @@ -15,6 +16,11 @@ export interface OperatorBuilderArgs
export class OperatorBuilder {
constructor(private args: OperatorBuilderArgs) { }

refs(): Ref[] {
return [].concat(this.args.value, this.args.arg)
.filter(x => x instanceof Ref) as Ref[];
}

build(transform: Transformer<OperatorBuilderArgs, OperatorArgs>) {
return new Operator(transform(this.args));
}
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/operator/operator.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Param } from '../param';
import { Uid } from '../uid';
import { Ref } from '../ref';

export type Subject = string;
export type OpValue = Param | Uid | RegExp | Date
export type OpValue = Param | Ref | Uid | RegExp | Date
| string | number | boolean;
export type OpArg = Param | string | number | boolean;

Expand Down
12 changes: 12 additions & 0 deletions packages/core/src/ref/extract.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Ref } from './ref';

export function extractRefs(obj: any): Ref[] {
if (!obj) return [];
if (typeof obj['refs'] === 'function')
return obj.refs();
if (Array.isArray(obj))
return obj.reduce((r, x) => r.concat(extractRefs(x)), []);
if (typeof obj === 'object')
return extractRefs(Object.values(obj));
return [];
}
1 change: 1 addition & 0 deletions packages/core/src/ref/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './ref';
export * from './extract';

0 comments on commit 74a4515

Please sign in to comment.