Skip to content

Commit

Permalink
feat(loader): LoadDiff quality of life improvements (#27)
Browse files Browse the repository at this point in the history
* Changed LoadDiff type to pointer to reflect its functionality, added important caveat to comment, changed language from object to struct in comment

* Made more precise error message
  • Loading branch information
mdesson authored Oct 9, 2024
1 parent ab1f233 commit 16cf5e6
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 20 deletions.
8 changes: 5 additions & 3 deletions loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import (

var (
// ErrInvalidType is returned when the provided type is not a pointer to a struct
ErrInvalidType = errors.New("invalid type")
ErrInvalidType = errors.New("invalid type: must pointer to struct")
)

// LoadDiff inserts the fields provided in the new object into the old object and returns the result.
// LoadDiff inserts the fields provided in the new struct pointer into the old struct pointer and returns the result.
//
// Note that it only pushes non-zero value updates, meaning you cannot set any field to zero, the empty string, etc.
//
// This can be if you are inserting a patch into an existing object but require a new object to be returned with
// all fields.
func LoadDiff[T any](old T, newT T) error {
func LoadDiff[T any](old *T, newT *T) error {
return loadDiff(old, newT)
}

Expand Down
2 changes: 1 addition & 1 deletion sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (s *SQLPatch) PerformPatch() (sql.Result, error) {
return s.db.Exec(s.GenerateSQL())
}

func NewDiffSQLPatch[T any](old, newT T, opts ...PatchOpt) (*SQLPatch, error) {
func NewDiffSQLPatch[T any](old, newT *T, opts ...PatchOpt) (*SQLPatch, error) {
if !isPointerToStruct(old) || !isPointerToStruct(newT) {
return nil, ErrInvalidType
}
Expand Down
17 changes: 1 addition & 16 deletions sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,22 +405,7 @@ func (s *NewDiffSQLPatchSuite) TestNewDiffSQLPatch_Success_noChange() {
func (s *NewDiffSQLPatchSuite) TestNewDiffSQLPatch_fail_notStruct() {
obj := 1

_, err := NewDiffSQLPatch(obj, obj)
s.Error(err)
}

func (s *NewDiffSQLPatchSuite) TestNewDiffSQLPatch_fail_notPointer() {
type testObj struct {
Id *int `db:"id"`
Name *string `db:"name"`
}

obj := testObj{
Id: ptr(1),
Name: ptr("test"),
}

_, err := NewDiffSQLPatch(obj, obj)
_, err := NewDiffSQLPatch(&obj, &obj)
s.Error(err)
}

Expand Down

0 comments on commit 16cf5e6

Please sign in to comment.