protojson

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2025 License: GPL-3.0 Imports: 11 Imported by: 1

Documentation

Index

Constants

View Source
const (
	ErrFieldNotHandled      = "field not handled on encoding: %s"
	ErrFieldNotProtoMessage = "field is not a proto message: %s"
)

Variables

View Source
var (
	ErrNilBody           = errors.New("body is nil")
	ErrNilMapper         = errors.New("encoder mapper is nil")
	ErrNilStructInstance = errors.New("struct instance is nil")
)

Functions

This section is empty.

Types

type Encoder

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

Encoder is the implementation of the Encoder interface

func NewEncoder

func NewEncoder(options *Options) *Encoder

NewEncoder creates a new Encoder instance

Parameters:

- options: the additional settings for the encoder implementation

Returns:

- *Encoder: the new Encoder instance

func (Encoder) Encode

func (e Encoder) Encode(
	body any,
) ([]byte, error)

Encode encodes the given body to JSON

Parameters:

  • body: The body to encode

Returns:

  • ([]byte, error): The encoded body and the error if any

func (Encoder) EncodeAndWrite

func (e Encoder) EncodeAndWrite(
	writer io.Writer,
	beforeWriteFn func() error,
	body any,
) error

EncodeAndWrite encodes and writes the given body to the writer

Parameters:

  • writer: The writer to write the encoded body to
  • beforeWriteFn: The function to call before writing the body
  • body: The body to encode

Returns:

- error: The error if any

func (Encoder) PrecomputeMarshal

func (e Encoder) PrecomputeMarshal(
	body any,
) (map[string]any, error)

PrecomputeMarshal precomputes the marshaled body by reflecting on the instance

Parameters:

- body: The body to precompute the marshaled body for

Returns:

- (map[string]any, error): The precomputed marshaled body and the error if any

type Mapper added in v0.2.0

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

Mapper is the protoJSON mapper struct

func NewMapper added in v0.2.0

func NewMapper(structInstance any) (*Mapper, error)

NewMapper creates a new protoJSON mapper

Parameters:

  • structInstance: instance of the struct to create the mapper from

Returns:

- *Mapper: instance of the mapper - error: error if the struct instance is nil

func (*Mapper) PrecomputeMarshalByReflection added in v0.2.0

func (m *Mapper) PrecomputeMarshalByReflection(
	body any,
	marshalOptions *protojson.MarshalOptions,
) (map[string]any, error)

PrecomputeMarshalByReflection marshals a struct to a map[string]any using reflection, handling nested proto.Message fields appropriately

Parameters:

  • body: The struct to marshal
  • marshalOptions: The protojson.MarshalOptions to use (optional)

Returns:

  • map[string]any: The marshaled struct as a map
  • error: The error if any

type Options added in v0.2.0

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

Options are the additional settings for the encoder implementation

func NewOptions added in v0.2.0

func NewOptions(
	cache bool,
) *Options

NewOptions creates a new Options instance

Parameters:

- cache: indicates whether to cache the precompute marshal by reflection functions

Returns:

- *Options: the new Options instance

Jump to

Keyboard shortcuts

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