MCPcopy Create free account
hub / github.com/astercloud/aster / List

Method List

pkg/store/json.go:422–456  ·  view source on GitHub ↗

List 列出资源

(ctx context.Context, collection string)

Source from the content-addressed store, hash-verified

420
421// List 列出资源
422func (js *JSONStore) List(ctx context.Context, collection string) ([]any, error) {
423 js.mu.RLock()
424 defer js.mu.RUnlock()
425
426 dir := js.collectionDir(collection)
427 entries, err := os.ReadDir(dir)
428 if err != nil {
429 if os.IsNotExist(err) {
430 return []any{}, nil
431 }
432 return nil, fmt.Errorf("read directory: %w", err)
433 }
434
435 items := make([]any, 0, len(entries))
436 for _, entry := range entries {
437 if entry.IsDir() || filepath.Ext(entry.Name()) != ".json" {
438 continue
439 }
440
441 var item any
442 path := filepath.Join(dir, entry.Name())
443 data, err := os.ReadFile(path)
444 if err != nil {
445 continue // 忽略读取失败的文件
446 }
447
448 if err := json.Unmarshal(data, &item); err != nil {
449 continue // 忽略损坏的文件
450 }
451
452 items = append(items, item)
453 }
454
455 return items, nil
456}
457
458// Exists 检查资源是否存在
459func (js *JSONStore) Exists(ctx context.Context, collection, key string) (bool, error) {

Callers

nothing calls this directly

Calls 4

collectionDirMethod · 0.95
NameMethod · 0.65
JoinMethod · 0.45
ReadFileMethod · 0.45

Tested by

no test coverage detected