Skip to content

refactor: introduce generics for toSet and findByKey helpers#20

Merged
AWaterColorPen merged 2 commits intomainfrom
feature/generics-refactor
Apr 15, 2026
Merged

refactor: introduce generics for toSet and findByKey helpers#20
AWaterColorPen merged 2 commits intomainfrom
feature/generics-refactor

Conversation

@AWaterColorPen
Copy link
Copy Markdown
Owner

Summary

This PR completes the generics modernization task from Phase 1 of the olap-sql modernization plan.

Changes

  • toSet[T comparable]([]T) map[T]struct{}: Replaces the old getKeySet([]string) map[any]struct{}. Now type-safe and pre-sized with make(..., len(items)) for better allocation efficiency.
  • findByKey[T models.IModel](items []T, key, kind string) (T, error): A generic linear-search helper that eliminates three identical for loops in FileAdapter (GetDataSetByKey, GetMetricByKey, GetDimensionByKey).
  • .gitignore: Added *.sh to exclude leftover shell scripts.

Why generics here?

The old getKeySet returned map[any]struct{} — using any for string keys lost type safety. The new toSet[T] is properly typed.

The three GetXByKey methods were copy-paste identical except for the element type. The generic findByKey collapses them into a single implementation backed by the existing models.IModel interface.

Tests

All existing tests pass (go test ./...).

- Replace getKeySet() (returned map[any]struct{}) with toSet[T comparable]()
  for type-safe O(1) membership tests
- Add generic findByKey[T models.IModel]() to eliminate repeated linear-search
  loops in FileAdapter.GetDataSetByKey, GetMetricByKey, GetDimensionByKey
- Update .gitignore to exclude *.sh scripts

All existing tests pass.
Comment thread .gitignore Outdated
# vendor/

.idea No newline at end of file
.idea*.sh
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

.gitignore has a merge artifact: .idea and *.sh ended up concatenated on one line as .idea*.sh. This means neither pattern works correctly — .idea directory is no longer ignored, and only *.sh files adjacent to .idea would match (they don't). Should be split into two lines:

.idea
*.sh

I'll push a fix.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Fixed in ace4638 — split into two separate lines:

.idea
*.sh

The previous commit accidentally concatenated .idea and *.sh onto one
line as '.idea*.sh', which broke both patterns. Separate them so that
the .idea directory and *.sh files are both properly ignored.
@AWaterColorPen AWaterColorPen merged commit 0533891 into main Apr 15, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant