expr

package
v1.0.23 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

README

Expression Builder

The expr package provides a builder for constructing DynamoDB expressions with proper handling of reserved words, placeholders, and complex operations.

Key Features

  • Automatic placeholder generation for attribute names and values
  • Reserved word detection and escaping
  • Support for all DynamoDB expression types
  • Function-based update operations (list_append, if_not_exists)

Available Methods

Query Operations
  • AddKeyCondition(field, operator, value) - Add key conditions for Query operations
  • AddFilterCondition(logicalOp, field, operator, value) - Add filter conditions with AND/OR logic
  • AddGroupFilter(logicalOp, components) - Add grouped filter expressions
  • AddProjection(fields...) - Add projection expressions
Update Operations
  • AddUpdateSet(field, value) - SET field = value
  • AddUpdateAdd(field, value) - ADD field value (atomic counters)
  • AddUpdateRemove(field) - REMOVE field
  • AddUpdateDelete(field, value) - DELETE value FROM field (for sets)
  • AddUpdateFunction(field, function, args...) - Function-based updates
Supported Functions
list_append

Appends or prepends values to a list:

// Append to end: SET field = list_append(field, :value)
builder.AddUpdateFunction("Tags", "list_append", "Tags", []string{"new"})

// Prepend to beginning: SET field = list_append(:value, field)  
builder.AddUpdateFunction("Tags", "list_append", []string{"new"}, "Tags")
if_not_exists

Sets a field only if it doesn't exist:

// SET field = if_not_exists(field, :default)
builder.AddUpdateFunction("Description", "if_not_exists", "Description", "Default text")
Condition Operations
  • AddConditionExpression(field, operator, value) - Add conditions for conditional updates

Supported Operators

  • Comparison: =, !=, <>, <, <=, >, >=
  • Range: BETWEEN
  • Membership: IN
  • String: BEGINS_WITH, CONTAINS
  • Existence: EXISTS, NOT_EXISTS

Example Usage

builder := expr.NewBuilder()

// Add key condition
builder.AddKeyCondition("UserID", "=", "user123")

// Add filter
builder.AddFilterCondition("AND", "Status", "=", "active")

// Add update with list append
builder.AddUpdateSet("Name", "New Name")
builder.AddUpdateFunction("Tags", "list_append", "Tags", []string{"updated"})

// Build the expressions
components := builder.Build()

// Use components in DynamoDB operations
// components.KeyConditionExpression
// components.FilterExpression  
// components.UpdateExpression
// components.ExpressionAttributeNames
// components.ExpressionAttributeValues

Reserved Words

The builder automatically detects and escapes DynamoDB reserved words by prefixing them with #. All 600+ reserved words are handled, including common ones like:

  • STATUS, STATE, NAME, TYPE
  • USER, GROUP, ROLE
  • DATA, VALUE, KEY
  • And many more...

Thread Safety

The Builder is not thread-safe. Create a new instance for each operation or use proper synchronization.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertFromAttributeValue

func ConvertFromAttributeValue(av types.AttributeValue, target any) error

ConvertFromAttributeValue converts a DynamoDB AttributeValue to a Go value

func ConvertToAttributeValue

func ConvertToAttributeValue(value any) (types.AttributeValue, error)

ConvertToAttributeValue converts a Go value to a DynamoDB AttributeValue

func ConvertToAttributeValueSecure added in v1.0.18

func ConvertToAttributeValueSecure(value any) (types.AttributeValue, error)

ConvertToAttributeValueSecure converts a value to AttributeValue with security checks

Types

type Builder

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

Builder compiles expressions for DynamoDB operations

func NewBuilder

func NewBuilder() *Builder

NewBuilder creates a new expression builder

func (*Builder) AddAdvancedFunction

func (b *Builder) AddAdvancedFunction(function string, field string, args ...any) (string, error)

AddAdvancedFunction adds support for DynamoDB functions

func (*Builder) AddConditionExpression

func (b *Builder) AddConditionExpression(field string, operator string, value any) error

AddConditionExpression adds a condition for conditional updates

func (*Builder) AddFilterCondition

func (b *Builder) AddFilterCondition(logicalOp, field, operator string, value any) error

AddFilterCondition adds a filter condition expression

func (*Builder) AddGroupFilter

func (b *Builder) AddGroupFilter(logicalOp string, components ExpressionComponents)

AddGroupFilter adds a grouped filter expression

func (*Builder) AddKeyCondition

func (b *Builder) AddKeyCondition(field string, operator string, value any) error

AddKeyCondition adds a key condition expression

func (*Builder) AddProjection

func (b *Builder) AddProjection(fields ...string)

AddProjection adds fields to the projection expression

func (*Builder) AddUpdateAdd

func (b *Builder) AddUpdateAdd(field string, value any)

AddUpdateAdd adds an ADD update expression (for numeric increment)

func (*Builder) AddUpdateDelete

func (b *Builder) AddUpdateDelete(field string, value any)

AddUpdateDelete adds a DELETE update expression (for removing elements from a set)

func (*Builder) AddUpdateFunction

func (b *Builder) AddUpdateFunction(field string, function string, args ...any) error

AddUpdateFunction adds a function-based update expression (e.g., list_append)

func (*Builder) AddUpdateRemove

func (b *Builder) AddUpdateRemove(field string)

AddUpdateRemove adds a REMOVE update expression

func (*Builder) AddUpdateSet

func (b *Builder) AddUpdateSet(field string, value any)

AddUpdateSet adds a SET update expression

func (*Builder) Build

func (b *Builder) Build() ExpressionComponents

Build compiles all expressions and returns the final components

type ExpressionComponents

type ExpressionComponents struct {
	KeyConditionExpression    string
	FilterExpression          string
	ProjectionExpression      string
	UpdateExpression          string
	ConditionExpression       string
	ExpressionAttributeNames  map[string]string
	ExpressionAttributeValues map[string]types.AttributeValue
}

ExpressionComponents holds all expression components

type Marshaler

type Marshaler interface {
	MarshalDynamoDBAttributeValue() (types.AttributeValue, error)
}

Marshaler interface for custom marshaling

type Unmarshaler

type Unmarshaler interface {
	UnmarshalDynamoDBAttributeValue(av types.AttributeValue) error
}

Unmarshaler interface for custom unmarshaling

Jump to

Keyboard shortcuts

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