Giter Site home page Giter Site logo

changeset's Introduction

changeset

GoDoc Build Status Go Report Card codecov Gitter chat

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

changeset's People

Contributors

dependabot[bot] avatar fs02 avatar talkanbaev-artur avatar imansohibul avatar deepsource-autofix[bot] avatar

Stargazers

Brooke avatar Dimitar Panayotov avatar lJOSVDE avatar  avatar  avatar Basel Ajarmah avatar Philipp Kübler avatar  avatar chikamim avatar Zhuojie Zhou avatar Waleed AlMalki avatar

Watchers

James Cloos avatar  avatar  avatar

changeset's Issues

Changeset apply function ignores non-serial primary field during insertion

Hi!
It seems that changeset struct only accepts the serial primary keys. In case of setting a custom primary key, it never gets inserted:

if field != pField && scannable(c.types[field]) {

To reproduce:

  1. create the table with string primary key
  2. create changeset which in case of primary key, generates its value
  3. try to insert the new record with changeset mutator

Expected behaviour:
For non-serial primary keys and insertion operation, primary filed should not be ignored and applied as a mutation.

For me just removing this check worked, however, I think that a more sophisticated logic should be applied (the value should not be mutated during update and serial primary key should not applied same way).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.