Skip to content

Commit

Permalink
chore(loader): Changing the ignore funcs signature (#55)
Browse files Browse the repository at this point in the history
* Updating the ignore func to take the reflected version of the field

* Updating compiling and tests

* Updating the ignore func to be a type

* Updating examples
  • Loading branch information
Jacobbrewer1 authored Oct 25, 2024
1 parent 45e4b47 commit befc09c
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 17 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci-code-approval.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,6 @@ jobs:
# Do not run this is the PR is created by dependabot
- name: "Check for changes"
run: git diff --exit-code

- name: "Check for new files"
run: git diff --exit-code --name-status
51 changes: 51 additions & 0 deletions examples/basic_ignores/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package main

import (
"encoding/json"
"fmt"

"github.com/jacobbrewer1/patcher"
)

type Person struct {
ID *int `json:"id" db:"id"`
Name *string `json:"name" db:"name"`
IgnoredField *string `json:"ignored_field" patcher:"-"`
}

type PersonWhere struct {
ID *int `db:"id"`
}

func NewPersonWhere(id int) *PersonWhere {
return &PersonWhere{
ID: &id,
}
}

func (p *PersonWhere) Where() (string, []any) {
return "id = ?", []any{*p.ID}
}

func main() {
const jsonStr = `{"id": 1, "name": "john", "ignored_field": "ignored"}`

person := new(Person)
if err := json.Unmarshal([]byte(jsonStr), person); err != nil {
panic(err)
}

condition := NewPersonWhere(*person.ID)

sqlStr, args, err := patcher.GenerateSQL(
person,
patcher.WithTable("people"),
patcher.WithWhere(condition),
)
if err != nil {
panic(err)
}

fmt.Println(sqlStr)
fmt.Println(args)
}
11 changes: 9 additions & 2 deletions examples/loader_with_opts/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"fmt"
"reflect"
"strings"

"github.com/jacobbrewer1/patcher"
)
Expand All @@ -16,6 +18,7 @@ type Something struct {
IgnoredField string
IgnoredFieldTwo string
IgnoredFieldByFunc string
IgnoredByTag string `patcher:"-"`
}

