Repository navigation
POC: 脚本字段持久化 - 业务表存 key、领域对象存正文 (17.x) #235
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b1d18fb
feat: 新增 GroovyScriptRef 抽象脚本 key 生命周期 POC (17.x/springboot-starter-s…
xlorne 2fb0afb
feat: 补齐脚本字段落库 POC,演示 DB 存 key、业务对象拿正文 (17.x/springboot-starter-script)
xlorne 2eb1e98
refactor: POC 按评审意见重构,key 生命周期下沉到持久化层 (17.x/springboot-starter-script)
xlorne e78a426
revert: 撤销框架侧 GroovyScript 与 pom 改动,POC 不侵入框架 (17.x/springboot-starte…
xlorne 31015b7
feat: 脚本字段改为持久化对象并由框架自动转换 (17.x/springboot-starter-script)
xlorne 80fb8ee
feat: 抽出脚本持久化仓库抽象与 key 策略上下文 (17.x/springboot-starter-script)
xlorne File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
...r-script/src/main/java/com/codingapi/springboot/script/persistence/GroovyScriptField.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| package com.codingapi.springboot.script.persistence; | ||
|
|
||
| import lombok.Getter; | ||
|
|
||
| /** | ||
| * 脚本持久化对象:实体字段上是脚本对象,数据库列上是 key | ||
| * | ||
| * <p>实体字段声明为 {@code GroovyScriptField} 即可被 {@link GroovyScriptFieldConverter} 自动转换, | ||
| * 业务侧不需要在转换器里处理 key,也不会因为字段是普通 String 而漏掉处理。</p> | ||
| * | ||
| * <p>key 生命周期:</p> | ||
| * <ul> | ||
| * <li>业务已指定 key:{@link #of(String, String)},落库直接使用业务 key;</li> | ||
| * <li>业务未指定 key:{@link #of(String)},落库时由策略生成;</li> | ||
| * <li>更新:{@link #carry(GroovyScriptField, String)} 沿用上一版本的 key,只替换正文。</li> | ||
| * </ul> | ||
| */ | ||
| @Getter | ||
| public class GroovyScriptField { | ||
|
|
||
| /** | ||
| * 脚本唯一编码,null 表示尚未落库 | ||
| */ | ||
| private String key; | ||
|
|
||
| /** | ||
| * 脚本正文 | ||
| */ | ||
| private String content; | ||
|
|
||
| private GroovyScriptField(String key, String content) { | ||
| this.key = key; | ||
| this.content = content; | ||
| } | ||
|
|
||
| /** | ||
| * 新建脚本(key 由框架按策略生成) | ||
| * | ||
| * @param content 脚本正文 | ||
| * @return 脚本持久化对象 | ||
| */ | ||
| public static GroovyScriptField of(String content) { | ||
| return new GroovyScriptField(null, content); | ||
| } | ||
|
|
||
| /** | ||
| * 业务指定 key 的脚本(或从库中还原) | ||
| * | ||
| * @param key 脚本唯一编码 | ||
| * @param content 脚本正文 | ||
| * @return 脚本持久化对象 | ||
| */ | ||
| public static GroovyScriptField of(String key, String content) { | ||
| return new GroovyScriptField(key, content); | ||
| } | ||
|
|
||
| /** | ||
| * 沿用上一版本已持久化的 key,替换脚本正文 | ||
| * | ||
| * @param previous 上一版本,null 表示新建 | ||
| * @param content 新的脚本正文 | ||
| * @return 脚本持久化对象 | ||
| */ | ||
| public static GroovyScriptField carry(GroovyScriptField previous, String content) { | ||
| if (previous == null) { | ||
| return of(content); | ||
| } | ||
| return new GroovyScriptField(previous.key, content); | ||
| } | ||
|
|
||
| /** | ||
| * 是否尚未落库 | ||
| */ | ||
| public boolean isNew() { | ||
| return this.key == null; | ||
| } | ||
|
|
||
| /** | ||
| * 落库:key 为空时由 {@link GroovyScriptKeyGeneratorContext} 生成,非空时复用同一 key 覆盖内容 | ||
| * | ||
| * @return 脚本 key | ||
| */ | ||
| public String persist() { | ||
| this.key = GroovyScriptPersistenceContext.getInstance().save(this.key, this.content); | ||
| return this.key; | ||
| } | ||
| } |
46 changes: 46 additions & 0 deletions
46
...src/main/java/com/codingapi/springboot/script/persistence/GroovyScriptFieldConverter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package com.codingapi.springboot.script.persistence; | ||
|
|
||
| import jakarta.persistence.AttributeConverter; | ||
| import jakarta.persistence.Converter; | ||
|
|
||
| /** | ||
| * 脚本字段转换器:数据库列存 key,实体属性是脚本持久化对象 | ||
| * | ||
| * <p>标注 {@code autoApply = true},实体字段只要是 {@link GroovyScriptField} 类型即自动生效, | ||
| * 业务侧无需为每个字段写转换代码。</p> | ||
| * | ||
| * <p>key 的生成、复用与内容存取都委托给 {@link GroovyScriptPersistenceContext}, | ||
| * 因此非 JPA 的持久化实现可以复用同一套抽象,而不是绑死在 JPA 上。</p> | ||
| */ | ||
| @Converter(autoApply = true) | ||
| public class GroovyScriptFieldConverter implements AttributeConverter<GroovyScriptField, String> { | ||
|
|
||
| /** | ||
| * 落库:脚本持久化对象转换为 key | ||
| * | ||
| * @param attribute 脚本持久化对象 | ||
| * @return 脚本 key | ||
| */ | ||
| @Override | ||
| public String convertToDatabaseColumn(GroovyScriptField attribute) { | ||
| if (attribute == null) { | ||
| return null; | ||
| } | ||
| return attribute.persist(); | ||
| } | ||
|
|
||
| /** | ||
| * 读取:key 还原为脚本持久化对象 | ||
| * | ||
| * @param dbData 脚本 key | ||
| * @return 脚本持久化对象 | ||
| */ | ||
| @Override | ||
| public GroovyScriptField convertToEntityAttribute(String dbData) { | ||
| if (dbData == null) { | ||
| return null; | ||
| } | ||
| String content = GroovyScriptPersistenceContext.getInstance().getScript(dbData); | ||
| return GroovyScriptField.of(dbData, content); | ||
| } | ||
| } | ||
27 changes: 27 additions & 0 deletions
27
...t/src/main/java/com/codingapi/springboot/script/persistence/GroovyScriptKeyGenerator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package com.codingapi.springboot.script.persistence; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| /** | ||
| * 脚本 key 生成策略 | ||
| * | ||
| * <p>业务可以先自行决定 key({@link GroovyScriptField#of(String, String)}), | ||
| * 只有业务未指定 key 时才由该策略兜底。默认 UUID。</p> | ||
| */ | ||
| @FunctionalInterface | ||
| public interface GroovyScriptKeyGenerator { | ||
|
|
||
| /** | ||
| * 生成脚本 key | ||
| * | ||
| * @return 脚本唯一编码 | ||
| */ | ||
| String generate(); | ||
|
|
||
| /** | ||
| * 默认策略:UUID(去掉分隔符) | ||
| */ | ||
| static GroovyScriptKeyGenerator uuid() { | ||
| return () -> UUID.randomUUID().toString().replace("-", ""); | ||
| } | ||
| } |
38 changes: 38 additions & 0 deletions
38
...ain/java/com/codingapi/springboot/script/persistence/GroovyScriptKeyGeneratorContext.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package com.codingapi.springboot.script.persistence; | ||
|
|
||
| import lombok.Getter; | ||
|
|
||
| /** | ||
| * 脚本 key 生成策略上下文 | ||
| * | ||
| * <p>业务在启动时通过 {@link #setGenerator(GroovyScriptKeyGenerator)} 注入自己的策略, | ||
| * 未注入时使用默认 UUID 策略。</p> | ||
| */ | ||
| public class GroovyScriptKeyGeneratorContext { | ||
|
|
||
| @Getter | ||
| private final static GroovyScriptKeyGeneratorContext instance = new GroovyScriptKeyGeneratorContext(); | ||
|
|
||
| private GroovyScriptKeyGenerator generator = GroovyScriptKeyGenerator.uuid(); | ||
|
|
||
| private GroovyScriptKeyGeneratorContext() { | ||
| } | ||
|
|
||
| /** | ||
| * 替换 key 生成策略,入参为 null 时回退为默认 UUID 策略 | ||
| * | ||
| * @param generator key 生成策略 | ||
| */ | ||
| public void setGenerator(GroovyScriptKeyGenerator generator) { | ||
| this.generator = (generator == null) ? GroovyScriptKeyGenerator.uuid() : generator; | ||
| } | ||
|
|
||
| /** | ||
| * 生成脚本 key | ||
| * | ||
| * @return 脚本唯一编码 | ||
| */ | ||
| public String generate() { | ||
| return this.generator.generate(); | ||
| } | ||
| } |
50 changes: 50 additions & 0 deletions
50
...main/java/com/codingapi/springboot/script/persistence/GroovyScriptPersistenceContext.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package com.codingapi.springboot.script.persistence; | ||
|
|
||
| import lombok.Getter; | ||
|
|
||
| /** | ||
| * 脚本持久化仓库上下文 | ||
| * | ||
| * <p>默认使用 {@link GroovyScriptRepositoryImpl};非 JPA 的持久化实现(文档库、缓存库等) | ||
| * 可以通过 {@link #setRepository(GroovyScriptRepository)} 替换为自己的实现。</p> | ||
| */ | ||
| public class GroovyScriptPersistenceContext { | ||
|
|
||
| @Getter | ||
| private final static GroovyScriptPersistenceContext instance = new GroovyScriptPersistenceContext(); | ||
|
|
||
| private GroovyScriptRepository repository = new GroovyScriptRepositoryImpl(); | ||
|
|
||
| private GroovyScriptPersistenceContext() { | ||
| } | ||
|
|
||
| /** | ||
| * 替换脚本持久化仓库实现,入参为 null 时回退为默认实现 | ||
| * | ||
| * @param repository 脚本持久化仓库 | ||
| */ | ||
| public void setRepository(GroovyScriptRepository repository) { | ||
| this.repository = (repository == null) ? new GroovyScriptRepositoryImpl() : repository; | ||
| } | ||
|
|
||
| /** | ||
| * 保存脚本正文 | ||
| * | ||
| * @param key 已有 key,null 表示新建 | ||
| * @param content 脚本正文 | ||
| * @return 最终使用的 key | ||
| */ | ||
| public String save(String key, String content) { | ||
| return this.repository.save(key, content); | ||
| } | ||
|
|
||
| /** | ||
| * 按 key 读取脚本正文 | ||
| * | ||
| * @param key 脚本 key | ||
| * @return 脚本正文,不存在时返回空串 | ||
| */ | ||
| public String getScript(String key) { | ||
| return this.repository.getScript(key); | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
...ipt/src/main/java/com/codingapi/springboot/script/persistence/GroovyScriptRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package com.codingapi.springboot.script.persistence; | ||
|
|
||
| /** | ||
| * 脚本持久化仓库抽象:对外只暴露「脚本正文」,内部负责 key 的生成、复用与存储 | ||
| * | ||
| * <p>JPA 场景由 {@link GroovyScriptFieldConverter} 委托本仓库实现; | ||
| * 非 JPA 场景(文档库、缓存库、自定义表结构等)可直接通过 | ||
| * {@link GroovyScriptPersistenceContext} 使用同一套能力,无需自己处理 key。</p> | ||
| */ | ||
| public interface GroovyScriptRepository { | ||
|
|
||
| /** | ||
| * 保存脚本正文 | ||
| * | ||
| * @param key 已有 key,null 表示新建 | ||
| * @param content 脚本正文 | ||
| * @return 最终使用的 key | ||
| */ | ||
| String save(String key, String content); | ||
|
|
||
| /** | ||
| * 按 key 读取脚本正文 | ||
| * | ||
| * @param key 脚本 key | ||
| * @return 脚本正文,不存在时返回空串 | ||
| */ | ||
| String getScript(String key); | ||
| } |
43 changes: 43 additions & 0 deletions
43
...src/main/java/com/codingapi/springboot/script/persistence/GroovyScriptRepositoryImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package com.codingapi.springboot.script.persistence; | ||
|
|
||
| import com.codingapi.springboot.script.GroovyScript; | ||
| import com.codingapi.springboot.script.repository.GroovyScriptRepositoryContext; | ||
|
|
||
| /** | ||
| * 默认脚本持久化仓库实现:基于脚本仓储完成正文与 key 的映射 | ||
| * | ||
| * <p>key 生命周期:key 为空时由 {@link GroovyScriptKeyGeneratorContext} 生成,非空时复用并覆盖内容。</p> | ||
| */ | ||
| public class GroovyScriptRepositoryImpl implements GroovyScriptRepository { | ||
|
|
||
| /** | ||
| * 保存脚本正文 | ||
| * | ||
| * @param key 已有 key,null 表示新建 | ||
| * @param content 脚本正文 | ||
| * @return 最终使用的 key | ||
| */ | ||
| @Override | ||
| public String save(String key, String content) { | ||
| String targetKey = (key == null || key.isBlank()) | ||
| ? GroovyScriptKeyGeneratorContext.getInstance().generate() | ||
| : key; | ||
| GroovyScript.builder(targetKey).script(content).build().save(); | ||
| return targetKey; | ||
| } | ||
|
|
||
| /** | ||
| * 按 key 读取脚本正文 | ||
| * | ||
| * @param key 脚本 key | ||
| * @return 脚本正文,不存在时返回空串 | ||
| */ | ||
| @Override | ||
| public String getScript(String key) { | ||
| if (key == null || key.isBlank()) { | ||
| return ""; | ||
| } | ||
| GroovyScript groovyScript = GroovyScriptRepositoryContext.getInstance().get(key); | ||
| return groovyScript == null ? "" : groovyScript.getScript(); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GroovyScriptFieldConverter 再jpa下是可以使用的,但是考虑到业务模块的持久化未必是通过jpa实现的。那就可能需要提供抽象的GroovyScriptRepository来实现了
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已解耦:Converter 只做 JPA 适配,转换与 key 生命周期委托 GroovyScriptRepository 抽象(GroovyScriptPersistenceContext 持有,可替换)。新增用例演示非 JPA 场景(模拟文档库存 key)直接复用同一抽象完成内容存取与 key 复用。