rogob

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: 3 Imported by: 0

README

Gob Encoding Plugin

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

Installation

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

Operators

Encode

Encodes values to gob bytes using Go's binary serialization format.

import (
    "github.com/samber/ro"
    rogob "github.com/samber/ro/plugins/encoding/gob"
)

type Person struct {
    Name string
    Age  int
}

observable := ro.Pipe1(
    ro.Just(
        Person{Name: "Alice", Age: 30},
        Person{Name: "Bob", Age: 25},
        Person{Name: "Charlie", Age: 35},
    ),
    rogob.Encode[Person](),
)

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

// Output:
// Next: [15 255 129 3 1 1 6 80 101 114 115 111 110 1 255 130 0 1 2 1 4 78 97 109 101 1 12 0 1 3 65 103 101 1 4 0 0 0 34 255 130 1 5 65 108 105 99 101 1 60 0 0 0 33 255 130 1 3 66 111 98 1 50 0 0 0 34 255 130 1 7 67 104 97 114 108 105 101 1 70 0 0]
// Completed
Decode

Decodes gob bytes to values using Go's binary deserialization format.

encoded := []byte{15, 255, 129, 3, 1, 1, 6, 80, 101, 114, 115, 111, 110, 1, 255, 130, 0, 1, 2, 1, 4, 78, 97, 109, 101, 1, 12, 0, 1, 3, 65, 103, 101, 1, 4, 0, 0, 0, 34, 255, 130, 1, 5, 65, 108, 105, 99, 101, 1, 60, 0, 0, 0, 33, 255, 130, 1, 3, 66, 111, 98, 1, 50, 0, 0, 0, 34, 255, 130, 1, 7, 67, 104, 97, 114, 108, 105, 101, 1, 70, 0, 0}

observable := ro.Pipe1(
    ro.Just(encoded),
    rogob.Decode[Person](),
)

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

// Output:
// Next: {Alice 30}
// Completed

Supported Types

The gob plugin supports encoding and decoding of all Go types that are supported by the encoding/gob package:

Basic Types
// Strings
observable := ro.Pipe1(
    ro.Just("hello", "world", "golang"),
    rogob.Encode[string](),
)

// Integers
observable := ro.Pipe1(
    ro.Just(1, 2, 3, 4, 5),
    rogob.Encode[int](),
)

// Floats
observable := ro.Pipe1(
    ro.Just(3.14, 2.718, 1.414),
    rogob.Encode[float64](),
)

// Booleans
observable := ro.Pipe1(
    ro.Just(true, false, true),
    rogob.Encode[bool](),
)
Complex Types
// Structs
type User struct {
    ID   int
    Name string
    Tags []string
}

observable := ro.Pipe1(
    ro.Just(User{ID: 1, Name: "Alice", Tags: []string{"admin", "user"}}),
    rogob.Encode[User](),
)

// Maps
observable := ro.Pipe1(
    ro.Just(map[string]int{"a": 1, "b": 2, "c": 3}),
    rogob.Encode[map[string]int](),
)

// Slices
observable := ro.Pipe1(
    ro.Just([]int{1, 2, 3, 4, 5}),
    rogob.Encode[[]int](),
)

Error Handling

Both Encode and Decode operators handle errors gracefully and will emit error notifications for invalid operations:

// Encoding error (e.g., unsupported type)
observable := ro.Pipe1(
    ro.Just(make(chan int)), // Channels are not supported by gob
    rogob.Encode[chan int](),
)

// Decoding error (e.g., invalid gob data)
observable := ro.Pipe1(
    ro.Just([]byte("invalid-gob-data")),
    rogob.Decode[Person](),
)

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

Roundtrip Examples

Demonstrate roundtrip encoding and decoding:

// Struct roundtrip
observable := ro.Pipe2(
    ro.Just(Person{Name: "Alice", Age: 30}),
    rogob.Encode[Person](),
    rogob.Decode[Person](),
)

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

// Output:
// Next: {Alice 30}
// Completed

Real-world Example

Here's a practical example that serializes user data for storage:

