robase64

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

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

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

README

Base64 Encoding Plugin

The base64 encoding plugin provides operators for encoding and decoding data using Go's encoding/base64 package.

Installation

go get github.com/samber/ro/plugins/encoding/base64

Operators

Encode

Encodes byte slices to base64 strings using the specified encoding.

import (
    "encoding/base64"
    "github.com/samber/ro"
    robase64 "github.com/samber/ro/plugins/encoding/base64"
)

observable := ro.Pipe1(
    ro.Just([]byte("hello"), []byte("world"), []byte("golang")),
    robase64.Encode[[]byte](base64.StdEncoding),
)

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

// Output:
// Next: aGVsbG8=
// Next: d29ybGQ=
// Next: Z29sYW5n
// Completed
Decode

Decodes base64 strings to byte slices using the specified encoding.

observable := ro.Pipe1(
    ro.Just("aGVsbG8=", "d29ybGQ=", "Z29sYW5n"),
    robase64.Decode[string](base64.StdEncoding),
)

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

// Output:
// Next: [104 101 108 108 111]
// Next: [119 111 114 108 100]
// Next: [103 111 108 97 110 103]
// Completed

Encoding Types

Standard Encoding

Uses base64.StdEncoding for standard base64 encoding:

observable := ro.Pipe1(
    ro.Just([]byte("hello world")),
    robase64.Encode[[]byte](base64.StdEncoding),
)
URL-Safe Encoding

Uses base64.URLEncoding for URL-safe base64 encoding:

observable := ro.Pipe1(
    ro.Just([]byte("hello world")),
    robase64.Encode[[]byte](base64.URLEncoding),
)
Raw Standard Encoding

Uses base64.RawStdEncoding for standard base64 encoding without padding:

observable := ro.Pipe1(
    ro.Just([]byte("hello world")),
    robase64.Encode[[]byte](base64.RawStdEncoding),
)
Raw URL-Safe Encoding

Uses base64.RawURLEncoding for URL-safe base64 encoding without padding:

observable := ro.Pipe1(
    ro.Just([]byte("hello world")),
    robase64.Encode[[]byte](base64.RawURLEncoding),
)

Error Handling

The Decode operator handles errors gracefully and will emit error notifications for invalid base64 input:

observable := ro.Pipe1(
    ro.Just("aGVsbG8=", "invalid-base64", "d29ybGQ="),
    robase64.Decode[string](base64.StdEncoding),
)

subscription := observable.Subscribe(
    ro.NewObserver(
        func(value []byte) {
            // Handle successful decoding
        },
        func(err error) {
            // Handle decoding error
        },
        func() {
            // Handle completion
        },
    ),
)
defer subscription.Unsubscribe()

Roundtrip Examples

Demonstrate roundtrip encoding and decoding:

// Standard encoding roundtrip
observable := ro.Pipe2(
    ro.Just([]byte("hello world")),
    robase64.Encode[[]byte](base64.StdEncoding),
    robase64.Decode[string](base64.StdEncoding),
)

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

// Output:
// Next: [104 101 108 108 111 32 119 111 114 108 100]
// Completed

Real-world Example

Here's a practical example that processes binary data with base64 encoding:

import (
    "encoding/base64"
    "github.com/samber/ro"
    robase64 "github.com/samber/ro/plugins/encoding/base64"
)

// Process binary data with base64 encoding
pipeline := ro.Pipe3(
    // Simulate binary data
    ro.Just(
        []byte("user:password"),
        []byte("api:key123"),
        []byte("token:secret"),
    ),
    // Encode as base64
    robase64.Encode[[]byte](base64.StdEncoding),
    // Process encoded strings
    ro.Map(func(encoded string) string {
        return "Basic " + encoded
    }),
)

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

// Output:
// Next: Basic dXNlcjpwYXNzd29yZA==
// Next: Basic YXBpOmtleTEyMw==
// Next: Basic dG9rZW46c2VjcmV0
// Completed

Performance Considerations

  • The plugin uses Go's standard encoding/base64 package for all operations
  • Error handling is built into the Decode operator
  • Choose the appropriate encoding type for your use case:
    • StdEncoding for general base64 encoding
    • URLEncoding for URL-safe encoding
    • RawStdEncoding or RawURLEncoding to avoid padding characters
  • Consider the size of your data when encoding/decoding large byte slices

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode[T ~string](encoder *base64.Encoding) func(ro.Observable[T]) ro.Observable[[]byte]

Decode decodes the input from a base64 string.

Example:

ro.Pipe1(
	ro.Just("aGVsbG8="),
	robase64.Decode(base64.StdEncoding),
)

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

Example
// Decode base64 strings to byte slices
observable := ro.Pipe1(
	ro.Just("aGVsbG8=", "d29ybGQ=", "Z29sYW5n"),
	Decode[string](base64.StdEncoding),
)

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

Next: [104 101 108 108 111]
Next: [119 111 114 108 100]
Next: [103 111 108 97 110 103]
Completed
Example (WithError)
// Decode with potential errors
observable := ro.Pipe1(
	ro.Just("aGVsbG8=", "invalid-base64", "d29ybGQ="),
	Decode[string](base64.StdEncoding),
)

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

Next: [104 101 108 108 111]
Error: illegal base64 data at input byte 7
Example (WithURLEncoding)
// Decode using URL-safe base64 encoding
observable := ro.Pipe1(
	ro.Just("aGVsbG8gd29ybGQ=", "Z29sYW5nIHByb2dyYW1taW5n"),
	Decode[string](base64.URLEncoding),
)

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

Next: [104 101 108 108 111 32 119 111 114 108 100]
Next: [103 111 108 97 110 103 32 112 114 111 103 114 97 109 109 105 110 103]
Completed

func Encode

func Encode[T ~[]byte](encoder *base64.Encoding) func(ro.Observable[T]) ro.Observable[string]

Encode encodes the input into a base64 string.

Example:

ro.Pipe1(
	ro.Just([]byte("hello")),
	robase64.Encode(base64.StdEncoding),
)

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

Example
// Encode byte slices to base64 strings
observable := ro.Pipe1(
	ro.Just([]byte("hello"), []byte("world"), []byte("golang")),
	Encode[[]byte](base64.StdEncoding),
)

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

Next: aGVsbG8=
Next: d29ybGQ=
Next: Z29sYW5n
Completed
Example (WithURLEncoding)
// Encode using URL-safe base64 encoding
observable := ro.Pipe1(
	ro.Just([]byte("hello world"), []byte("golang programming")),
	Encode[[]byte](base64.URLEncoding),
)

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

Next: aGVsbG8gd29ybGQ=
Next: Z29sYW5nIHByb2dyYW1taW5n
Completed

Types

This section is empty.

Jump to

Keyboard shortcuts

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