mut

package module
v0.24.0 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

README

Package mut

A utility package for Go that provides the generic Mut[T] type to handle Partial Updates, State Tracking, and Patch operations via JSON.

Features

  • Generic Mut[T]: Works with any data type using Go Generics.
  • Dirty Tracking: Explicitly tracks if a value was assigned or modified.
  • JSON Native: Seamlessly integrates with encoding/json.
  • SQL Ready: Convert structs to maps for UPDATE operations using ToMap.
  • Minimalist API: Focused on the Get, Set, and Dirty contract via the Mutable interface.

Installation

go get github.com/leandroluk/gox/mut

Usage

type UserUpdate struct {
    Name  mut.Mut[string] `json:"name"`
    Age   mut.Mut[int]    `json:"age"`
}
func main() {
    userUpdate := &UserUpdate{}
    userUpdate.Name.Set("Leandro")
    userUpdate.Age.Set(30)

    if userUpdate.Name.Dirty() {
        fmt.Println("UserUpdate.Name:", userUpdate.Name.Get())
    }

    if userUpdate.Age.Dirty() {
        fmt.Println("UserUpdate.Age:", userUpdate.Age.Get())
    }

    data, _ := json.Marshal(userUpdate)
    fmt.Println("JSON:", string(data))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToMap

func ToMap(obj any) map[string]any

ToMap converts structs with Value fields into map[string]any, including only dirty fields.

Types

type Value added in v0.18.9

type Value[T any] struct {
	// contains filtered or unexported fields
}

Value is the structure that tracks the value state.

func New

func New[T any](val ...T) Value[T]

New creates a new Value instance. If an initial value is provided, it starts as Dirty.

func (*Value[T]) Apply added in v0.24.0

func (v *Value[T]) Apply(ptr *T)

Apply updates the value of ptr if the value is dirty.

func (*Value[T]) Get added in v0.18.9

func (v *Value[T]) Get() T

Get returns the stored value.

func (*Value[T]) GetAny added in v0.18.9

func (v *Value[T]) GetAny() any

GetAny is a bridge method for ToMap using reflection.

func (*Value[T]) IsDirty added in v0.18.11

func (v *Value[T]) IsDirty() bool

IsDirty returns true if the value has been modified.

func (*Value[T]) MarshalJSON added in v0.18.9

func (v *Value[T]) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*Value[T]) OnDirty added in v0.18.11

func (v *Value[T]) OnDirty(then func(T))

OnDirty runs the then function if the value is dirty.

func (*Value[T]) Set added in v0.18.9

func (v *Value[T]) Set(value T)

Set updates the value and marks it as dirty.

func (*Value[T]) UnmarshalJSON added in v0.18.9

func (v *Value[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface and marks the value as dirty.

Jump to

Keyboard shortcuts

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