jsonutils

package
v0.0.0-...-30a190e Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func JSONDiff

func JSONDiff(before, after any, opts *DiffOptions) (map[string]FieldChange, error)

JSONDiff compares two structs and returns a map of the changes

func MustToJSON

func MustToJSON(v any) map[string]any

MustToJSON converts any struct to a map[string]any, panics on error Use this only when you're absolutely certain the conversion won't fail

func MustToJSONString

func MustToJSONString(v any) string

MustToJSONString converts any struct to a JSON string, panics on error Use this only when you're absolutely certain the conversion won't fail

func ToJSON

func ToJSON(v any) (map[string]any, error)

ToJSON converts any struct to a map[string]any

func ToJSONString

func ToJSONString(v any) (string, error)

ToJSONString converts any struct to a JSON string

Types

type ChangeType

type ChangeType string

ChangeType represents the type of change that occurred

const (
	ChangeTypeCreated ChangeType = "created"
	ChangeTypeUpdated ChangeType = "updated"
	ChangeTypeDeleted ChangeType = "deleted"
)

type Comparator

type Comparator func(before, after any) (bool, error)

Comparator is a function type that defines custom comparison logic

type DiffOptions

type DiffOptions struct {
	IgnoreFields    []string // Fields to ignore during comparison
	CustomComparors map[string]Comparator
	MaxDepth        int  // Maximum depth for nested object comparison
	IgnoreCase      bool // Whether to ignore case in string comparisons
}

DiffOptions provides configuration options for the diff operation

func DefaultOptions

func DefaultOptions() *DiffOptions

DefaultOptions returns the default DiffOptions

type FieldChange

type FieldChange struct {
	From      any        `json:"from"`
	To        any        `json:"to"`
	Type      ChangeType `json:"type"`
	FieldType FieldType  `json:"fieldType"`
	Path      string     `json:"path"`
}

FieldChange represents a change in a single field with additional metadata

type FieldType

type FieldType string

FieldType represents the data type of the field

const (
	FieldTypeString    FieldType = "string"
	FieldTypeNumber    FieldType = "number"
	FieldTypeBoolean   FieldType = "boolean"
	FieldTypeArray     FieldType = "array"
	FieldTypeObject    FieldType = "object"
	FieldTypeDateTime  FieldType = "datetime"
	FieldTypeNull      FieldType = "null"
	FieldTypeUndefined FieldType = "undefined"
)

type JSONConverter

type JSONConverter interface {
	ToJSONString() (string, error)
	ToJSON() (map[string]any, error)
}

JSONConverter provides an interface for objects that can convert themselves to JSON

Jump to

Keyboard shortcuts

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