import (
    "github.com/samber/ro"
    rogob "github.com/samber/ro/plugins/encoding/gob"
)

type User struct {
    ID       int
    Username string
    Email    string
    Settings map[string]interface{}
}

// Serialize users for storage
pipeline := ro.Pipe3(
    // Simulate user data
    ro.Just(
        User{
            ID:       1,
            Username: "alice",
            Email:    "alice@example.com",
            Settings: map[string]interface{}{"theme": "dark", "notifications": true},
        },
        User{
            ID:       2,
            Username: "bob",
            Email:    "bob@example.com",
            Settings: map[string]interface{}{"theme": "light", "notifications": false},
        },
    ),
    // Encode to gob bytes
    rogob.Encode[User](),
    // Process encoded data (e.g., store to database)
    ro.Map(func(data []byte) string {
        return "stored: " + string(data)
    }),
)

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

Performance Considerations

  • The plugin uses Go's standard encoding/gob package for all operations
  • Gob encoding is efficient for Go-specific data structures
  • Error handling is built into both Encode and Decode operators
  • Gob format is binary and more compact than text-based formats like JSON
  • Gob is Go-specific and not suitable for cross-language communication
  • Consider the size of your data when encoding/decoding large structures
  • Gob encoding includes type information, making it self-describing

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode[T any]() func(ro.Observable[[]byte]) ro.Observable[T]

Decode decodes gob binary data to values. Play: https://go.dev/play/p/cH3AiWEwFQe

Example
// Decode gob bytes to a struct
encoded := []byte{37, 255, 141, 3, 1, 1, 6, 80, 101, 114, 115, 111, 110, 1, 255, 142, 0, 1, 2, 1, 4, 78, 97, 109, 101, 1, 12, 0, 1, 3, 65, 103, 101, 1, 4, 0, 0, 0, 12, 255, 142, 1, 5, 65, 108, 105, 99, 101, 1, 60, 0}

observable := ro.Pipe1(
	ro.Just(encoded),
	Decode[Person](),
)

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

Next: {Alice 30}
Completed
Example (WithError)
// Decode with potential errors
observable := ro.Pipe1(
	ro.Just([]byte("invalid-gob-data")),
	Decode[Person](),
)

subscription := observable.Subscribe(
	ro.NewObserver(
		func(value Person) {
			// Handle successful decoding
		},
		func(err error) {
			// Handle decoding error
			fmt.Println("Error:", err.Error())
		},
		func() {
			// Handle completion
		},
	),
)
defer subscription.Unsubscribe()
Output:

Error: unexpected EOF
Example (WithMaps)
// Decode gob bytes to a map
encoded := []byte{14, 255, 143, 4, 1, 2, 255, 144, 0, 1, 12, 1, 4, 0, 0, 10, 255, 144, 0, 2, 1, 97, 2, 1, 98, 4}

observable := ro.Pipe1(
	ro.Just(encoded),
	Decode[map[string]int](),
)

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

Next: map[a:1 b:2]
Completed
Example (WithSimpleTypes)
// Decode gob bytes to a string
encoded := []byte{8, 12, 0, 5, 104, 101, 108, 108, 111}

observable := ro.Pipe1(
	ro.Just(encoded),
	Decode[string](),
)

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

Next: hello
Completed

func Encode

func Encode[T any]() func(ro.Observable[T]) ro.Observable[[]byte]

Encode encodes values to gob binary format. Play: https://go.dev/play/p/HdU4DMTagoA

Example
// Encode a single struct to gob bytes
observable := ro.Pipe1(
	ro.Just(42),
	Encode[int](),
)

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

Next: [3 4 0 84]
Completed
Example (WithMaps)
// Encode a single map to gob bytes
observable := ro.Pipe1(
	ro.Just(1),
	Encode[int](),
)

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

Next: [3 4 0 2]
Completed
Example (WithSimpleTypes)
// Encode a single string to gob bytes
observable := ro.Pipe1(
	ro.Just("hello"),
	Encode[string](),
)

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

Next: [8 12 0 5 104 101 108 108 111]
Completed

Types

This section is empty.

Jump to

Keyboard shortcuts

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