structsnapshot

package module
v0.0.0-...-f08e821 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2023 License: MIT Imports: 5 Imported by: 0

README

structsnapshot

structsnapshot is a Go package that allows you to take a snapshot of a struct, saving its fields, types, and tags in a JSON format. It provides functions to create a snapshot, convert it to JSON, save it to a file, and load a snapshot from a file.

Installing

go install github.com/gustavosbarreto/structsnapshot/cmd/structsnapshot@latest

Using

There are two methods of generating struct snapshots (see bellow).

Regardless of the method you choose, a special directory called __structsnapshots__ will be created in the current working directory. This directory will contain the generated struct snapshots as JSON files for each struct you have configured.

Add a go:generate comment before the struct:

//go:generate structsnapshot User
type User struct {
	Username string `json:"username" validate:"required"`
	Email    string `json:"email" validate:"required,email"`
	Timezone string `json:"timezone" validate:"timezone"`
}

Run go generate:

$ go generate

Standalone

Run the following command on the directory where the User struct is defined:

$ structsnapshot User

Unit testing

After generating struct snapshots, you need to ensure that your structs match the generated snapshots. To accomplish this, you can use the TakeSnapshot(struct) function to generate an in-memory snapshot of the struct. This snapshot can then be compared with the generated snapshot on the filesystem by calling LoadSnapshot(struct) function.

See example.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FieldSnapshot

type FieldSnapshot struct {
	Name string `json:"name"`
	Type string `json:"type"`
	Tag  string `json:"tag"`
}

FieldSnapshot represents a field of a struct

type Snapshot

type Snapshot struct {
	Name   string          `json:"name"`
	Fields []FieldSnapshot `json:"fields"`
}

Snapshot represents a snapshot of a struct

func LoadSnapshot

func LoadSnapshot(data interface{}) (*Snapshot, error)

LoadSnapshot loads a JSON snapshot from a file

func TakeSnapshot

func TakeSnapshot(data interface{}) (*Snapshot, error)

TakeSnapshot creates a snapshot of a struct

func (*Snapshot) SaveToFile

func (s *Snapshot) SaveToFile() error

func (*Snapshot) ToJSON

func (s *Snapshot) ToJSON() ([]byte, error)

ToJSON converts the snapshot to a JSON representation

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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