ddbcalc

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2024 License: MIT Imports: 3 Imported by: 0

README

ddbcalc

Go Coverage

A DynamoDB item size calculator.

This package has no dependencies other than AWS's aws-sdk-go-v2.

Raison d'être

DynamoDB has a limit of 400KB for the size of an item. The ddbcalc package provides functionality to calculate the size of any Go struct in bytes. This can be useful when you need to ensure that the size of an item does not exceed the limit, either through tests or at runtime.

For more info, please see the blog post at rygard.se.

Usage

The StructSizeInBytes function calculates the size of a struct in bytes. It may be used as follows:

package main

import (
	"fmt"

	"github.com/ryeguard/ddbcalc"
)

type Item struct {
	ID    string
	Name  string
	Age   int
	Email string
}

func main() {
	item := Item{
		ID:    "123",
		Name:  "John Doe",
		Age:   30,
		Email: "john@example.com",
	}

	size, err := ddbcalc.StructSizeInBytes(item)
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Printf("Item size: %d bytes\n", size)
}

You can run the example above with the following command:

go run example/basic/main.go

For more examples, see the examples directory and also check out the tests in all *_test.go files.

Contributing

Contributions are welcome in any shape or form. Please feel free to open an issue or a pull request.

Documentation

Index

Constants

View Source
const SizeLimitInBytes = 400_000 // 400 KB

Variables

This section is empty.

Functions

func MapSizeInBytes

func MapSizeInBytes(m map[string]types.AttributeValue) (int, error)

MapSizeInBytes returns the size of a map of AttributeValue in bytes.

func StructSizeInBytes

func StructSizeInBytes(s interface{}) (int, error)

StructSizeInBytes returns the size of a struct in bytes. It is a convenience function as an alternative to first calling attributevalue.MarshalMap and then MapSizeInBytes.

Types

This section is empty.

Directories

Path Synopsis
cmd
examples

Jump to

Keyboard shortcuts

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