Skip to content

Commit 9db1c10

Browse files
committed
feat: add code doc
1 parent debe4fb commit 9db1c10

1 file changed

Lines changed: 45 additions & 14 deletions

File tree

base/src/main/java/com/tinyengine/it/mcp/tools/GitFileReaderService.java

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
import java.util.*;
3939

4040
/**
41-
* GitFileReaderService
41+
* and processing them to create or update components and component libraries.
4242
*/
4343
@Service
4444
public class GitFileReaderService {
@@ -162,10 +162,11 @@ public String readFileFromRepo(String url) {
162162

163163

164164
/**
165-
* 从给定的 Raw GitHub URL 读取内容并返回字节数组
166-
* @param urlString Raw 内容链接
167-
* @return 文件内容的字节数组
168-
* @throws IOException 网络或IO错误
165+
* Fetches the content of a file from a given URL as a byte array.
166+
*
167+
* @param urlString The URL of the file.
168+
* @return The file content as a byte array.
169+
* @throws IOException If a network or I/O error occurs.
169170
*/
170171
public static byte[] fetchBytes(String urlString) throws IOException {
171172
log.info("开始从 Raw GitHub URL {} 中读取内容", urlString);
@@ -196,12 +197,10 @@ public static byte[] fetchBytes(String urlString) throws IOException {
196197
}
197198
}
198199
/**
199-
* 通过浅克隆仓库到临时目录,以字节数组形式读取指定文件
200+
* Removes the Byte Order Mark (BOM) from the beginning of a string, if present.
200201
*
201-
* @param gitUrl Git 仓库地址(如 https://github.com/opentiny/tiny-engine.git)
202-
* @param filePath 文件在仓库中的相对路径,如 "README.md"
203-
* @param branch 分支名,如 "main"
204-
* @return 文件的原始字节内容
202+
* @param gitUrl The input string.
203+
* @return The string without the BOM.
205204
*/
206205
public static byte[] getFileBytesViaClone(String gitUrl, String filePath, String branch)
207206
throws GitAPIException, IOException, URISyntaxException {
@@ -246,7 +245,10 @@ public static byte[] getFileBytesViaClone(String gitUrl, String filePath, String
246245
}
247246
}
248247

249-
248+
/**
249+
* Recursively deletes a directory and all of its contents.
250+
* @param dir
251+
*/
250252
private void deleteDirectory(java.io.File dir) {
251253
if (dir.isDirectory()) {
252254
java.io.File[] children = dir.listFiles();
@@ -259,7 +261,12 @@ private void deleteDirectory(java.io.File dir) {
259261
dir.delete();
260262
}
261263

262-
264+
/**
265+
* Removes the Byte Order Mark (BOM) from the beginning of a string, if present.
266+
*
267+
* @param input The input string.
268+
* @return The string without the BOM.
269+
*/
263270
public static String removeBOM(String input) {
264271
if (input != null && input.startsWith("\uFEFF")) {
265272
return input.substring(1);
@@ -282,6 +289,14 @@ private String toPascalCase(String input) {
282289
}
283290
return result.toString();
284291
}
292+
/**
293+
* Builds a list of Component objects from the given BundleDto, components, and snippets. It maps the component data from the BundleDto to Component entities, and associates any relevant snippets based on the component names.
294+
*
295+
* @param bundleDto The BundleDto containing the framework information and materials.
296+
* @param components A list of maps representing the component data to be converted into Component entities.
297+
* @param snippets A list of Child objects representing the snippets that may be associated with the components.
298+
* @return A list of Component objects constructed from the provided data.
299+
*/
285300
private List<Component> buildComponentList(BundleDto bundleDto, List<Map<String, Object>> components,
286301
List<Child> snippets) {
287302
List<Component> componentList = new ArrayList<>();
@@ -323,6 +338,12 @@ private List<Component> buildComponentList(BundleDto bundleDto, List<Map<String,
323338
return componentList;
324339
}
325340

341+
/**
342+
* Parses the given BundleDto to extract component and package information, and constructs a BundleResultDto.
343+
*
344+
* @param bundleDto The BundleDto containing the materials to be parsed.
345+
* @return A Result object containing the BundleResultDto with the parsed components and packages, or an error if parsing fails.
346+
*/
326347
public Result<BundleResultDto> parseBundle(BundleDto bundleDto) {
327348

328349
List<Map<String, Object>> components = bundleDto.getMaterials().getComponents();
@@ -351,7 +372,12 @@ public Result<BundleResultDto> parseBundle(BundleDto bundleDto) {
351372
return Result.success(bundleList);
352373
}
353374

354-
375+
/**
376+
* Performs bulk creation or update of components based on the provided list. If a component already exists (based on certain conditions), it will be updated; otherwise, a new record will be created.
377+
*
378+
* @param componentList The list of components to be processed for creation or update.
379+
* @return A FileResult object containing the count of inserted and updated records.
380+
*/
355381
public FileResult bulkCreate(List<Component> componentList) {
356382
int addNum = 0;
357383
int updateNum = 0;
@@ -411,7 +437,12 @@ public FileResult bulkCreate(List<Component> componentList) {
411437
fileResult.setUpdateNum(updateNum);
412438
return fileResult;
413439
}
414-
440+
/**
441+
* Validates whether a given string is a valid JSON structure.
442+
*
443+
* @param json The JSON string to validate.
444+
* @return True if the string is valid JSON, false otherwise.
445+
*/
415446
public static boolean isValidJson(String json) {
416447
ObjectMapper mapper = new ObjectMapper();
417448
if (json == null || json.trim().isEmpty()) {

0 commit comments

Comments
 (0)