convert

package
v1.2.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 20, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package convert provides type conversion utilities for NornicDB.

This package consolidates type conversion functions used throughout the codebase, ensuring consistent behavior for numeric conversions, slice transformations, and string parsing.

Key Functions:

  • ToFloat64: Convert various types to float64
  • ToInt64: Convert various types to int64
  • ToFloat64Slice: Convert slices to []float64
  • ToFloat32Slice: Convert slices to []float32

All conversion functions return a success boolean to allow callers to handle conversion failures gracefully.

Example:

// Convert any numeric type to float64
if f, ok := convert.ToFloat64(someValue); ok {
	// Use f
}

// Convert slice to []float32 for vector operations
floats := convert.ToFloat32Slice(data)
if floats != nil {
	// Use floats
}

ELI12:

This package is like a universal translator for numbers. You give it any kind of number (whole number, decimal, even text that looks like a number), and it converts it to the type you need. If it can't convert something (like "hello"), it tells you by returning false or nil.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToFloat32Slice

func ToFloat32Slice(v interface{}) []float32

ToFloat32Slice converts various slice types to []float32. Returns slice on success, nil on failure.

This is commonly used for converting data to embedding vector format, since most ML models use float32 vectors.

Supported types:

  • []float32 (returned as-is)
  • []float64 (each element converted)
  • []interface{} (each element converted)

Example:

s := ToFloat32Slice([]interface{}{1, 2.5, 3})  // Returns [1.0, 2.5, 3.0]
s := ToFloat32Slice([]float64{1.0, 2.0})      // Returns [1.0, 2.0]

ELI12:

This converts a list of numbers to the format used by AI/ML models for vector embeddings. The result is more memory efficient than float64.

func ToFloat64

func ToFloat64(v interface{}) (float64, bool)

ToFloat64 converts various numeric types to float64. Returns (value, true) on success, (0, false) on failure.

This is the STANDARD conversion function for all numeric types. Use this instead of custom type switches throughout the codebase.

Supported types:

  • float64 (returned as-is)
  • float32 (converted)
  • int, int32, int64 (lossless for values within float64 range)
  • string (parsed as decimal, supports scientific notation)

String parsing supports:

  • Decimal: "3.14", "-2.5"
  • Scientific notation: "1.23e-4", "6.02e23"
  • Special values: "NaN", "Inf", "-Inf"

Example 1 - Numeric types:

f, ok := ToFloat64(42)        // Returns (42.0, true)
f, ok := ToFloat64(int64(99)) // Returns (99.0, true)
f, ok := ToFloat64(3.14)      // Returns (3.14, true)

Example 2 - String parsing:

f, ok := ToFloat64("3.14")    // Returns (3.14, true)
f, ok := ToFloat64("1.5e-3")  // Returns (0.0015, true)
f, ok := ToFloat64("invalid") // Returns (0, false)

Example 3 - Error handling:

value := "user_input"
if f, ok := ToFloat64(value); ok {
	result := f * 2.0
} else {
	return fmt.Errorf("invalid number: %s", value)
}

ELI12:

This function is like a universal translator for numbers. You give it any kind of number (whole number, decimal, even text that looks like a number), and it converts it to a decimal number (float64). If it can't convert it (like trying to convert "hello" to a number), it tells you by returning false.

func ToFloat64Slice

func ToFloat64Slice(v interface{}) ([]float64, bool)

ToFloat64Slice converts various slice types to []float64. Returns (slice, true) on success, (nil, false) on failure.

Supported types:

  • []float64 (returned as-is)
  • []float32 (each element converted)
  • []interface{} (each element converted via ToFloat64)

Example:

s, ok := ToFloat64Slice([]interface{}{1, 2.5, "3"})  // Returns ([1.0, 2.5, 3.0], true)
s, ok := ToFloat64Slice([]float32{1.0, 2.0})        // Returns ([1.0, 2.0], true)

ELI12:

This takes a list of numbers (in any format) and converts them all to decimal numbers. If any number in the list can't be converted, it returns nil to tell you something went wrong.

func ToInt64

func ToInt64(v interface{}) (int64, bool)

ToInt64 converts various numeric types to int64. Returns (value, true) on success, (0, false) on failure.

Supported types:

  • int64 (returned as-is)
  • int, int32 (converted)
  • uint, uint32, uint64 (converted, may overflow for large uint64)
  • float64, float32 (truncated toward zero)
  • string (parsed as integer)

Example:

i, ok := ToInt64(42)        // Returns (42, true)
i, ok := ToInt64(3.7)       // Returns (3, true) - truncated
i, ok := ToInt64("123")     // Returns (123, true)
i, ok := ToInt64("invalid") // Returns (0, false)

ELI12:

This converts numbers to whole numbers (integers). If you give it a decimal like 3.7, it chops off the .7 part and gives you 3.

func ToStringSlice

func ToStringSlice(v interface{}) []string

ToStringSlice converts various slice types to []string. Returns slice on success, nil on failure.

Supported types:

  • []string (returned as-is)
  • []interface{} (each element converted via fmt.Sprint)

Example:

s := ToStringSlice([]interface{}{"a", "b", "c"})  // Returns ["a", "b", "c"]
s := ToStringSlice([]interface{}{1, 2, 3})        // Returns ["1", "2", "3"]

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL