protobuf

package module
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package protobuf adapts Protobuf message decoders to the Lambda Kafka consumer.

New creates a Kafka field configuration from a Message. FromDescriptor supports generated or runtime descriptors through the official Go Protobuf implementation and returns fresh native messages. Custom Message.Decode callbacks receive the buffer and prefix position in Input.

Wire and ownership boundaries

Decoder handles the documented Confluent index conventions. Custom callbacks must validate input, be concurrency-safe and avoid mutable shared results. Native messages follow Go Protobuf semantics rather than protobufjs object defaults or JavaScript serialization.

The application owns descriptors and schema-registry configuration; this adapter does not fetch schemas. This module is an optional companion to core Kafka.

For usage and compatibility details, see the user guide.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(message *Message) *kafka.FieldConfig

New shares the process-wide index preference, as the reference module does.

Types

type Decoder

type Decoder struct {
	// contains filtered or unexported fields
}

Decoder retains the preferred Confluent index convention. The zero value starts with int32, like the reference. A successful fallback updates it atomically. No mutex is held while calling application code.

func (*Decoder) Deserialize

func (d *Decoder) Deserialize(ctx context.Context, data string, schema, metadata any) (any, error)

func (*Decoder) Field

func (d *Decoder) Field(message *Message) *kafka.FieldConfig

Field uses this decoder's preference, allowing explicit application isolation.

type Input

type Input struct {
	Buffer         []byte
	Position       int
	ExplicitLength bool
}

Input preserves the original buffer, prefix position and explicit-length flag. A negative position can occur with a malformed Confluent index in the reference. Custom decoders own their input validation, just as a supplied messageType does.

type Message

type Message struct {
	Decode      func(Input) (any, error)
	Description jsonv1.RawMessage
}

Message is the Go equivalent of a supplied messageType.decode method. Description is optional JSON used in diagnostic messages; it defaults to {}. The decoder must be concurrency-safe and must not retain mutable shared results.

func FromDescriptor

func FromDescriptor(descriptor protoreflect.MessageDescriptor) *Message

FromDescriptor supports generated and runtime descriptors through the official Go Protobuf implementation. Results are fresh proto.Message values; protobufjs object defaults and JavaScript serialization are not applied to native messages.

Example
package main

import (
	"context"
	"fmt"

	"github.com/rambow-cloud/powertools-lambda-go/kafka/protobuf"
	"google.golang.org/protobuf/proto"
	"google.golang.org/protobuf/types/known/wrapperspb"
)

func main() {
	message := protobuf.FromDescriptor(wrapperspb.String("").ProtoReflect().Descriptor())
	var decoder protobuf.Decoder
	value, err := decoder.Deserialize(context.Background(), "CgVoZWxsbw==", message, map[string]any{})
	if err != nil {
		panic(err)
	}
	decoded := value.(proto.Message).ProtoReflect()
	field := decoded.Descriptor().Fields().ByName("value")
	fmt.Println(decoded.Get(field).String())
}
Output:
hello

Jump to

Keyboard shortcuts

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