Skip to content

Reduce code duplication#82

Merged
jaeyson merged 4 commits into
mainfrom
81-reduce-code-duplication
Jun 1, 2025
Merged

Reduce code duplication#82
jaeyson merged 4 commits into
mainfrom
81-reduce-code-duplication

Conversation

@jaeyson
Copy link
Copy Markdown
Owner

@jaeyson jaeyson commented Jun 1, 2025

Closes #81

The purpose of this change is to reduce code duplication and to streamline passing of options (including conn). The breaking change here is conn is now part of opts. When calling functions, see example below:

# pre-v2
Collections.get_collections(conn, opts)

# v2
Collections.get_collections(conn: conn)

# another way (v2)
opts = [limit: 1, conn: conn]
Collections.get_collections(opts)

Summary by Sourcery

Streamline the API by consolidating all client entrypoints to a single keyword-list signature with a conn option and other parameters as options; remove redundant overloaded clauses and aliases; update documentation, examples, and tests to reflect the unified interface; bump open_api_typesense dependency to 1.0 and add upgrade instructions in the README.

Enhancements:

  • Consolidate all functions across modules to accept just one opts keyword-list parameter (including conn), removing duplicated arity overloads
  • Simplify client module APIs by removing explicit Connection aliases and redundant delegates
  • Standardize module documentation with unified “## Options” and “## Examples” sections
  • Revise all tests to use the new conn: conn form instead of separate conn arguments

Build:

  • Bump open_api_typesense dependency to “~> 1.0”

Documentation:

  • Add migration guide to README explaining the new opts-based interface and how to upgrade to v2

@jaeyson jaeyson self-assigned this Jun 1, 2025
@jaeyson jaeyson added the documentation Improvements or additions to documentation label Jun 1, 2025
@jaeyson jaeyson linked an issue Jun 1, 2025 that may be closed by this pull request
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Jun 1, 2025

Reviewer's Guide

The pull request eliminates repetitive function clauses across the codebase by unifying all API calls to a single arity that accepts a keyword list of options (including the conn), updates module-level delegates to match these signatures, overhauls documentation with consistent “## Options” and “## Examples” blocks, adjusts the README for the v2 breaking change, bumps the open_api_typesense dependency, and updates all tests to call the new signatures.

Sequence Diagram: Refactored API Call Pattern

sequenceDiagram
    participant Client
    participant ExTypesenseModule as ExTypesense.Module
    participant OpenApiLib as OpenApiTypesense.SubModuleAPI

    Client->>ExTypesenseModule: refactored_function(regular_args*, opts_with_conn_and_params)
    ExTypesenseModule->>OpenApiLib: underlying_api_call(regular_args*, opts_with_conn_and_params)
    OpenApiLib-->>ExTypesenseModule: ApiResponse
    ExTypesenseModule-->>Client: Result
Loading

Updated Class Diagram for ExTypesense.Collection

classDiagram
  class ExTypesense.Collection {
    +list_collections(opts: keyword) : Result
    +create_collection_with_alias(schema: map_or_module, opts: keyword) : Result
    +create_collection(schema: map_or_module, opts: keyword) : Result
    +clone_collection(src_coll: string_or_module, new_coll_name: string, opts: keyword) : Result
    +get_collection(coll_name_or_module: string_or_module, opts: keyword) : Result
    +update_collection_fields(name_or_module: string_or_module, fields: map, opts: keyword) : Result
    +drop_collection(name_or_module: string_or_module, opts: keyword) : Result
    +list_collection_aliases(opts: keyword) : Result
    +get_collection_alias(alias_name_or_module: string_or_module, opts: keyword) : Result
    +upsert_collection_alias(alias_name: string, coll_name_or_module: string_or_module, opts: keyword) : Result
    +delete_collection_alias(alias_name: string, opts: keyword) : Result
  }
Loading

Updated Class Diagram for ExTypesense.Document

classDiagram
  class ExTypesense.Document {
    +get_document(coll_name_or_module: string_or_module, doc_id: string, opts: keyword) : Result
    +import_documents(coll_name_or_module: string_or_module, documents: list, opts: keyword) : Result
    +index_document(doc_map_or_struct: map_or_struct, opts: keyword) : Result
    +index_document(coll_name: string, doc_body: map, opts: keyword) : Result
    +update_document(doc_map_or_struct: map_or_struct, opts: keyword) : Result
    +update_documents_by_query(coll_name_or_module: string_or_module, body: map, opts: keyword) : Result
    +delete_document(doc_map_or_struct: map_or_struct, opts: keyword) : Result
    +delete_document(coll_name: string, doc_id: string, opts: keyword) : Result
    +export_documents(coll_name_or_module: string_or_module, opts: keyword) : Result
    +delete_documents_by_query(coll_name_or_module: string_or_module, opts: keyword) : Result
    +delete_all_documents(coll_name_or_module: string_or_module, opts: keyword) : Result
  }
