Skip to content

feature/add-int-operations#4

Merged
KilianBoute merged 1 commit intomainfrom
feature/add-int-operations
Jan 20, 2026
Merged

feature/add-int-operations#4
KilianBoute merged 1 commit intomainfrom
feature/add-int-operations

Conversation

@KilianBoute
Copy link
Member

@KilianBoute KilianBoute commented Jan 20, 2026

Description

Pull Request Description

Title: feature/add-int-operations

Motivation and Improvement

This pull request introduces a new utility function, ToInt, for converting various types to an integer with a fallback option. The primary motivation behind this change is to streamline and simplify the conversion process of different numeric types and other types to an integer. This enhancement is particularly useful for handling scenarios where input types might vary, providing a consistent and reliable method to obtain an integer value.

Changes Made

  1. New Function in pkg/int/operations.go:

    • Function: ToInt(value any, fallback int) int
    • Purpose: Converts the given value to an integer. If the conversion is not possible, it returns the provided fallback value.
    • Supported Conversions:
      • int
      • int32
      • int64
      • float32 (truncated to int)
      • float64 (truncated to int)
      • Default case returns the fallback value
  2. Unit Tests in pkg/int/operations_test.go:

    • Added a comprehensive set of tests to cover various scenarios and types that ToInt should handle.
    • Test cases include:
      • Direct integer values
      • Integer types with different bit sizes (int32, int64)
      • Floating-point numbers (float32, float64) ensuring truncation
      • Non-numeric types (e.g., string, bool, nil) to test fallback behavior

Benefits

  • Consistency: Provides a standardized way to convert various types to an integer, reducing the need for repetitive type-checking and conversion code throughout the project.
  • Reliability: By including a fallback mechanism, the function ensures that a valid integer is always returned, even when the input type is unsupported.
  • Maintainability: Simplifies code maintenance and readability by encapsulating conversion logic in a single utility function.

This addition enhances the robustness and flexibility of the project by addressing potential type handling issues in a clean and efficient manner.

Introduces a ToInt function to convert various types (int, int32, int64, float32, float64) to int with a fallback value. Includes comprehensive unit tests covering supported and unsupported types.
@KilianBoute KilianBoute merged commit e242f67 into main Jan 20, 2026
3 checks passed
@KilianBoute KilianBoute deleted the feature/add-int-operations branch January 20, 2026 12:07
@codecov
Copy link

codecov bot commented Jan 20, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@KilianBoute KilianBoute restored the feature/add-int-operations branch January 21, 2026 11:39
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