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: theme finders supports the reactive API #2695

Merged
merged 5 commits into from Nov 17, 2022

Conversation

guqing
Copy link
Member

@guqing guqing commented Nov 11, 2022

What type of PR is this?

/kind feature
/area core
/milestone 2.0

What this PR does / why we need it:

将所有 Finder 的返回值都修改为 Mono 或 Flux

Which issue(s) this PR fixes:

Fixes #2671

Special notes for your reviewer:

how to test it?
切换几个主题都点一下,没有报错即为正常

/cc @halo-dev/sig-halo

Does this PR introduce a user-facing change?

主题端 Finder 支持 Reactive API

@f2c-ci-robot f2c-ci-robot bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. kind/feature Categorizes issue or PR as related to a new feature. labels Nov 11, 2022
@f2c-ci-robot f2c-ci-robot bot added this to the 2.0 milestone Nov 11, 2022
@f2c-ci-robot f2c-ci-robot bot added the area/core Issues or PRs related to the Halo Core label Nov 11, 2022
Copy link
Member

@ruibaby ruibaby left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

主题各个页面的访问表现正常。

/cc @halo-dev/sig-halo

@f2c-ci-robot f2c-ci-robot bot added the lgtm Indicates that a PR is ready to be merged. label Nov 15, 2022
Copy link
Member

@JohnNiang JohnNiang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议提供一些关于自定义SpelExpression 的参考文档。

@guqing
Copy link
Member Author

guqing commented Nov 16, 2022

建议提供一些关于自定义SpelExpression 的参考文档。

实现思路:
当 thymeleaf 想解析一个表达式例如: ${menuFinder.getPrimary().menuItems}
会调用 Thymeleaf 实现的 SPELVariableExpressionEvaluator 来解析,我们可以 delegate 一下它的到返回值之后如果是 Mono 或 Flux 则 block 后在返回表达式解析结果,但这样只能解决例如 ${menuFinder.getPrimary()} 这样的表达式,此时 getPrimary 返回值为 Mono,delegate 后可以在页面的到想要的值,但是再继续链式调用 menuItems 就不再可行了因为是一个 Mono

为了让链式调用可行,可以模仿 MapAccessor 或 JsonPropertyAccessor,它们都是干预表达式解析后 evaluation 值的过程,比如 JsonPropertyAccessor 可以让 SPEL 表达式可以访问 jackson JsonNode 的值,例如有表达式 ${site.url}, site 是一个JsonNode, 当 AST Node 计算到 site.url 时词法分析前已经知道 site 为对象,url 为对象属性,所以使用 PropertyAccessor取值,找到支持 JsonNode 对象的 Accessor(canRead方法) 得到 JsonPropertyAccessor 然后使用它取值read(EvaluationContext ctx, Object target, String name),它会从 EvaluationContext 的到 site 即 JsonNode 然后调用 get(name) 即 site.get("url") 的到结果。
同样的思路,只需增加一个 PropertyAccessor 用于在遇到 Mono 或 Flux 时获取到其真实值包装为 TypedValue 会被放到 EvaluationContext 中然后即可被 SPELExpressionParser 继续解析而不报错,最终 SPELExpression#getValue() 就的到了正确的结果。

相关文档
Spring expressions-evaluation

可供参考以下源码:

Spring

Copy link
Member

@JohnNiang JohnNiang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/approve

@f2c-ci-robot
Copy link

f2c-ci-robot bot commented Nov 17, 2022

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: JohnNiang

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@f2c-ci-robot f2c-ci-robot bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Nov 17, 2022
@f2c-ci-robot f2c-ci-robot bot merged commit 9f53cd1 into halo-dev:main Nov 17, 2022
@guqing guqing deleted the refactor/2671 branch November 17, 2022 02:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. area/core Issues or PRs related to the Halo Core kind/feature Categorizes issue or PR as related to a new feature. lgtm Indicates that a PR is ready to be merged. release-note Denotes a PR that will be considered when it comes time to generate release notes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

主题端 Finder 支持 Reactive API
3 participants