Loading

Updated Class Diagram for ExTypesense.Cluster

classDiagram
  class ExTypesense.Cluster {
    +health(opts: keyword) : Result
    +api_stats(opts: keyword) : Result
    +cluster_metrics(opts: keyword) : Result
    +create_snapshot(opts: keyword) : Result
    +compact_db(opts: keyword) : Result
    +clear_cache(opts: keyword) : Result
    +get_schema_changes(opts: keyword) : Result
    +toggle_slow_request_log(config: map, opts: keyword) : Result
    +vote(opts: keyword) : Result
  }
Loading

Updated Class Diagram for ExTypesense.Analytics

classDiagram
  class ExTypesense.Analytics {
    +create_analytics_rule(body: map, opts: keyword) : Result
    +create_analytics_event(body: map, opts: keyword) : Result
    +list_analytics_rules(opts: keyword) : Result
    +get_analytics_rule(rule_name: string, opts: keyword) : Result
    +upsert_analytics_rule(rule_name: string, body: map, opts: keyword) : Result
    +delete_analytics_rule(rule_name: string, opts: keyword) : Result
  }
Loading

Updated Class Diagram for ExTypesense.Curation

classDiagram
  class ExTypesense.Curation {
    +get_override(collection_name_or_module: string_or_module, override_id: string, opts: keyword) : Result
    +upsert_override(collection_name_or_module: string_or_module, override_id: string, body: map, opts: keyword) : Result
    +list_overrides(collection_name_or_module: string_or_module, opts: keyword) : Result
    +delete_override(collection_name_or_module: string_or_module, override_id: string, opts: keyword) : Result
  }
Loading

Updated Class Diagram for ExTypesense.Conversation

classDiagram
  class ExTypesense.Conversation {
    +create_model(body: map, opts: keyword) : Result
    +get_model(model_id: string, opts: keyword) : Result
    +update_model(model_id: string, body: map, opts: keyword) : Result
    +list_models(opts: keyword) : Result
    +delete_model(model_id: string, opts: keyword) : Result
  }
Loading

File-Level Changes

Change Details Files
Consolidate function signatures to a single opts-based arity
  • Removed multiple pattern‐matched clauses for handling custom vs default connections
  • Updated @SPEC declarations to accept a keyword list only
  • Replaced `Connection.new()
> …` pipelines with direct calls using opts
Adjust ExTypesense module delegates to match new signatures
  • Removed duplicate delegate clauses
  • Changed all delegates to accept only opts (keyword list)
lib/ex_typesense.ex
Overhaul documentation in all modules
  • Removed old inline example blocks
  • Added uniform “## Options” and “## Examples” sections
  • Documented conn as part of opts
lib/ex_typesense/collection.ex
lib/ex_typesense/document.ex
lib/ex_typesense/cluster.ex
lib/ex_typesense/analytics.ex
lib/ex_typesense/curation.ex
lib/ex_typesense/conversation.ex
lib/ex_typesense/debug.ex
lib/ex_typesense/search.ex
lib/ex_typesense/synonym.ex
lib/ex_typesense/stopwords.ex
lib/ex_typesense/stemming.ex
lib/ex_typesense/preset.ex
lib/ex_typesense/key.ex
Update README and bump open_api_typesense dependency
  • Added “Upgrading to v2” warning and examples
  • Adjusted usage examples to new opts style
  • Updated mix.exs dependency to ~> 1.0
README.md
mix.exs
mix.lock
Revise test suite to use new opts signature
  • Replaced multi‐arity calls with single arity plus conn: conn
  • Removed legacy calls and updated assertions
test/collection_test.exs
test/document_test.exs
test/cluster_test.exs
test/search_test.exs
test/analytics_test.exs
test/synonym_test.exs
test/curation_test.exs
test/conversation_test.exs
test/key_test.exs
test/preset_test.exs
test/stopwords_test.exs
test/stemming_test.exs
test/debug_test.exs

Assessment against linked issues

Issue Objective Addressed Explanation
#81 Reduce code duplication by removing redundant code blocks and centralizing common functionalities.

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@jaeyson jaeyson moved this to In Progress in ExTypesense Jun 1, 2025
Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey @jaeyson - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 9 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread lib/ex_typesense/document.ex
Comment thread lib/ex_typesense/document.ex
Comment thread lib/ex_typesense/document.ex
Comment thread lib/ex_typesense/document.ex
Comment thread lib/ex_typesense/document.ex
Comment thread lib/ex_typesense/synonym.ex
Comment thread lib/ex_typesense/search.ex
Comment thread lib/ex_typesense/stopwords.ex
Comment thread README.md Outdated
@jaeyson jaeyson merged commit 26a3872 into main Jun 1, 2025
1 check passed
@github-project-automation github-project-automation Bot moved this from In Progress to Done in ExTypesense Jun 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Reduce code duplication

1 participant