move visibility decorators into lib

This commit is contained in:
Brian Terlson 2021-02-12 13:29:30 -08:00
Родитель 6fd5cb1757
Коммит 4709e4db1f
1 изменённых файлов: 29 добавлений и 1 удалений

Просмотреть файл

@ -1,5 +1,5 @@
import { Program } from "../compiler/program";
import { Type } from "../compiler/types";
import { ModelTypeProperty, Type } from "../compiler/types";
const docs = new Map<Type, string>();
@ -151,6 +151,34 @@ export function getVisibility(target: Type): string | undefined {
return visibilitySettings.get(target);
}
export function withVisibility(
program: Program,
target: Type,
...visibilities: string[]
) {
if (target.kind !== "Model") {
throw new Error("The @withVisibility decorator can only be applied to models.");
}
const filter = (_: any, prop: ModelTypeProperty) => {
const vis = getVisibility(prop);
return vis ? !visibilities.includes(vis) : false;
};
mapFilterOut(target.properties, filter);
}
function mapFilterOut(
map: Map<string, ModelTypeProperty>,
pred: (key: string, prop: ModelTypeProperty) => boolean
) {
for (const [key, prop] of map) {
if (pred(key, prop)) {
map.delete(key);
}
}
}
// -- @list decorator ---------------------
const listProperties = new Set<Type>();