From 2de6e8b30e92c97d284f545dac7eed25f68f44fb Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 27 Jan 2025 12:13:03 +0000 Subject: [PATCH] chore(deps): Bump github.com/jacobbrewer1/patcher from 0.1.14 to 0.1.15 (#66) Bumps [github.com/jacobbrewer1/patcher](https://github.com/jacobbrewer1/patcher) from 0.1.14 to 0.1.15. - [Release notes](https://github.com/jacobbrewer1/patcher/releases) - [Commits](https://github.com/jacobbrewer1/patcher/compare/v0.1.14...v0.1.15) --- updated-dependencies: - dependency-name: github.com/jacobbrewer1/patcher dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- go.mod | 2 +- go.sum | 4 +- .../github.com/jacobbrewer1/patcher/README.md | 28 ++++++++++++ .../github.com/jacobbrewer1/patcher/joiner.go | 1 + .../github.com/jacobbrewer1/patcher/loader.go | 15 ++++--- .../jacobbrewer1/patcher/mock_Joiner.go | 2 +- .../jacobbrewer1/patcher/mock_WhereTyper.go | 4 +- .../jacobbrewer1/patcher/mock_Wherer.go | 2 +- .../github.com/jacobbrewer1/patcher/patch.go | 7 +++ vendor/github.com/jacobbrewer1/patcher/sql.go | 44 +++++++++++++++---- vendor/modules.txt | 4 +- 11 files changed, 89 insertions(+), 24 deletions(-) diff --git a/go.mod b/go.mod index aad03dc..6adaf70 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,7 @@ require ( github.com/google/subcommands v1.2.0 github.com/hashicorp/vault/api v1.15.0 github.com/huandu/xstrings v1.5.0 - github.com/jacobbrewer1/patcher v0.1.14 + github.com/jacobbrewer1/patcher v0.1.15 github.com/jacobbrewer1/vaulty v0.1.6 github.com/jmoiron/sqlx v1.4.0 github.com/pingcap/tidb/pkg/parser v0.0.0-20241220080229-acba0cd1e2b0 diff --git a/go.sum b/go.sum index 5641063..3eb7520 100644 --- a/go.sum +++ b/go.sum @@ -94,8 +94,8 @@ github.com/huandu/xstrings v1.5.0 h1:2ag3IFq9ZDANvthTwTiqSSZLjDc+BedvHPAp5tJy2TI github.com/huandu/xstrings v1.5.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE= github.com/imdario/mergo v0.3.11 h1:3tnifQM4i+fbajXKBHXWEH+KvNHqojZ778UH75j3bGA= github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= -github.com/jacobbrewer1/patcher v0.1.14 h1:+E043ZXH4pyT+nMM0R8eKhA3/fCvmVvrnbxpWnEDoXE= -github.com/jacobbrewer1/patcher v0.1.14/go.mod h1:RTWS5kY5uTo0VBv7PcFamRy6UGiNvM6f4zGvIShBw0k= +github.com/jacobbrewer1/patcher v0.1.15 h1:DK0xrBKlPPSNt+mXUY1AuZ7lydSin8EcHkPMt/68aIE= +github.com/jacobbrewer1/patcher v0.1.15/go.mod h1:zEgOixvOt2wO2WmoUreN5L5qJpCOdP2eWiYd0XZULn8= github.com/jacobbrewer1/vaulty v0.1.6 h1:h+STKGtDwVugWmeVu3ixlWiuU/DQYmJj0x5ADq1jm5Y= github.com/jacobbrewer1/vaulty v0.1.6/go.mod h1:kKQcDjAdRZLTlaOv6HbhqDXObDjIBgWC7FBC9rdDfbA= github.com/jmoiron/sqlx v1.4.0 h1:1PLqN7S1UYp5t4SrVVnt4nUVNemrDAtxlulVe+Qgm3o= diff --git a/vendor/github.com/jacobbrewer1/patcher/README.md b/vendor/github.com/jacobbrewer1/patcher/README.md index a12d3f2..2686de3 100644 --- a/vendor/github.com/jacobbrewer1/patcher/README.md +++ b/vendor/github.com/jacobbrewer1/patcher/README.md @@ -1,5 +1,9 @@ # Patcher +[![Go Reference](https://pkg.go.dev/badge/github.com/Jacobbrewer1/patcher.svg)](https://pkg.go.dev/github.com/Jacobbrewer1/patcher) +[![Go Report Card](https://goreportcard.com/badge/github.com/jacobbrewer1/patcher)](https://goreportcard.com/report/github.com/jacobbrewer1/patcher) + + Patcher is a GO library that provides a simple way to generate and SQL patches from structs. The library was built out of the need to generate patches for a database; when a new field is added to a struct, this would result in a bunch of new `if` checks to be created in the codebase. This library aims to solve that problem by generating the SQL patches for @@ -18,6 +22,30 @@ you. simplifying the process of managing related data across multiple tables. +## Why Use Patcher? + +* **Saves Time**: It saves time by automatically generating SQL queries from structs, reducing the need to write and + maintain SQL statements manually. +* **Reduces Errors**: It reduces the risk of errors by automatically generating SQL queries based on struct fields, + eliminating the need to manually update queries when struct fields change. +* **Simplifies Code**: It simplifies the codebase by reducing the amount of boilerplate code and if-else conditions + required to handle different struct fields, making the code easier to read and maintain. +* **Streamlines Data Synchronization**: It streamlines the process of synchronizing data changes by allowing you to + inject changes from one struct to another and generate update scripts based on differences. +* **Supports Joins**: It supports generating SQL joins by creating structs that implement the Joiner interface, making it + easier to manage related data across multiple tables. +* **Flexible Configuration**: It provides flexible configuration options to customize the SQL generation process, such as + including zero or nil values in the diff. +* **Easy Integration**: It is easy to integrate into existing projects and can be used with any Go project that needs to + generate SQL queries from structs. +* **Open Source**: It is open-source and available under the Apache 2.0 license. +* **Actively Maintained**: It is actively maintained and updated to support the latest Go versions and best practices, + ensuring compatibility and reliability. +* **Comprehensive Documentation**: It has comprehensive documentation and examples to help you get started quickly and + understand how to use the library effectively. +* **Tested and Reliable**: It is thoroughly tested and reliable, ensuring that it works as expected and meets the + requirements of your project. + ## Usage ### Configuration diff --git a/vendor/github.com/jacobbrewer1/patcher/joiner.go b/vendor/github.com/jacobbrewer1/patcher/joiner.go index c4fa5d5..16d8e99 100644 --- a/vendor/github.com/jacobbrewer1/patcher/joiner.go +++ b/vendor/github.com/jacobbrewer1/patcher/joiner.go @@ -1,5 +1,6 @@ package patcher +// Joiner is an interface that can be used to specify the JOIN clause to use when the SQL is being generated. type Joiner interface { Join() (string, []any) } diff --git a/vendor/github.com/jacobbrewer1/patcher/loader.go b/vendor/github.com/jacobbrewer1/patcher/loader.go index 55260c8..3239448 100644 --- a/vendor/github.com/jacobbrewer1/patcher/loader.go +++ b/vendor/github.com/jacobbrewer1/patcher/loader.go @@ -12,19 +12,22 @@ var ( ErrInvalidType = errors.New("invalid type: must pointer to struct") ) -// LoadDiff inserts the fields provided in the new struct pointer into the old struct pointer and injects the new -// values into the old struct +// LoadDiff inserts the fields from the new struct pointer into the old struct pointer, updating the old struct. // -// Note that it only pushes non-zero value updates, meaning you cannot set any field to zero, the empty string, etc. -// This is configurable by setting the includeZeroValues option to true or for nil values by setting includeNilValues. +// Note that it only updates non-zero value fields, meaning you cannot set any field to zero, the empty string, etc. +// This behavior is configurable by setting the includeZeroValues option to true or for nil values by setting includeNilValues. // Please see the LoaderOption's for more configuration options. // -// This can be if you are inserting a patch into an existing object but require a new object to be returned with -// all fields +// This function is useful if you are inserting a patch into an existing object but require a new object to be returned with +// all fields updated. func LoadDiff[T any](old *T, newT *T, opts ...PatchOpt) error { return newPatchDefaults(opts...).loadDiff(old, newT) } +// loadDiff inserts the fields provided in the new struct pointer into the old struct pointer and injects the new +// values into the old struct. It only pushes non-zero value updates, meaning you cannot set any field to zero, +// the empty string, etc. This is configurable by setting the includeZeroValues option to true or for nil values +// by setting includeNilValues. It handles embedded structs and recursively calls loadDiff for nested structs. func (s *SQLPatch) loadDiff(old, newT any) error { if !isPointerToStruct(old) || !isPointerToStruct(newT) { return ErrInvalidType diff --git a/vendor/github.com/jacobbrewer1/patcher/mock_Joiner.go b/vendor/github.com/jacobbrewer1/patcher/mock_Joiner.go index 28c6b96..ba60dd4 100644 --- a/vendor/github.com/jacobbrewer1/patcher/mock_Joiner.go +++ b/vendor/github.com/jacobbrewer1/patcher/mock_Joiner.go @@ -9,7 +9,7 @@ type MockJoiner struct { mock.Mock } -// Join provides a mock function with given fields: +// Join provides a mock function with no fields func (_m *MockJoiner) Join() (string, []interface{}) { ret := _m.Called() diff --git a/vendor/github.com/jacobbrewer1/patcher/mock_WhereTyper.go b/vendor/github.com/jacobbrewer1/patcher/mock_WhereTyper.go index 9fb7052..4cfdaf0 100644 --- a/vendor/github.com/jacobbrewer1/patcher/mock_WhereTyper.go +++ b/vendor/github.com/jacobbrewer1/patcher/mock_WhereTyper.go @@ -9,7 +9,7 @@ type MockWhereTyper struct { mock.Mock } -// Where provides a mock function with given fields: +// Where provides a mock function with no fields func (_m *MockWhereTyper) Where() (string, []interface{}) { ret := _m.Called() @@ -39,7 +39,7 @@ func (_m *MockWhereTyper) Where() (string, []interface{}) { return r0, r1 } -// WhereType provides a mock function with given fields: +// WhereType provides a mock function with no fields func (_m *MockWhereTyper) WhereType() WhereType { ret := _m.Called() diff --git a/vendor/github.com/jacobbrewer1/patcher/mock_Wherer.go b/vendor/github.com/jacobbrewer1/patcher/mock_Wherer.go index 597f481..398aa91 100644 --- a/vendor/github.com/jacobbrewer1/patcher/mock_Wherer.go +++ b/vendor/github.com/jacobbrewer1/patcher/mock_Wherer.go @@ -9,7 +9,7 @@ type MockWherer struct { mock.Mock } -// Where provides a mock function with given fields: +// Where provides a mock function with no fields func (_m *MockWherer) Where() (string, []interface{}) { ret := _m.Called() diff --git a/vendor/github.com/jacobbrewer1/patcher/patch.go b/vendor/github.com/jacobbrewer1/patcher/patch.go index 5d6a87a..82a6599 100644 --- a/vendor/github.com/jacobbrewer1/patcher/patch.go +++ b/vendor/github.com/jacobbrewer1/patcher/patch.go @@ -96,6 +96,7 @@ func newPatchDefaults(opts ...PatchOpt) *SQLPatch { return p } +// Fields returns the fields to update in the SQL statement func (s *SQLPatch) Fields() []string { if len(s.fields) == 0 { // Default behaviour is to return nil if there are no fields @@ -104,6 +105,7 @@ func (s *SQLPatch) Fields() []string { return s.fields } +// Args returns the arguments to use in the SQL statement func (s *SQLPatch) Args() []any { if len(s.args) == 0 { // Default behaviour is to return nil if there are no args @@ -112,6 +114,7 @@ func (s *SQLPatch) Args() []any { return s.args } +// validatePerformPatch validates the SQLPatch for the PerformPatch method func (s *SQLPatch) validatePerformPatch() error { if s.db == nil { return ErrNoDatabaseConnection @@ -128,6 +131,7 @@ func (s *SQLPatch) validatePerformPatch() error { return nil } +// validateSQLGen validates the SQLPatch for the SQLGen method func (s *SQLPatch) validateSQLGen() error { if s.table == "" { return ErrNoTable @@ -142,6 +146,7 @@ func (s *SQLPatch) validateSQLGen() error { return nil } +// shouldIncludeNil determines whether the field should be included in the patch func (s *SQLPatch) shouldIncludeNil(tag string) bool { if s.includeNilValues { return true @@ -150,6 +155,7 @@ func (s *SQLPatch) shouldIncludeNil(tag string) bool { return s.shouldOmitEmpty(tag) } +// shouldIncludeZero determines whether zero values should be included in the patch func (s *SQLPatch) shouldIncludeZero(tag string) bool { if s.includeZeroValues { return true @@ -158,6 +164,7 @@ func (s *SQLPatch) shouldIncludeZero(tag string) bool { return s.shouldOmitEmpty(tag) } +// shouldOmitEmpty determines whether the field should be omitted if it is empty func (s *SQLPatch) shouldOmitEmpty(tag string) bool { if tag != "" { tags := strings.Split(tag, TagOptSeparator) diff --git a/vendor/github.com/jacobbrewer1/patcher/sql.go b/vendor/github.com/jacobbrewer1/patcher/sql.go index ca306be..bdacfa0 100644 --- a/vendor/github.com/jacobbrewer1/patcher/sql.go +++ b/vendor/github.com/jacobbrewer1/patcher/sql.go @@ -19,12 +19,18 @@ var ( ErrNoChanges = errors.New("no changes detected between the old and new objects") ) +// NewSQLPatch creates a new SQLPatch instance with the given resource and options. +// It initializes the SQLPatch with default settings and generates the SQL patch +// for the provided resource by processing its fields and applying the necessary tags and options. func NewSQLPatch(resource any, opts ...PatchOpt) *SQLPatch { sqlPatch := newPatchDefaults(opts...) sqlPatch.patchGen(resource) return sqlPatch } +// patchGen generates the SQL patch for the given resource. +// It processes the fields of the struct, applying the necessary tags and options, +// and prepares the SQL update statement components (fields and arguments). func (s *SQLPatch) patchGen(resource any) { // If the resource is a pointer, we need to dereference it to get the value if reflect.TypeOf(resource).Kind() == reflect.Ptr { @@ -96,16 +102,20 @@ func (s *SQLPatch) patchGen(resource any) { addField() - var val reflect.Value + val := fVal if fVal.Kind() == reflect.Ptr { val = fVal.Elem() - } else { - val = fVal } switch val.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: s.args = append(s.args, val.Int()) + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: + s.args = append(s.args, val.Uint()) + case reflect.Float32, reflect.Float64: + s.args = append(s.args, val.Float()) + case reflect.Complex64, reflect.Complex128: + s.args = append(s.args, val.Complex()) case reflect.String: s.args = append(s.args, val.String()) case reflect.Bool: @@ -114,8 +124,6 @@ func (s *SQLPatch) patchGen(resource any) { boolArg = 1 } s.args = append(s.args, boolArg) - case reflect.Float32, reflect.Float64: - s.args = append(s.args, val.Float()) default: // This is intentionally a panic as this is a programming error and should be fixed by the developer panic(fmt.Sprintf("unsupported type: %s", val.Kind())) @@ -123,11 +131,17 @@ func (s *SQLPatch) patchGen(resource any) { } } +// GenerateSQL generates the SQL update statement and its arguments for the given resource. +// It creates a new SQLPatch instance with the provided options, processes the resource's fields, +// and constructs the SQL update statement along with the necessary arguments. func GenerateSQL(resource any, opts ...PatchOpt) (string, []any, error) { - sqlPatch := NewSQLPatch(resource, opts...) - return sqlPatch.GenerateSQL() + return NewSQLPatch(resource, opts...).GenerateSQL() } +// GenerateSQL constructs the SQL update statement and its arguments. +// It validates the SQL generation process, builds the SQL update statement +// with the table name, join clauses, set clauses, and where clauses, +// and returns the final SQL string along with the arguments. func (s *SQLPatch) GenerateSQL() (string, []any, error) { if err := s.validateSQLGen(); err != nil { return "", nil, fmt.Errorf("validate perform patch: %w", err) @@ -166,11 +180,16 @@ func (s *SQLPatch) GenerateSQL() (string, []any, error) { return sqlBuilder.String(), args, nil } +// PerformPatch executes the SQL update statement for the given resource. +// It creates a new SQLPatch instance with the provided options, generates the SQL update statement, +// and executes it using the database connection. func PerformPatch(resource any, opts ...PatchOpt) (sql.Result, error) { - sqlPatch := NewSQLPatch(resource, opts...) - return sqlPatch.PerformPatch() + return NewSQLPatch(resource, opts...).PerformPatch() } +// PerformDiffPatch executes the SQL update statement for the differences between the old and new resources. +// It creates a new SQLPatch instance by comparing the old and new resources, generates the SQL update statement, +// and executes it using the database connection. func PerformDiffPatch[T any](old, newT *T, opts ...PatchOpt) (sql.Result, error) { sqlPatch, err := NewDiffSQLPatch(old, newT, opts...) if err != nil { @@ -180,6 +199,10 @@ func PerformDiffPatch[T any](old, newT *T, opts ...PatchOpt) (sql.Result, error) return sqlPatch.PerformPatch() } +// PerformPatch executes the SQL update statement for the current SQLPatch instance. +// It validates the SQL generation process, constructs the SQL update statement and its arguments, +// and executes the statement using the database connection. +// It returns the result of the SQL execution or an error if the process fails. func (s *SQLPatch) PerformPatch() (sql.Result, error) { if err := s.validatePerformPatch(); err != nil { return nil, fmt.Errorf("validate perform patch: %w", err) @@ -193,6 +216,9 @@ func (s *SQLPatch) PerformPatch() (sql.Result, error) { return s.db.Exec(sqlStr, args...) } +// NewDiffSQLPatch creates a new SQLPatch instance by comparing the old and new resources. +// It initializes the SQLPatch with default settings, loads the differences between the old and new resources, +// and prepares the SQL update statement components (fields and arguments) for the differences. func NewDiffSQLPatch[T any](old, newT *T, opts ...PatchOpt) (*SQLPatch, error) { if !isPointerToStruct(old) || !isPointerToStruct(newT) { return nil, ErrInvalidType diff --git a/vendor/modules.txt b/vendor/modules.txt index ed93c9c..39cd092 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -108,8 +108,8 @@ github.com/huandu/xstrings # github.com/imdario/mergo v0.3.11 ## explicit; go 1.13 github.com/imdario/mergo -# github.com/jacobbrewer1/patcher v0.1.14 -## explicit; go 1.22 +# github.com/jacobbrewer1/patcher v0.1.15 +## explicit; go 1.23 github.com/jacobbrewer1/patcher github.com/jacobbrewer1/patcher/inserter # github.com/jacobbrewer1/vaulty v0.1.6