rostrconv

package module
v0.0.0-...-a6ee939 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

README

String Conversion Plugin

The string conversion plugin provides operators for converting between strings and various data types using Go's strconv package.

Installation

go get github.com/samber/ro/plugins/strconv

Operators

Atoi

Converts strings to integers using strconv.Atoi.

import (
    "github.com/samber/ro"
    rostrconv "github.com/samber/ro/plugins/strconv"
)

observable := ro.Pipe1(
    ro.Just("123", "456", "789"),
    rostrconv.Atoi[string](),
)

subscription := observable.Subscribe(ro.PrintObserver[int]())
defer subscription.Unsubscribe()

// Output:
// Next: 123
// Next: 456
// Next: 789
// Completed
ParseInt

Converts strings to int64 values with specified base and bit size.

observable := ro.Pipe1(
    ro.Just("123", "FF", "1010"),
    rostrconv.ParseInt[string](16, 64), // Parse as hex, 64-bit
)

subscription := observable.Subscribe(ro.PrintObserver[int64]())
defer subscription.Unsubscribe()

// Output:
// Next: 291
// Next: 255
// Next: 4112
// Completed
ParseFloat

Converts strings to float64 values with specified bit size.

observable := ro.Pipe1(
    ro.Just("3.14", "2.718", "1.414"),
    rostrconv.ParseFloat[string](64), // Parse as 64-bit float
)

subscription := observable.Subscribe(ro.PrintObserver[float64]())
defer subscription.Unsubscribe()

// Output:
// Next: 3.14
// Next: 2.718
// Next: 1.414
// Completed
ParseBool

Converts strings to boolean values using strconv.ParseBool.

observable := ro.Pipe1(
    ro.Just("true", "false", "1", "0"),
    rostrconv.ParseBool[string](),
)

subscription := observable.Subscribe(ro.PrintObserver[bool]())
defer subscription.Unsubscribe()

// Output:
// Next: true
// Next: false
// Next: true
// Next: false
// Completed
ParseUint

Converts strings to uint64 values with specified base and bit size.

observable := ro.Pipe1(
    ro.Just("123", "456", "789"),
    rostrconv.ParseUint[string](10, 64), // Parse as decimal, 64-bit unsigned
)

subscription := observable.Subscribe(ro.PrintObserver[uint64]())
defer subscription.Unsubscribe()

// Output:
// Next: 123
// Next: 456
// Next: 789
// Completed
FormatBool

Converts boolean values to strings using strconv.FormatBool.

observable := ro.Pipe1(
    ro.Just(true, false, true),
    rostrconv.FormatBool(),
)

subscription := observable.Subscribe(ro.PrintObserver[string]())
defer subscription.Unsubscribe()

// Output:
// Next: true
// Next: false
// Next: true
// Completed
FormatFloat

Converts float64 values to strings with specified format, precision, and bit size.

observable := ro.Pipe1(
    ro.Just(3.14159, 2.71828, 1.41421),
    rostrconv.FormatFloat('f', 3, 64), // Format with 3 decimal places
)

subscription := observable.Subscribe(ro.PrintObserver[string]())
defer subscription.Unsubscribe()

// Output:
// Next: 3.142
// Next: 2.718
// Next: 1.414
// Completed
FormatInt

Converts int64 values to strings with specified base.

observable := ro.Pipe1(
    ro.Just(int64(255), int64(123), int64(456)),
    rostrconv.FormatInt[string](16), // Format as hex
)

subscription := observable.Subscribe(ro.PrintObserver[string]())
defer subscription.Unsubscribe()

// Output:
// Next: ff
// Next: 7b
// Next: 1c8
// Completed
FormatUint

Converts uint64 values to strings with specified base.

observable := ro.Pipe1(
    ro.Just(uint64(255), uint64(123), uint64(456)),
    rostrconv.FormatUint[string](10), // Format as decimal
)

subscription := observable.Subscribe(ro.PrintObserver[string]())
defer subscription.Unsubscribe()

// Output:
// Next: 255
// Next: 123
// Next: 456
// Completed
Itoa

Converts integers to strings using strconv.Itoa.

observable := ro.Pipe1(
    ro.Just(123, 456, 789),
    rostrconv.Itoa(),
)

subscription := observable.Subscribe(ro.PrintObserver[string]())
defer subscription.Unsubscribe()

