marshal

package
v6.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Overview

Package marshal provides optimized marshaling for DynamoDB

Package marshal provides safe marshaling for DynamoDB without unsafe operations

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MarshalItemForWriterPath

func MarshalItemForWriterPath(model any, metadata *model.Metadata, path ItemWriterPath) (map[string]types.AttributeValue, error)

MarshalItemForWriterPath returns the DynamoDB item that the named writer path stores for model, so a caller can measure the bytes it is about to write.

The returned item is byte-equal to a payload captured from the real writer: ItemWriterPathPut matches a Model().Create or BatchCreate capture, and ItemWriterPathTransactWrite matches a Transact().Put or Transact().Create capture. Both are pinned by regression tests that capture live payloads through a fake session, so neither answer can drift from its writer without failing the build.

metadata must be the registered metadata for model's type, the same value SafeMarshaler.MarshalItem takes, so both writers and the preflight read the same field set, names and `omitempty` flags.

Scope:

  • The preflight reproduces attribute serialization with TableTheory's default type converter. Custom converters registered through RegisterTypeConverter live on the database's converter, which is not reachable from a standalone preflight; a model whose serialization depends on one of those converters is not covered.
  • Models that declare `encrypted` attributes are refused with an error. The stored bytes of an encrypted attribute depend on KMS key material and envelope randomness, so no static preflight can predict them.
  • A model that declares `createdAt` or `updatedAt` is serialized with the current time, exactly as the writers do; callers that need a fixed timestamp should pass a model whose lifecycle fields are unset.

An unrecognized path is refused with an error. The preflight has no side effects and never contacts DynamoDB.

func SetGlobalConfig

func SetGlobalConfig(config Config)

SetGlobalConfig sets the global marshaler configuration

func ValidateConfig

func ValidateConfig(config Config) error

ValidateConfig validates marshaler configuration for security compliance

Types

type Config

type Config struct {
	// MarshalerType specifies which marshaler to use (default: safe)
	MarshalerType MarshalerType `json:"marshaler_type" yaml:"marshaler_type"`

	// AllowUnsafeMarshaler must be explicitly set to true to enable unsafe operations
	// This flag is not serialized to prevent accidental persistence
	AllowUnsafeMarshaler bool `json:"-" yaml:"-"`

	// RequireExplicitUnsafeAck requires explicit acknowledgment of security risks
	RequireExplicitUnsafeAck bool `json:"require_explicit_unsafe_ack" yaml:"require_explicit_unsafe_ack"`

	// WarnOnUnsafeUsage logs warnings when unsafe marshaler is used
	WarnOnUnsafeUsage bool `json:"warn_on_unsafe_usage" yaml:"warn_on_unsafe_usage"`
}

Config holds marshaler configuration with security defaults

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a secure default configuration

func GetGlobalConfig

func GetGlobalConfig() Config

GetGlobalConfig returns the current global configuration

type ItemWriterPath

type ItemWriterPath string

ItemWriterPath names a TableTheory writer path whose stored DynamoDB representation an item preflight must reproduce.

TableTheory serializes a model through two different writers, and for a nil collection written without `omitempty` those writers store different representations. The put family (Model().Create, Model().CreateOrUpdate, BatchCreate, BatchCreateWithResult and the puts issued by BatchWrite) serializes through SafeMarshaler, which stores DynamoDB NULL for a nil slice or a nil map. The transactional family (Transact().Put, Transact().Create) serializes through the transaction converter, which stores an empty list (L) or an empty map (M) instead.

Each writer path's stored representation is a permanent compatibility contract: it is frozen by the operator's policy that TableTheory supports its consumers rather than changing stored data, so no writer's output may change. ItemWriterPath selects which frozen representation MarshalItemForWriterPath returns, so a caller measures exactly what the named writer stores. Neither writer is edited by this preflight.

const (
	// ItemWriterPathPut selects the put-family writer paths: Model().Create,
	// Model().CreateOrUpdate, BatchCreate, BatchCreateWithResult and the puts
	// issued by BatchWrite.
	//
	// Those paths store the SafeMarshaler representation. In it, a nil slice or
	// nil map written without `omitempty` becomes DynamoDB NULL, a non-nil empty
	// slice becomes an empty list (L), and a non-nil empty map becomes an empty
	// map (M).
	ItemWriterPathPut ItemWriterPath = "put"

	// ItemWriterPathTransactWrite selects the transactional writer paths:
	// Transact().Put and Transact().Create.
	//
	// Those paths store the transaction converter representation. In it, a nil
	// slice written without `omitempty` becomes an empty list (L) and a nil map
	// becomes an empty map (M); a non-nil empty slice or map is stored the same
	// way.
	ItemWriterPathTransactWrite ItemWriterPath = "transact_write"
)