func main() {
Expand All @@ -30,6 +33,7 @@ func main() {
IgnoredField: "Ignored",
IgnoredFieldTwo: "Ignored Two",
IgnoredFieldByFunc: "Ignored By Func",
IgnoredByTag: "Ignored By Tag",
}

n := Something{
Expand All @@ -41,15 +45,16 @@ func main() {
IgnoredField: "Diff Ignored",
IgnoredFieldTwo: "Diff Ignored Two",
IgnoredFieldByFunc: "Diff Ignored By Func",
IgnoredByTag: "Diff Ignored By Tag",
}

// The patcher.LoadDiff function will apply the changes from n to s.
if err := patcher.LoadDiff(&s, &n,
patcher.WithIncludeZeroValues(),
patcher.WithIncludeNilValues(),
patcher.WithIgnoredFields("ignoredField", "IgNoReDfIeLdTwO"),
patcher.WithIgnoredFieldsFunc(func(fieldName string, oldValue, newValue interface{}) bool {
return fieldName == "ignoredfieldbyfunc"
patcher.WithIgnoredFieldsFunc(func(field reflect.StructField, oldValue, newValue interface{}) bool {
return strings.ToLower(field.Name) == "ignoredfieldbyfunc"
}),
); err != nil {
panic(err)
Expand All @@ -65,6 +70,7 @@ func main() {
// Ignored
// Ignored Two
// Ignored By Func
// Ignored By Tag
fmt.Println(s.Number)
fmt.Println(s.Text)
fmt.Println(s.PrePopulated)
Expand All @@ -74,4 +80,5 @@ func main() {
fmt.Println(s.IgnoredField)
fmt.Println(s.IgnoredFieldTwo)
fmt.Println(s.IgnoredFieldByFunc)
fmt.Println(s.IgnoredByTag)
}
14 changes: 8 additions & 6 deletions loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ var (
ErrInvalidType = errors.New("invalid type: must pointer to struct")
)

type IgnoreFieldsFunc func(field reflect.StructField, oldValue, newValue any) bool

type loader struct {
// includeZeroValues determines whether zero values should be included in the patch
includeZeroValues bool
Expand All @@ -25,7 +27,7 @@ type loader struct {
// ignoreFieldsFunc is a function that determines whether a field should be ignored
//
// This func should return true is the field is to be ignored
ignoreFieldsFunc func(fieldName string, oldValue, newValue any) bool
ignoreFieldsFunc IgnoreFieldsFunc
}

func newLoader(opts ...LoaderOption) *loader {
Expand Down Expand Up @@ -124,7 +126,7 @@ func (l *loader) checkSkipField(field reflect.StructField, oldValue, newValue an
return true
}

return l.ignoredFieldsCheck(strings.ToLower(field.Name), oldValue, newValue)
return l.ignoredFieldsCheck(field, oldValue, newValue)
}

func (l *loader) checkSkipTag(field reflect.StructField) bool {
Expand All @@ -137,12 +139,12 @@ func (l *loader) checkSkipTag(field reflect.StructField) bool {
return slices.Contains(tags, TagOptSkip)
}

func (l *loader) ignoredFieldsCheck(field string, oldValue, newValue any) bool {
return l.checkIgnoredFields(field) || l.checkIgnoreFunc(field, oldValue, newValue)
func (l *loader) ignoredFieldsCheck(field reflect.StructField, oldValue, newValue any) bool {
return l.checkIgnoredFields(strings.ToLower(field.Name)) || l.checkIgnoreFunc(field, oldValue, newValue)
}

func (l *loader) checkIgnoreFunc(field string, oldValue, newValue any) bool {
return l.ignoreFieldsFunc != nil && l.ignoreFieldsFunc(strings.ToLower(field), oldValue, newValue)
func (l *loader) checkIgnoreFunc(field reflect.StructField, oldValue, newValue any) bool {
return l.ignoreFieldsFunc != nil && l.ignoreFieldsFunc(field, oldValue, newValue)
}

func (l *loader) checkIgnoredFields(field string) bool {
Expand Down
9 changes: 4 additions & 5 deletions loader_opts.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package patcher

import "strings"
import (
"strings"
)

type LoaderOption func(*loader)

Expand Down Expand Up @@ -43,10 +45,7 @@ func WithIgnoredFields(fields ...string) func(*loader) {
}

// WithIgnoredFieldsFunc sets a function that determines whether a field should be ignored when patching.
//
// Note. The field name is wrapped with `strings.ToLower` before being passed to this function, so please ensure that
// the field name is in lowercase if you are comparing it with this function.
func WithIgnoredFieldsFunc(f func(fieldName string, oldValue, newValue any) bool) func(*loader) {
func WithIgnoredFieldsFunc(f IgnoreFieldsFunc) func(*loader) {
return func(l *loader) {
if f == nil {
return
Expand Down
10 changes: 6 additions & 4 deletions loader_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package patcher

import (
"reflect"
"strings"
"testing"

"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -606,8 +608,8 @@ func (s *loadDiffSuite) TestLoadDiff_Success_IgnoreFields() {

func (s *loadDiffSuite) TestLoadDiff_Success_IgnoreFieldsFunc() {
l := s.l
l.ignoreFieldsFunc = func(fieldName string, oldValue, newValue any) bool {
return fieldName == "name"
l.ignoreFieldsFunc = func(field reflect.StructField, oldValue, newValue any) bool {
return strings.ToLower(field.Name) == "name"
}

type testStruct struct {
Expand All @@ -634,8 +636,8 @@ func (s *loadDiffSuite) TestLoadDiff_Success_IgnoreFieldsFunc() {
func (s *loadDiffSuite) TestLoadDiff_Success_IgnoreFieldsFuncAndIgnoreFields() {
l := s.l
l.ignoreFields = []string{"name"}
l.ignoreFieldsFunc = func(fieldName string, oldValue, newValue any) bool {
return fieldName == "name"
l.ignoreFieldsFunc = func(field reflect.StructField, oldValue, newValue any) bool {
return strings.ToLower(field.Name) == "name"
}

type testStruct struct {
Expand Down

0 comments on commit befc09c

Please sign in to comment.