CacheControl / CacheControl/json-rules-engine

Feat: Type-safety using a FactTypeMapping

Abierto
#342 1 comentario 1 reacción 0 asignados Ver en GitHub
Lenguaje dominante
JavaScript
Estrellas
3.1k
Forks
507
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

We're doing a quick POC and looking at using this library. One thing that I'd love to see is the ability to enforce or at least make consistent the concept of fact types. Something along the lines of:
```typescript
type Dictionary = {
userId: number;
};

const engine = rulesEngine([]);
engine.addFact("userId", 1);
// Not Valid:
// engine.addFact("userId", false);
expectType>(engine.getFact("userId"));
expectType>(engine.getFact("other"));

engine.addFact("userId", (params, almanac) => {
expectType>(almanac);
expectType>(almanac.factValue("userId"));
expectType>(almanac.factValue("other"));
return 43;
});
```

You get the idea. Let users specify a dictionary of facts whose type is known ahead of time and fixed. Today, this is kind of done by just letting the user set the type of the fact value, but it doesn't carry through the system.

I took an initial stab at doing this which is below, but the issue is that this would require a breaking type change.

Today since users can specify the type `almanac.factValue('userId')`. But in order to infer types, we'd need to template the key-type as well, giving us two generics. TS doesn't like having partially filled out generics...so we hit an impasse: if we want to allow user overriding of the types, then we'll need to ask people to specify `` where before it was just ``.

Of course the ideal scenario is that they remove the template together and just pass a dictionary of types on engine construction; changing this usage would be smoothest by default, but they could still override the type by casting through any (or we could make any the default instead of unknown).

Here's the new ts file, complete with changes if anyone would like to play around further or discuss more.

View Code

```typescript
export interface EngineOptions {
allowUndefinedFacts?: boolean;
allowUndefinedConditions?: boolean;
pathResolver?: PathResolver;
}

export interface EngineResult {
events: Event[];
failureEvents: Event[];
almanac: Almanac;
results: RuleResult[];
failureResults: RuleResult[];
}

export default function engineFactory<
FactTypeDictionary extends Record = {}
>(
rules: Array>,
options?: EngineOptions
): Engine;

// This helper gives us optionality. If the key is in the dictionary, then we return the type,
// otherwise we return the default type which is unknown (unless the user specifies it on the call itself)
type FactReturn<
Dictionary,
Key,
Default = unknown
> = Key extends keyof Dictionary ? Dictionary[Key] : Default;

export class Engine = {}> {
constructor(
rules?: Array>,
options?: EngineOptions
);

addRule(rule: RuleProperties): this;
removeRule(ruleOrName: Rule | string): boolean;
updateRule(rule: Rule): void;

setCondition(name: string, conditions: TopLevelCondition): this;
removeCondition(name: string): boolean;

addOperator(operator: Operator): Map;
addOperator(
operatorName: string,
callback: OperatorEvaluator
): Map;
removeOperator(operator: Operator | string): boolean;

addFact(fact: Fact): this;
addFact(
id: Key,
valueCallback:
| DynamicFactCallback<
FactReturn,
FactTypeDictionary
>
| FactReturn,
options?: FactOptions
): this;
removeFact(factOrId: string | Fact): boolean;
getFact(
factId: Key
): Fact, FactTypeDictionary>;

on(eventName: "success", handler: EventHandler): this;
on(eventName: "failure", handler: EventHandler): this;
on(eventName: string, handler: EventHandler): this;

// TODO: This run keyset should optionally reference the types from FactTypeDictionary
run(facts?: Record): Promise>;
stop(): this;
}

export interface OperatorEvaluator {
(factValue: A, compareToValue: B): boolean;
}

export class Operator {
public name: string;
constructor(
name: string,
evaluator: OperatorEvaluator,
validator?: (factValue: A) => boolean
);
}

export class Almanac {
// If a path is passed, then we don't have a good way to know the type anymore so we return unknown
factValue(
factId: Key,
params?: Record,
path?: Path
): Promise<
Path extends string ? unknown : FactReturn
>;
addRuntimeFact(
factId: Key,
value: FactReturn
): void;
}

export type FactOptions = {
cache?: boolean;
priority?: number;
};

export type DynamicFactCallback = (
params: Record,
almanac: Almanac
) => T;

export class Fact {
id: string;
priority: number;
options: FactOptions;
value?: T;
calculationMethod?: DynamicFactCallback;

constructor(
id: string,
value: T | DynamicFactCallback,
options?: FactOptions
);
}

export interface Event {
type: string;
params?: Record;
}

export type PathResolver = (value: object, path: string) => any;

export type EventHandler = (
event: Event,
almanac: Almanac,
ruleResult: RuleResult
) => void;

export interface RuleProperties {
conditions: TopLevelCondition;
event: Event;
name?: string;
priority?: number;
onSuccess?: EventHandler;
onFailure?: EventHandler;
}
export type RuleSerializable = Pick<
Required>,
"conditions" | "event" | "name" | "priority"
>;

export interface RuleResult {
name: string;
conditions: TopLevelCondition;
event?: Event;
priority?: number;
result: any;
}

// Something like a rule could be constructed outside of the context of an engine.
// For simplicity, we just default it to an empty dictionary since often the types won't come up in the rule definition
// (basically only if you were to attach an event, AND reference factValue from the almanac)
export class Rule = {}>
implements RuleProperties
{
constructor(ruleProps: RuleProperties | string);
name: string;
conditions: TopLevelCondition;
event: Event;
priority: number;
setConditions(conditions: TopLevelCondition): this;
setEvent(event: Event): this;
setPriority(priority: number): this;
toJSON(): string;
toJSON(
stringify: T
): T extends true ? string : RuleSerializable;
}

interface ConditionProperties {
fact: string;
operator: string;
value: { fact: string } | any;
path?: string;
priority?: number;
params?: Record;
name?: string;
}

type NestedCondition = ConditionProperties | TopLevelCondition;
type AllConditions = {
all: NestedCondition[];
name?: string;
priority?: number;
};
type AnyConditions = {
any: NestedCondition[];
name?: string;
priority?: number;
};
type NotConditions = { not: NestedCondition; name?: string; priority?: number };
type ConditionReference = {
condition: string;
name?: string;
priority?: number;
};
export type TopLevelCondition =
| AllConditions
| AnyConditions
| NotConditions
| ConditionReference;

```

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.