List 列出资源
(ctx context.Context, collection string)
| 420 | |
| 421 | // List 列出资源 |
| 422 | func (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 检查资源是否存在 |
| 459 | func (js *JSONStore) Exists(ctx context.Context, collection, key string) (bool, error) { |
nothing calls this directly
no test coverage detected