Skip to content

Commit

Permalink
wip(core): 🚧 refactor oas transfomer2, and core TypeResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
charlzyx committed Dec 13, 2024
1 parent e21a0a8 commit bf6a8dc
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 36 deletions.
13 changes: 1 addition & 12 deletions changelog.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
"tagMessage": "v{{newVersion}}"
},
"types": {
"build": {
"semver": "patch",
"title": "📦 构建相关 / Build"
},
"chore": {
"title": "🏡 杂务处理 / Chore"
},
Expand All @@ -19,9 +15,6 @@
"semver": "patch",
"title": "📖 文档更新 / Documentation"
},
"examples": {
"title": "🏀 示例更新 / Examples"
},
"feat": {
"semver": "minor",
"title": "🚀 增强功能 / Enhancements"
Expand All @@ -44,12 +37,8 @@
"test": {
"title": "✅ 测试用例 / Tests"
},
"types": {
"semver": "patch",
"title": "🌊 类型定义 / Types"
},
"wip": {
"title": "🚧 未完成 / Work in Progress"
}
}
}
}
33 changes: 13 additions & 20 deletions packages/core/src/TypeResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,14 @@ type NodeResolver<ResolvedNode, Context> = (
self: TypeResolver<ResolvedNode, Context>
) => ResolvedNode;

type NodeResolverHook<ResolvedNode, Context> = (
type: Type,
ctx: Context,
self: TypeResolver<ResolvedNode, Context>
) => false | undefined | ResolvedNode;

export class TypeResolver<ResolvedNode, Context> {
// 用于处理类型的集合,存储映射
private mappings: {
[key: string]: NodeResolverHook<ResolvedNode, Context>;
[key: string]: NodeResolver<ResolvedNode, Context>;
} = {};

private hooks = {
before: null as NodeResolverHook<ResolvedNode, Context>,
before: null as NodeResolver<ResolvedNode, Context>,
};

// 在每次解析之前都会调用, 返回 false 会阻止 resovler 调用
Expand All @@ -55,21 +49,20 @@ export class TypeResolver<ResolvedNode, Context> {
});

const before = this.hooks.before;
const next = before && before(type, ctx, this);
if (next === false) return undefined;
if (next != undefined) return next as ResolvedNode;
const next = before?.(type, ctx, this);
if (next) {
return next;
}

for (const key of keys) {
const isType = "is" + key.replace(/^\w/, (m) => m.toUpperCase());
const keyType = keys.find((typeName) => {
const isType = "is" + typeName.replace(/^\w/, (m) => m.toUpperCase());
const isMatch = type[isType]?.();
if (!isMatch) return;
const resolver = this.mappings[key];
const ans = resolver(type, ctx, this);
if (ans) {
return ans;
}
return isMatch;
});
const resolver = this.mappings[keyType];
if (resolver) {
return resolver(type, ctx, this);
}
return undefined; // 如果没有匹配的 resolver,返回 undefined
}
// 获取匿名类型
anonymous(
Expand Down
16 changes: 12 additions & 4 deletions packages/oas/src/transformer2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,34 +58,42 @@ const schemaResolver = new TypeResolver<OutputSchema, ResolveContext>()
.null(baseResolve("null"))
.array((type, ctx, resovler) => {
ctx.refs.set(type, type.getText());
const extra = ctx.extra;
ctx.extra = {};
return {
...ctx.extra,
...extra,
type: "array",
items: resovler.resolve(type.getArrayElementType(), ctx),
};
})
.union((type, ctx, resolver) => {
ctx.refs.set(type, type.getText());
const extra = ctx.extra;
ctx.extra = {};
return {
...ctx.extra,
...extra,
oneOf: type
.getUnionTypes()
.map((subType) => resolver.resolve(subType, ctx)),
};
})
.intersection((type, ctx, resolver) => {
ctx.refs.set(type, type.getText());
const extra = ctx.extra;
ctx.extra = {};
return {
...ctx.extra,
...extra,
allOf: type
.getIntersectionTypes()
.map((subType) => resolver.resolve(subType, ctx)),
};
})
.object((type, ctx, resolver) => {
ctx.refs.set(type, type.getText());
const extra = ctx.extra;
ctx.extra = {};
return {
...ctx.extra,
...extra,
type: "object",
properties: type.getProperties().reduce((map, propSymbol) => {
const propNode =
Expand Down

0 comments on commit bf6a8dc

Please sign in to comment.