// Output:
// Next: 123
// Next: 456
// Next: 789
// Completed
Quote

Quotes strings using strconv.Quote.

observable := ro.Pipe1(
    ro.Just("hello", "world", "golang"),
    rostrconv.Quote(),
)

subscription := observable.Subscribe(ro.PrintObserver[string]())
defer subscription.Unsubscribe()

// Output:
// Next: "hello"
// Next: "world"
// Next: "golang"
// Completed
QuoteRune

Quotes runes using strconv.QuoteRune.

observable := ro.Pipe1(
    ro.Just('a', 'b', 'c'),
    rostrconv.QuoteRune(),
)

subscription := observable.Subscribe(ro.PrintObserver[string]())
defer subscription.Unsubscribe()

// Output:
// Next: 'a'
// Next: 'b'
// Next: 'c'
// Completed
Unquote

Unquotes strings using strconv.Unquote.

observable := ro.Pipe1(
    ro.Just(`"hello"`, `"world"`, `"golang"`),
    rostrconv.Unquote(),
)

subscription := observable.Subscribe(ro.PrintObserver[string]())
defer subscription.Unsubscribe()

// Output:
// Next: hello
// Next: world
// Next: golang
// Completed

Error Handling

All parsing operators handle errors gracefully and will emit error notifications for invalid input:

observable := ro.Pipe1(
    ro.Just("123", "abc", "456"), // "abc" will cause an error
    rostrconv.ParseInt[string](10, 64),
)

subscription := observable.Subscribe(
    ro.NewObserver(
        func(value int64) {
            // Handle successful parsing
        },
        func(err error) {
            // Handle parsing error
        },
        func() {
            // Handle completion
        },
    ),
)
defer subscription.Unsubscribe()

Roundtrip Examples

Demonstrate roundtrip conversions:

// Integer roundtrip
observable := ro.Pipe2(
    ro.Just(123, 456, 789),
    rostrconv.Itoa(),
    rostrconv.Atoi[string](),
)

subscription := observable.Subscribe(ro.PrintObserver[int]())
defer subscription.Unsubscribe()

// Output:
// Next: 123
// Next: 456
// Next: 789
// Completed

Real-world Example

Here's a practical example that processes CSV data with type conversions:

import (
    "github.com/samber/ro"
    rostrconv "github.com/samber/ro/plugins/strconv"
)

type User struct {
    ID   int
    Name string
    Age  int
}

// Process CSV data with type conversions
pipeline := ro.Pipe4(
    // Simulate CSV data as strings
    ro.Just(
        []string{"1", "Alice", "30"},
        []string{"2", "Bob", "25"},
        []string{"3", "Charlie", "35"},
    ),
    // Convert ID and Age to integers
    ro.Map(func(row []string) []string {
        // In a real scenario, you'd use rostrconv.Atoi for each field
        return row
    }),
    // Extract and convert ID
    ro.Map(func(row []string) int {
        // This would be done with rostrconv.Atoi in practice
        return 1 // Simplified for example
    }),
    // Convert back to string for output
    rostrconv.Itoa(),
)

subscription := pipeline.Subscribe(ro.PrintObserver[string]())
defer subscription.Unsubscribe()

Base and Bit Size Parameters

ParseInt and ParseUint
  • base: Number system (10 for decimal, 16 for hex, 8 for octal, 2 for binary)
  • bitSize: Integer type size (32 for int32/uint32, 64 for int64/uint64)
ParseFloat
  • bitSize: Float type size (32 for float32, 64 for float64)
FormatFloat
  • fmt: Format byte ('f', 'e', 'E', 'g', 'G')
  • prec: Precision (number of decimal places)
  • bitSize: Float type size (32 or 64)

Performance Considerations

  • The plugin uses Go's standard strconv package for all conversions
  • Error handling is built into all parsing operators
  • Consider using appropriate bit sizes to avoid overflow
  • Use base 10 for decimal numbers, base 16 for hexadecimal, etc.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Atoi

func Atoi[T ~string]() func(ro.Observable[T]) ro.Observable[int]

Atoi converts strings to integers using strconv.Atoi. Returns an error if the string cannot be converted to an integer.

Example:

ro.Pipe(ro.FromSlice([]string{"123", "456", "789"}), rostrconv.Atoi[string]()).
  Subscribe(ro.NewObserver[int](...))