type Marshaler

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

Marshaler provides high-performance marshaling to DynamoDB AttributeValues

func New

func New(converter *pkgTypes.Converter) *Marshaler

New creates a new optimized marshaler. If a converter is provided it will be consulted for custom type conversions during marshaling.

func (*Marshaler) ClearCache

func (m *Marshaler) ClearCache()

ClearCache removes all cached struct marshalers. This is useful when new custom converters are registered and previously compiled functions need to be rebuilt.

func (*Marshaler) MarshalItem

func (m *Marshaler) MarshalItem(model any, metadata *model.Metadata) (map[string]types.AttributeValue, error)

MarshalItem marshals a model to DynamoDB AttributeValues using cached reflection

type MarshalerFactory

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

MarshalerFactory creates marshalers with security controls

func NewMarshalerFactory

func NewMarshalerFactory(config Config) *MarshalerFactory

NewMarshalerFactory creates a new factory with the given configuration

func (*MarshalerFactory) NewMarshaler

func (f *MarshalerFactory) NewMarshaler() (MarshalerInterface, error)

NewMarshaler creates a marshaler based on configuration

func (*MarshalerFactory) NewMarshalerWithAcknowledgment

func (f *MarshalerFactory) NewMarshalerWithAcknowledgment(ack *SecurityAcknowledgment) (MarshalerInterface, error)

NewMarshalerWithAcknowledgment creates a marshaler with explicit security acknowledgment

func (*MarshalerFactory) WithConverter

func (f *MarshalerFactory) WithConverter(converter *pkgTypes.Converter) *MarshalerFactory

WithConverter sets the converter used for creating unsafe marshalers.

func (*MarshalerFactory) WithNowFunc

func (f *MarshalerFactory) WithNowFunc(now func() time.Time) *MarshalerFactory

type MarshalerInterface

type MarshalerInterface interface {
	MarshalItem(model any, metadata *model.Metadata) (map[string]types.AttributeValue, error)
}

MarshalerInterface defines the common interface for all marshalers

type MarshalerType

type MarshalerType string

MarshalerType defines the type of marshaler to use

const (
	// SafeMarshalerType uses reflection-based marshaling (RECOMMENDED)
	SafeMarshalerType MarshalerType = "safe"

	// UnsafeMarshalerType uses unsafe pointer operations (DEPRECATED)
	// Will be removed in v2.0
	UnsafeMarshalerType MarshalerType = "unsafe"
)

type SafeMarshaler

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

SafeMarshaler provides memory-safe marshaling implementation without unsafe operations This is the default marshaler and should be used in production environments

SafeMarshaler is a writer, not a neutral serialization helper: its MarshalItem output is what the put family stores — Model().Create, Model().CreateOrUpdate, BatchCreate, BatchCreateWithResult and the puts issued by BatchWrite. A transactional writer path does not use it and stores a different representation for a nil collection written without `omitempty`, so calling this method to predict what Transact().Put or Transact().Create writes measures the wrong bytes. Use MarshalItemForWriterPath to name the writer path a preflight must reproduce.

func NewSafeMarshaler

func NewSafeMarshaler() *SafeMarshaler

NewSafeMarshaler creates a new safe marshaler (recommended for production)

func NewSafeMarshalerWithConverter

func NewSafeMarshalerWithConverter(converter *pkgTypes.Converter) *SafeMarshaler

NewSafeMarshalerWithConverter creates a safe marshaler that consults the provided converter for registered custom type conversions.

func (*SafeMarshaler) MarshalItem

func (m *SafeMarshaler) MarshalItem(model any, metadata *model.Metadata) (map[string]types.AttributeValue, error)

MarshalItem safely marshals a model to DynamoDB AttributeValues using only reflection This implementation prioritizes security over performance but is still highly optimized

type SecurityAcknowledgment

type SecurityAcknowledgment struct {
	DeveloperSignature              string
	Timestamp                       int64
	AcknowledgeMemoryCorruptionRisk bool
	AcknowledgeSecurityVulnerable   bool
	AcknowledgeDeprecationWarning   bool
}

SecurityAcknowledgment represents explicit acknowledgment of unsafe marshaler risks

func CreateSecurityAcknowledgment

func CreateSecurityAcknowledgment(developerSignature string) *SecurityAcknowledgment

CreateSecurityAcknowledgment creates a security acknowledgment for unsafe marshaler usage

type SecurityStats

type SecurityStats struct {
	CurrentConfig    Config
	UnsafeUsageCount int64
	SecurityWarnings int64
}

SecurityStats contains security monitoring information

func GetSecurityStats

func GetSecurityStats() SecurityStats

GetSecurityStats returns security-related statistics

Jump to

Keyboard shortcuts

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