# README
changeset
Changeset mutator for REL. Changesets allow filtering, casting, validation and definition of constraints when manipulating structs.
Install
go get github.com/go-rel/changeset
Example
package main
import (
"time"
"github.com/go-rel/rel"
"github.com/go-rel/rel/adapter/mysql"
"github.com/go-rel/changeset"
"github.com/go-rel/changeset/params"
)
type Product struct {
ID int
Name string
Price int
CreatedAt time.Time
UpdatedAt time.Time
}
// ChangeProduct prepares data before database operation.
// Such as casting value to appropriate types and perform validations.
func ChangeProduct(product interface{}, params params.Params) *changeset.Changeset {
ch := changeset.Cast(product, params, []string{"name", "price"})
changeset.ValidateRequired(ch, []string{"name", "price"})
changeset.ValidateMin(ch, "price", 100)
return ch
}
func main() {
// initialize mysql adapter.
adapter, _ := mysql.Open(dsn)
defer adapter.Close()
// initialize rel's repo.
repo := rel.New(adapter)
var product Product
// Inserting Products.
// Changeset is used when creating or updating your data.
ch := ChangeProduct(product, params.Map{
"name": "shampoo",
"price": 1000,
})
if ch.Error() != nil {
// handle error
}
// Changeset can also be created directly from json string.
jsonch := ChangeProduct(product, params.ParseJSON(`{
"name": "soap",
"price": 2000,
}`))
// Create products with changeset and return the result to &product,
if err := repo.Insert(context.TODO(), &product, ch); err != nil {
// handle error
}
}
License
Released under the MIT License
# Packages
Package params defines different types of params used for changeset input.
# Functions
AddError adds an error to changeset.
ApplyString apply a function for string value.
Cast params as changes for the given data according to the permitted fields.
CastAssoc casts association changes using changeset function.
Change make a new changeset without changes and build from given schema.
ChangeOnly is used to define if validate is only check change.
CheckConstraint adds an unique constraint to changeset.
Code for changeset operation's error.
Convert a struct as changeset, every field's value will be treated as changes.
DeleteChange from changeset.
EmptyValues defines list of empty values when casting.
EscapeString escapes special characters like "<" to become "<".
Exact is used to define how index name is matched.
ForeignKeyConstraint adds an unique constraint to changeset.
Message for changeset operation's error.
Name is used to define index name of constraints.
PutAssoc to changeset.
PutChange to changeset.
PutDefault to changeset.
Required is used to define whether an assoc needs to be required or not.
SourceField to define used field name in params.
UnescapeString unescapes entities like "<" to become "<".
UniqueConstraint adds an unique constraint to changeset.
ValidateExclusion validates a change is not included in the given values.
ValidateInclusion validates a change is included in the given values.
ValidateMax validates the value of given field is not larger than max.
ValidateMin validates the value of given field is not smaller than min.
ValidatePattern validates the value of given field to match given pattern.
ValidateRange validates the value of given field is not larger than max and not smaller than min.
ValidateRegexp validates the value of given field to match given regexp.
ValidateRequired validates that one or more fields are present in the changeset.
# Variables
CastAssocErrorMessage is the default error message for CastAssoc when its invalid.
CastAssocRequiredMessage is the default error message for CastAssoc when its missing.
CastErrorMessage is the default error message for Cast.
CheckConstraintMessage is the default error message for CheckConstraint.
ForeignKeyConstraintMessage is the default error message for ForeignKeyConstraint.
PutAssocErrorMessage is the default error message for PutAssoc.
PutChangeErrorMessage is the default error message for PutChange.
PutDefaultErrorMessage is the default error message for PutDefault.
UniqueConstraintMessage is the default error message for UniqueConstraint.
ValidateExclusionErrorMessage is the default error message for ValidateExclusion.
ValidateInclusionErrorMessage is the default error message for ValidateInclusion.
ValidateMaxErrorMessage is the default error message for ValidateMax.
ValidateMinErrorMessage is the default error message for ValidateMin.
ValidatePatternErrorMessage is the default error message for ValidatePattern.
ValidateRangeErrorMessage is the default error message for ValidateRange.
ValidateRegexpErrorMessage is the default error message for ValidateRegexp.
ValidateRequiredErrorMessage is the default error message for ValidateRequired.
# Structs
Changeset used to cast and validate data before saving it to the database.
Constraint defines information to infer constraint error.
Error struct.
Options applicable to changeset.
# Type aliases
ChangeFunc is changeset function.
Constraints is slice of Constraint.
Option for changeset operation.