Skip to content

Commit 4073248

Browse files
committed
Move into compare and change into own module
1 parent c7879b6 commit 4073248

File tree

5 files changed

+43
-32
lines changed

5 files changed

+43
-32
lines changed

.github/workflows/go.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ jobs:
2020
go-version: '1.22'
2121

2222
- name: Test
23-
run: go test -v ./...
23+
run: go test -v ./compare ./change

change.go renamed to change/change.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
package comparing_structs_for_changes
1+
package change
22

33
import (
44
"fmt"
5+
"github.com/rschoonheim/go-struct-sync/compare"
56
"reflect"
67
)
78

89
// ApplyChanges applies a list of changes to the original struct and returns a modified copy
9-
func ApplyChanges(original interface{}, changes []Change) (interface{}, error) {
10+
func ApplyChanges(original interface{}, changes []compare.Change) (interface{}, error) {
1011
// Extract and validate original value
1112
originalVal := reflect.ValueOf(original)
1213
isPointer := originalVal.Kind() == reflect.Ptr
@@ -51,10 +52,10 @@ func ApplyChanges(original interface{}, changes []Change) (interface{}, error) {
5152
}
5253

5354
switch change.ChangeType {
54-
case Deleted:
55+
case compare.Deleted:
5556
// Set zero value for deleted fields
5657
field.Set(reflect.Zero(field.Type()))
57-
case Modified, Added:
58+
case compare.Modified, compare.Added:
5859
// Fast path for nil values
5960
if change.NewValue == nil {
6061
if field.Kind() == reflect.Ptr || field.Kind() == reflect.Interface ||

change_test.go renamed to change/change_test.go

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
package comparing_structs_for_changes
1+
package change
22

33
import (
4+
"github.com/rschoonheim/go-struct-sync/compare"
45
"reflect"
56
"testing"
67
)
@@ -24,10 +25,10 @@ func TestApplyChangesModifiesValues(t *testing.T) {
2425
Address: "123 Main St",
2526
}
2627

27-
changes := []Change{
28-
{Field: "Name", ChangeType: Modified, NewValue: "Jane"},
29-
{Field: "Age", ChangeType: Modified, NewValue: 31},
30-
{Field: "Active", ChangeType: Modified, NewValue: false},
28+
changes := []compare.Change{
29+
{Field: "Name", ChangeType: compare.Modified, NewValue: "Jane"},
30+
{Field: "Age", ChangeType: compare.Modified, NewValue: 31},
31+
{Field: "Active", ChangeType: compare.Modified, NewValue: false},
3132
}
3233

3334
result, err := ApplyChanges(original, changes)
@@ -50,8 +51,8 @@ func TestApplyChangesToPointer(t *testing.T) {
5051
Age: 30,
5152
}
5253

53-
changes := []Change{
54-
{Field: "Name", ChangeType: Modified, NewValue: "Jane"},
54+
changes := []compare.Change{
55+
{Field: "Name", ChangeType: compare.Modified, NewValue: "Jane"},
5556
}
5657

5758
result, err := ApplyChanges(original, changes)
@@ -61,7 +62,7 @@ func TestApplyChangesToPointer(t *testing.T) {
6162

6263
modified := result.(*Person)
6364
if modified.Name != "Jane" || modified.Age != 30 {
64-
t.Errorf("Expected only Name to be modified, got: %+v", modified)
65+
t.Errorf("Expected only Name to be compare.Modified, got: %+v", modified)
6566
}
6667
}
6768

@@ -73,10 +74,10 @@ func TestApplyChangesDeletesFields(t *testing.T) {
7374
Manager: &Person{Name: "Boss"},
7475
}
7576

76-
changes := []Change{
77-
{Field: "Name", ChangeType: Deleted, OldValue: "John"},
78-
{Field: "Children", ChangeType: Deleted, OldValue: []string{"Alice", "Bob"}},
79-
{Field: "Manager", ChangeType: Deleted, OldValue: &Person{Name: "Boss"}},
77+
changes := []compare.Change{
78+
{Field: "Name", ChangeType: compare.Deleted, OldValue: "John"},
79+
{Field: "Children", ChangeType: compare.Deleted, OldValue: []string{"Alice", "Bob"}},
80+
{Field: "Manager", ChangeType: compare.Deleted, OldValue: &Person{Name: "Boss"}},
8081
}
8182

8283
result, err := ApplyChanges(original, changes)
@@ -101,10 +102,10 @@ func TestApplyChangesAddsValues(t *testing.T) {
101102
Name: "John",
102103
}
103104

104-
changes := []Change{
105-
{Field: "Age", ChangeType: Added, NewValue: 25},
106-
{Field: "Active", ChangeType: Added, NewValue: true},
107-
{Field: "Children", ChangeType: Added, NewValue: []string{"Child"}},
105+
changes := []compare.Change{
106+
{Field: "Age", ChangeType: compare.Added, NewValue: 25},
107+
{Field: "Active", ChangeType: compare.Added, NewValue: true},
108+
{Field: "Children", ChangeType: compare.Added, NewValue: []string{"Child"}},
108109
}
109110

110111
result, err := ApplyChanges(original, changes)
@@ -123,7 +124,7 @@ func TestApplyChangesAddsValues(t *testing.T) {
123124

124125
func TestApplyChangesWithEmptyChangesList(t *testing.T) {
125126
original := Person{Name: "John", Age: 30}
126-
changes := []Change{}
127+
changes := []compare.Change{}
127128

128129
result, err := ApplyChanges(original, changes)
129130
if err != nil {
@@ -138,8 +139,8 @@ func TestApplyChangesWithEmptyChangesList(t *testing.T) {
138139

139140
func TestApplyChangesFailsOnNonStruct(t *testing.T) {
140141
original := "not a struct"
141-
changes := []Change{
142-
{Field: "something", ChangeType: Modified, NewValue: "new"},
142+
changes := []compare.Change{
143+
{Field: "something", ChangeType: compare.Modified, NewValue: "new"},
143144
}
144145

145146
_, err := ApplyChanges(original, changes)
@@ -150,8 +151,8 @@ func TestApplyChangesFailsOnNonStruct(t *testing.T) {
150151

151152
func TestApplyChangesFailsOnNonExistentField(t *testing.T) {
152153
original := Person{Name: "John"}
153-
changes := []Change{
154-
{Field: "NonExistentField", ChangeType: Modified, NewValue: "value"},
154+
changes := []compare.Change{
155+
{Field: "NonExistentField", ChangeType: compare.Modified, NewValue: "value"},
155156
}
156157

157158
_, err := ApplyChanges(original, changes)
@@ -162,8 +163,8 @@ func TestApplyChangesFailsOnNonExistentField(t *testing.T) {
162163

163164
func TestApplyChangesFailsOnUnexportedField(t *testing.T) {
164165
original := Person{Name: "John"}
165-
changes := []Change{
166-
{Field: "private", ChangeType: Modified, NewValue: "value"},
166+
changes := []compare.Change{
167+
{Field: "private", ChangeType: compare.Modified, NewValue: "value"},
167168
}
168169

169170
_, err := ApplyChanges(original, changes)
@@ -174,8 +175,8 @@ func TestApplyChangesFailsOnUnexportedField(t *testing.T) {
174175

175176
func TestApplyChangesFailsOnTypeConversionError(t *testing.T) {
176177
original := Person{Name: "John"}
177-
changes := []Change{
178-
{Field: "Name", ChangeType: Modified, NewValue: struct{}{}},
178+
changes := []compare.Change{
179+
{Field: "Name", ChangeType: compare.Modified, NewValue: struct{}{}},
179180
}
180181

181182
_, err := ApplyChanges(original, changes)

compare.go renamed to compare/compare.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package comparing_structs_for_changes
1+
package compare
22

33
import (
44
"encoding/json"

compare_test.go renamed to compare/compare_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1-
package comparing_structs_for_changes
1+
package compare
22

33
import (
44
"strings"
55
"testing"
66
)
77

8+
type Person struct {
9+
Name string
10+
Age int
11+
Active bool
12+
Address string
13+
Children []string
14+
Manager *Person
15+
}
16+
817
func TestCompareStructsDetectsModifiedValues(t *testing.T) {
918
old := Person{
1019
Name: "John",

0 commit comments

Comments
 (0)