Play: https://go.dev/play/p/5hL9m8jK3nQ

Example
// Convert strings to integers
observable := ro.Pipe1(
	ro.Just("123", "456", "789"),
	Atoi[string](),
)

subscription := observable.Subscribe(ro.PrintObserver[int]())
defer subscription.Unsubscribe()
Output:

Next: 123
Next: 456
Next: 789
Completed

func FormatBool

func FormatBool() func(ro.Observable[bool]) ro.Observable[string]

FormatBool converts boolean values to strings using strconv.FormatBool. Returns "true" for true values and "false" for false values.

Example:

ro.Pipe(ro.FromSlice([]bool{true, false, true}), rostrconv.FormatBool()).
  Subscribe(ro.NewObserver[string](...))

Play: https://go.dev/play/p/8vDdaQyzoi_b

Example
// Convert booleans to strings
observable := ro.Pipe1(
	ro.Just(true, false, true),
	FormatBool(),
)

subscription := observable.Subscribe(ro.PrintObserver[string]())
defer subscription.Unsubscribe()
Output:

Next: true
Next: false
Next: true
Completed

func FormatComplex

func FormatComplex(mt byte, prec, bitSize int) func(ro.Observable[complex128]) ro.Observable[string]

FormatComplex converts complex128 values to strings with specified format, precision, and bit size. The format parameter (mt) can be:

'f' for fixed-point notation
'e' for scientific notation
'g' for the shortest representation

The prec parameter specifies the precision (number of digits). The bitSize parameter specifies the float type size (64 or 128).

Example:

ro.Pipe(ro.FromSlice([]complex128{3+4i, 1+2i}), rostrconv.FormatComplex('f', 2, 128)). // Fixed-point, 2 decimal places
  Subscribe(ro.NewObserver[string](...))

Play: https://go.dev/play/p/gbp_kl7XJWv

func FormatFloat

func FormatFloat(mt byte, prec, bitSize int) func(ro.Observable[float64]) ro.Observable[string]

FormatFloat converts float64 values to strings with specified format, precision, and bit size. The format parameter (mt) can be:

'f' for fixed-point notation
'e' for scientific notation
'g' for the shortest representation
'x' for hexadecimal notation

The prec parameter specifies the precision (number of digits). The bitSize parameter specifies the float type size (32 or 64).

Example:

ro.Pipe(ro.FromSlice([]float64{3.14159, 2.71828}), rostrconv.FormatFloat('f', 2, 64)). // Fixed-point, 2 decimal places
  Subscribe(ro.NewObserver[string](...))

Play: https://go.dev/play/p/GWSPE4Mp-uy

Example
// Convert floats to strings
observable := ro.Pipe1(
	ro.Just(3.14159, 2.71828, 1.41421),
	FormatFloat('f', 3, 64), // Format with 3 decimal places
)

subscription := observable.Subscribe(ro.PrintObserver[string]())
defer subscription.Unsubscribe()
Output:

Next: 3.142
Next: 2.718
Next: 1.414
Completed

func FormatInt

func FormatInt[T ~string](base int) func(ro.Observable[int64]) ro.Observable[string]

FormatInt converts int64 values to strings with specified base. The base parameter determines the number system (e.g., 10 for decimal, 16 for hexadecimal).

Example:

ro.Pipe(ro.FromSlice([]int64{123, 456, 789}), rostrconv.FormatInt[string](16)). // Format as hexadecimal
  Subscribe(ro.NewObserver[string](...))

Play: https://go.dev/play/p/hUpBVHRJgXY

Example
// Convert integers to strings with different bases
observable := ro.Pipe1(
	ro.Just(int64(255), int64(123), int64(456)),
	FormatInt[string](16), // Format as hex
)

subscription := observable.Subscribe(ro.PrintObserver[string]())
defer subscription.Unsubscribe()
Output:

Next: ff
Next: 7b
Next: 1c8
Completed

func FormatUint

func FormatUint[T ~string](base int) func(ro.Observable[uint64]) ro.Observable[string]

FormatUint converts uint64 values to strings with specified base. The base parameter determines the number system (e.g., 10 for decimal, 16 for hexadecimal).

Example:

ro.Pipe(ro.FromSlice([]uint64{123, 456, 789}), rostrconv.FormatUint[string](16)). // Format as hexadecimal
  Subscribe(ro.NewObserver[string](...))

