Skip to content

ryeguard/ddbcalc

Repository files navigation

ddbcalc

Go Reference Go Report Card 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

CLI

The ddbcalc command-line tool can be used to calculate the size of a json file from the terminal.

Install the tool with the following command:

go install github.com/ryeguard/ddbcalc/cmd/ddbcalc@latest

You can then run the tool with the following command:

ddbcalc -file path/to/file.json

Package

By importing github.com/ryeguard/ddbcalc, you can use the modules's exported functions in your Go code.

For example, 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 any *_test.go files.

Contributing

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

License

This project is licensed under the MIT License. However, it includes dependencies licensed under the Apache License 2.0. See the LICENSE and NOTICE files for details.