Skip to content

Commit

Permalink
feat(loader): Adding loader examples (#11)
Browse files Browse the repository at this point in the history
Adding loader examples
  • Loading branch information
Jacobbrewer1 authored Oct 7, 2024
1 parent 1a94798 commit afe3fac
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 25 deletions.
43 changes: 43 additions & 0 deletions examples/loader/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"fmt"

"github.com/Jacobbrewer1/patcher"
)

type Something struct {
Number int
Text string
PrePopulated string
NewText string
}

func main() {
s := Something{
Number: 5,
Text: "Hello",
PrePopulated: "PrePopulated",
}

n := Something{
Number: 6,
Text: "Old Text",
NewText: "New Text",
}

// The patcher.LoadDiff function will apply the changes from n to s.
if err := patcher.LoadDiff(&s, &n); err != nil {
panic(err)
}

// Output:
// 6
// Old Text
// PrePopulated
// New Text
fmt.Println(s.Number)
fmt.Println(s.Text)
fmt.Println(s.PrePopulated)
fmt.Println(s.NewText)
}
50 changes: 25 additions & 25 deletions loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ func (s *loadDiffSuite) TestLoadDiff_Success() {
}

old := testStruct{
Name: "Jacob",
Name: "John",
Age: 25,
}

n := testStruct{
Name: "Jacob Brewer",
Name: "John Smith",
Age: 26,
}

err := loadDiff(&old, &n)
s.NoError(err)
s.Equal("Jacob Brewer", old.Name)
s.Equal("John Smith", old.Name)
s.Equal(26, old.Age)
}

Expand All @@ -43,18 +43,18 @@ func (s *loadDiffSuite) TestLoadDiff_Success_ZeroValue() {
}

old := testStruct{
Name: "Jacob",
Name: "John",
Age: 25,
}

n := testStruct{
Name: "Jacob Brewer",
Name: "John Smith",
Age: 0,
}

err := loadDiff(&old, &n)
s.NoError(err)
s.Equal("Jacob Brewer", old.Name)
s.Equal("John Smith", old.Name)
s.Equal(25, old.Age)
}

Expand All @@ -65,15 +65,15 @@ func (s *loadDiffSuite) TestLoadDiff_Success_NoNewValue() {
}

old := testStruct{
Name: "Jacob",
Name: "John",
Age: 25,
}

n := testStruct{}

err := loadDiff(&old, &n)
s.NoError(err)
s.Equal("Jacob", old.Name)
s.Equal("John", old.Name)
s.Equal(25, old.Age)
}

Expand All @@ -84,7 +84,7 @@ func (s *loadDiffSuite) TestLoadDiff_Success_OneNewField() {
}

old := testStruct{
Name: "Jacob",
Name: "John",
Age: 25,
}

Expand All @@ -94,7 +94,7 @@ func (s *loadDiffSuite) TestLoadDiff_Success_OneNewField() {

err := loadDiff(&old, &n)
s.NoError(err)
s.Equal("Jacob", old.Name)
s.Equal("John", old.Name)
s.Equal(26, old.Age)
}

Expand All @@ -106,7 +106,7 @@ func (s *loadDiffSuite) TestLoadDiff_Success_EmbeddedStruct() {
}

old := testStruct{
Name: "Jacob",
Name: "John",
Age: 25,
}

Expand All @@ -119,7 +119,7 @@ func (s *loadDiffSuite) TestLoadDiff_Success_EmbeddedStruct() {

err := loadDiff(&old, &n)
s.NoError(err)
s.Equal("Jacob", old.Name)
s.Equal("John", old.Name)
s.Equal(25, old.Age)
s.Equal("Sarah", old.Partner.Name)
s.Equal(24, old.Partner.Age)
Expand All @@ -133,12 +133,12 @@ func (s *loadDiffSuite) TestLoadDiff_Success_EmbeddedStructWithNewValue() {
}

old := testStruct{
Name: "Jacob",
Name: "John",
Age: 25,
}

n := testStruct{
Name: "Jacob Brewer",
Name: "John Smith",
Partner: &testStruct{
Name: "Sarah",
Age: 24,
Expand All @@ -150,7 +150,7 @@ func (s *loadDiffSuite) TestLoadDiff_Success_EmbeddedStructWithNewValue() {

err := loadDiff(&old, &n)
s.NoError(err)
s.Equal("Jacob Brewer", old.Name)
s.Equal("John Smith", old.Name)
s.Equal(25, old.Age)
s.Equal("Sarah Brewer", old.Partner.Name)
s.Equal(25, old.Partner.Age)
Expand All @@ -169,14 +169,14 @@ func (s *loadDiffSuite) TestLoadDiff_Success_EmbeddedInheritedStruct() {

old := testStruct2{
TestStruct: &TestStruct{
Name: "Jacob",
Name: "John",
Age: 25,
},
}

n := testStruct2{
TestStruct: &TestStruct{
Name: "Jacob Brewer",
Name: "John Smith",
Age: 26,
},
Partner: &TestStruct{
Expand All @@ -187,7 +187,7 @@ func (s *loadDiffSuite) TestLoadDiff_Success_EmbeddedInheritedStruct() {

err := loadDiff(&old, &n)
s.NoError(err)
s.Equal("Jacob Brewer", old.Name)
s.Equal("John Smith", old.Name)
s.Equal(26, old.Age)
s.Equal("Sarah", old.Partner.Name)
s.Equal(24, old.Partner.Age)
Expand All @@ -200,12 +200,12 @@ func (s *loadDiffSuite) TestLoadDiff_FailureNotPointer() {
}

old := testStruct{
Name: "Jacob",
Name: "John",
Age: 25,
}

n := testStruct{
Name: "Jacob Brewer",
Name: "John Smith",
Age: 26,
}

Expand All @@ -223,7 +223,7 @@ func (s *loadDiffSuite) TestLoadDiff_Success_NilOldField() {
}

old := testStruct{
Name: "Jacob",
Name: "John",
Age: 25,
Partner: nil,
}
Expand All @@ -237,7 +237,7 @@ func (s *loadDiffSuite) TestLoadDiff_Success_NilOldField() {

err := loadDiff(&old, &n)
s.NoError(err)
s.Equal("Jacob", old.Name)
s.Equal("John", old.Name)
s.Equal(25, old.Age)
s.Equal("Sarah", old.Partner.Name)
s.Equal(24, old.Partner.Age)
Expand Down Expand Up @@ -326,16 +326,16 @@ func (s *loadDiffSuite) TestLoadDiff_Failure_UnsupportedType() {
}

old := testStruct{
Name: "Jacob",
Name: "John",
Updates: make(chan string),
}

n := testStruct{
Name: "Jacob Brewer",
Name: "John Smith",
}

err := loadDiff(&old, &n)
s.NoError(err)
s.Equal("Jacob Brewer", old.Name)
s.Equal("John Smith", old.Name)
s.NotNil(old.Updates) // Channel should not be nil as it started as a non-nil channel
}

0 comments on commit afe3fac

Please sign in to comment.