Play: https://go.dev/play/p/h4TYG9sFPZw

Example
// Convert unsigned integers to strings
observable := ro.Pipe1(
	ro.Just(uint64(255), uint64(123), uint64(456)),
	FormatUint[string](10), // Format as decimal
)

subscription := observable.Subscribe(ro.PrintObserver[string]())
defer subscription.Unsubscribe()
Output:

Next: 255
Next: 123
Next: 456
Completed

func Itoa

func Itoa() func(ro.Observable[int]) ro.Observable[string]

Itoa converts integers to strings using strconv.Itoa. This is equivalent to FormatInt with base 10.

Example:

ro.Pipe(ro.FromSlice([]int{123, 456, 789}), rostrconv.Itoa()).
  Subscribe(ro.NewObserver[string](...))

Play: https://go.dev/play/p/6hN7k9jL4mR

Example
// Convert integers to strings
observable := ro.Pipe1(
	ro.Just(123, 456, 789),
	Itoa(),
)

subscription := observable.Subscribe(ro.PrintObserver[string]())
defer subscription.Unsubscribe()
Output:

Next: 123
Next: 456
Next: 789
Completed

func ParseBool

func ParseBool[T ~string]() func(ro.Observable[T]) ro.Observable[bool]

ParseBool converts strings to boolean values using strconv.ParseBool. Accepts "1", "t", "T", "true", "TRUE", "True" for true values. Accepts "0", "f", "F", "false", "FALSE", "False" for false values. Returns an error if the string cannot be converted to a boolean.

Example:

ro.Pipe(ro.FromSlice([]string{"true", "false", "1", "0"}), rostrconv.ParseBool[string]()).
  Subscribe(ro.NewObserver[bool](...))

Play: https://go.dev/play/p/2C5fkrRLyW_g

Example
// Parse strings as booleans
observable := ro.Pipe1(
	ro.Just("true", "false", "1", "0"),
	ParseBool[string](),
)

subscription := observable.Subscribe(ro.PrintObserver[bool]())
defer subscription.Unsubscribe()
Output:

Next: true
Next: false
Next: true
Next: false
Completed

func ParseFloat

func ParseFloat[T ~string](bitSize int) func(ro.Observable[T]) ro.Observable[float64]

ParseFloat converts strings to float64 values with specified bit size. The bitSize parameter specifies the float type size (e.g., 32 for float32, 64 for float64). Returns an error if the string cannot be converted to a float.

Example:

ro.Pipe(ro.FromSlice([]string{"3.14", "2.718", "1.414"}), rostrconv.ParseFloat[string](64)). // Parse as 64-bit float
  Subscribe(ro.NewObserver[float64](...))

Play: https://go.dev/play/p/g-YvtjXtX7V

Example
// Parse strings as floats
observable := ro.Pipe1(
	ro.Just("3.14", "2.718", "1.414"),
	ParseFloat[string](64), // Parse as 64-bit float
)

subscription := observable.Subscribe(ro.PrintObserver[float64]())
defer subscription.Unsubscribe()
Output:

Next: 3.14
Next: 2.718
Next: 1.414
Completed

func ParseInt

func ParseInt[T ~string](base int, bitSize int) func(ro.Observable[T]) ro.Observable[int64]

ParseInt converts strings to int64 values with specified base and bit size. The base parameter determines the number system (e.g., 10 for decimal, 16 for hexadecimal). The bitSize parameter specifies the integer type size (e.g., 32 for int32, 64 for int64). Returns an error if the string cannot be converted to the specified integer type.

Example:

ro.Pipe(ro.FromSlice([]string{"123", "FF", "1010"}), rostrconv.ParseInt[string](16, 64)). // Parse as hex, 64-bit
  Subscribe(ro.NewObserver[int64](...))

Play: https://go.dev/play/p/CqjCmQVAPXC

Example
// Parse strings as integers with different bases
observable := ro.Pipe1(
	ro.Just("123", "FF", "1010"),
	ParseInt[string](16, 64), // Parse as hex, 64-bit
)

subscription := observable.Subscribe(ro.PrintObserver[int64]())
defer subscription.Unsubscribe()
Output:

