dataobject

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 28, 2023 License: MIT Imports: 5 Imported by: 0

README

Data Object Open in Gitpod

A data object is a special purpose structure that is designed to hold data and track the changes to allow efficient serialization to a data store.

It follows the following principles:

  1. Has a default, non-argument constructor
  2. Has a unique identifier (ID) allowing to safely find the object among any other
  3. All the fields are private, thus non-modifiable from outside
  4. The fields are only accessed via public setter (mutator) and getter (accessor) methods
  5. All changes are tracked, and returned on request as a map of type map[string]string
  6. All data can be returned on request as a map of type map[string]string

Any object can be considered a data object as long as it adheres to the above principles, regardless of its specific implementation.

The implemetation in this repo is just one way to implement the above principles. Other variations are possible to suit specific needs.

The concept is a bit similar to a POJO and a Java Bean.

Usage

For an object using the above specifications

// Create new user with autogenerated default ID
user := NewUser()

// Create new user with already existing data (i.e. from database)
user := NewUserFromData(data)

// returns the ID of the object
id := user.ID()

// example setter method
user.SetFirstName("John")

// example getter method
firstName := user.FirstName()

// find if the object has been modified
isDirty := user.IsDirty()

// reurns the changed data
dataChanged := user.DataChanged()

// reurns all the data
data := user.Data()

Saving data

Saving the data is left to the end user, as it is specific for each data store.

Some stores (i.e. relational databases) allow to only change specific fields, which is why the DataChanged() getter method should be used to identify the changed fields, then only save the changes efficiently.

func SaveUserChanges(user User) bool {
    if !user.IsDirty() {
        return true
    }

    changedData := user.DataChanged()

    return save(changedData)
}

Other stores (i.e. document stores, file stores) save all the fields each time, which is where the Data() getter method should be used

func SaveFullUser(user User) bool {
    if !user.IsDirty() {
        return true
    }

    allData := user.Data()

    return save(allData)
}

Serialize to JSON

jsonString, err := user.ToJSON()


if err != nil {
    log.Fatal("Error serializing")
}

log.Println(jsonString)

Unserialize from JSON

user, err := NewDataObjectFromJSON(jsonString)

if err != nil {
    log.Fatal("Error unserializing")
}

user.Get("first_name")

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DataObject

type DataObject struct {
	// contains filtered or unexported fields
}

func NewDataObject

func NewDataObject() *DataObject

NewDataObject creates a new data object and generates an ID

func NewDataObjectFromExistingData

func NewDataObjectFromExistingData(data map[string]string) *DataObject

NewDataObjectFromExistingData creates a new data object and hydrates it with the passed data

func NewDataObjectFromJSON added in v0.1.0

func NewDataObjectFromJSON(jsonString string) (do *DataObject, err error)

func (*DataObject) Data

func (do *DataObject) Data() map[string]string

Data returns all the data of the object

func (*DataObject) DataChanged

func (do *DataObject) DataChanged() map[string]string

DataChanged returns only the modified data

func (*DataObject) Get

func (do *DataObject) Get(key string) string

Get helper getter method

func (*DataObject) Hydrate

func (do *DataObject) Hydrate(data map[string]string)

Hybernate sets the data for the object without marking as dirty

func (*DataObject) ID

func (do *DataObject) ID() string

func (*DataObject) Init

func (do *DataObject) Init()

func (*DataObject) IsDirty

func (do *DataObject) IsDirty() bool

IsDirty returns if data has been modified

func (*DataObject) Set

func (do *DataObject) Set(key string, value string)

Set helper setter method

func (*DataObject) SetData

func (do *DataObject) SetData(data map[string]string) DataObjectFluentInterface

SetData sets the data for the object and marks it as dirty see Hydrate for dirtyless assignment

func (*DataObject) SetID

func (*DataObject) ToJSON added in v0.1.0

func (do *DataObject) ToJSON() (string, error)

type DataObjectFluentInterface

type DataObjectFluentInterface interface {

	// ID returns the ID of the object
	ID() string

	// SetID sets the ID of the object
	SetID(id string) DataObjectFluentInterface

	// GetData returns the data for the object
	Data() map[string]string

	// GetChangedData returns the data that has been changed since the last hydration
	DataChanged() map[string]string

	// Hydrates the data object with data
	Hydrate(map[string]string)
}

DataObjectFluentInterface is an interface for a fluent data object

type DataObjectInterface

type DataObjectInterface interface {

	// ID returns the ID of the object
	ID() string

	// SetID sets the ID of the object
	SetID(id string)

	// GetData returns the data for the object
	Data() map[string]string

	// GetChangedData returns the data that has been changed since the last hydration
	DataChanged() map[string]string

	// Hydrates the data object with data
	Hydrate(map[string]string)
}

DataObjectInterface is an interface for a data object

type DataObjectRepositoryInterface

type DataObjectRepositoryInterface interface {
	Create(dataObject DataObjectInterface) error

	Find(id string) (DataObjectInterface, error)

	List() ([]DataObjectInterface, error)

	Update(dataObject DataObjectInterface) error
}

Jump to

Keyboard shortcuts

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