Next: 291
Next: 255
Next: 4112
Completed
Example (WithError)
// Parse strings with potential errors
observable := ro.Pipe1(
	ro.Just("123", "abc", "456"), // "abc" will cause an error
	ParseInt[string](10, 64),
)

subscription := observable.Subscribe(ro.PrintObserver[int64]())
defer subscription.Unsubscribe()
Output:

Next: 123
Error: strconv.ParseInt: parsing "abc": invalid syntax

func ParseUint

func ParseUint[T ~string](base int, bitSize int) func(ro.Observable[T]) ro.Observable[uint64]

ParseUint converts strings to uint64 values with specified base and bit size. The base parameter determines the number system (e.g., 10 for decimal, 16 for hexadecimal). The bitSize parameter specifies the integer type size (e.g., 32 for uint32, 64 for uint64). Returns an error if the string cannot be converted to the specified unsigned integer type.

Example:

ro.Pipe(ro.FromSlice([]string{"123", "FF", "1010"}), rostrconv.ParseUint[string](16, 64)). // Parse as hex, 64-bit unsigned
  Subscribe(ro.NewObserver[uint64](...))
Example
// Parse strings as unsigned integers
observable := ro.Pipe1(
	ro.Just("123", "456", "789"),
	ParseUint[string](10, 64), // Parse as decimal, 64-bit unsigned
)

subscription := observable.Subscribe(ro.PrintObserver[uint64]())
defer subscription.Unsubscribe()
Output:

Next: 123
Next: 456
Next: 789
Completed

func ParseUint64

func ParseUint64[T ~string](base int, bitSize int) func(ro.Observable[T]) ro.Observable[uint64]

ParseUint64 is an alias for ParseUint that specifically returns uint64. It provides the same functionality as ParseUint with bitSize=64.

Example:

ro.Pipe(ro.FromSlice([]string{"123", "456", "789"}), rostrconv.ParseUint64[string](10, 64)). // Parse as decimal, 64-bit unsigned
  Subscribe(ro.NewObserver[uint64](...))

func Quote

func Quote() func(ro.Observable[string]) ro.Observable[string]

Quote converts strings to Go string literals using strconv.Quote. This adds double quotes and escapes special characters.

Example:

ro.Pipe(ro.FromSlice([]string{"hello", "world\n", "test"}), rostrconv.Quote()).
  Subscribe(ro.NewObserver[string](...))

Play: https://go.dev/play/p/O72Y-oUwBxr

Example
// Quote strings
observable := ro.Pipe1(
	ro.Just("hello", "world", "golang"),
	Quote(),
)

subscription := observable.Subscribe(ro.PrintObserver[string]())
defer subscription.Unsubscribe()
Output:

Next: "hello"
Next: "world"
Next: "golang"
Completed

func QuoteRune

func QuoteRune() func(ro.Observable[rune]) ro.Observable[string]

QuoteRune converts runes to Go character literals using strconv.QuoteRune. This adds single quotes and escapes special characters.

Example:

ro.Pipe(ro.FromSlice([]rune{'a', 'b', 'c'}), rostrconv.QuoteRune()).
  Subscribe(ro.NewObserver[string](...))

Play: https://go.dev/play/p/tCnviYGuSMn

Example
// Quote runes
observable := ro.Pipe1(
	ro.Just('a', 'b', 'c'),
	QuoteRune(),
)

subscription := observable.Subscribe(ro.PrintObserver[string]())
defer subscription.Unsubscribe()
Output:

Next: 'a'
Next: 'b'
Next: 'c'
Completed

func Unquote

func Unquote() func(ro.Observable[string]) ro.Observable[string]

Unquote converts Go string literals back to strings using strconv.Unquote. This removes quotes and unescapes special characters. Returns an error if the string is not a valid Go string literal.

Example:

ro.Pipe(ro.FromSlice([]string{`"hello"`, `"world\n"`, `"test"`}), rostrconv.Unquote()).
  Subscribe(ro.NewObserver[string](...))

Play: https://go.dev/play/p/cMaHM-He8NT

Example
// Unquote strings
observable := ro.Pipe1(
	ro.Just(`"hello"`, `"world"`, `"golang"`),
	Unquote(),
)

subscription := observable.Subscribe(ro.PrintObserver[string]())
defer subscription.Unsubscribe()
Output:

Next: hello
Next: world
Next: golang
Completed

Types

This section is empty.

Jump to

Keyboard shortcuts

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