Skip to content
Snippets Groups Projects
Verified Commit c87cae04 authored by Volker Schukai's avatar Volker Schukai :alien:
Browse files

feat new merge method

parent 87d00532
No related branches found
No related tags found
No related merge requests found
...@@ -10,7 +10,12 @@ import ( ...@@ -10,7 +10,12 @@ import (
// // Copy implements the xflags.Copyable interface. // // Copy implements the xflags.Copyable interface.
func (s *Settings[C]) Copy(m map[string]any) { func (s *Settings[C]) Copy(m map[string]any) {
c := s.Config() if s == nil {
panic("struct is not initialized")
}
s.Lock()
defer s.Unlock()
errorCount := len(s.errors) errorCount := len(s.errors)
defer func() { defer func() {
...@@ -19,6 +24,12 @@ func (s *Settings[C]) Copy(m map[string]any) { ...@@ -19,6 +24,12 @@ func (s *Settings[C]) Copy(m map[string]any) {
} }
}() }()
copyOf := func(s C) C {
return s
}
c := copyOf(s.config)
for k, v := range m { for k, v := range m {
err := pathfinder.SetValue(&c, k, v) err := pathfinder.SetValue(&c, k, v)
if err != nil { if err != nil {
...@@ -26,5 +37,6 @@ func (s *Settings[C]) Copy(m map[string]any) { ...@@ -26,5 +37,6 @@ func (s *Settings[C]) Copy(m map[string]any) {
} }
} }
s.setConfigInternal(c, true) s.setConfigInternal(c, false)
} }
...@@ -3,22 +3,31 @@ ...@@ -3,22 +3,31 @@
package configuration package configuration
//type testExecutionStruct struct { import (
//} "github.com/stretchr/testify/assert"
// "testing"
//type testConfigStruct struct { )
//}
// func TestCopyable(t *testing.T) {
//func (s *testConfigStruct) Copy(m map[string]any) { config := struct {
//} Host string
// Port int
//func TestXFlagsCopyable(t *testing.T) { }{
// config := New(testConfigStruct{}) Host: "localhost",
// flags := xflags.Execute(testExecutionStruct{}, config) Port: 8080,
// flags.Parse([]string{"-a", "command1", "-d"}) }
//}
// c := New(config)
//func TestXFlagsCopyable2(t *testing.T) {
// flags := xflags.Execute(testExecutionStruct{}) assert.Equal(t, c.Config().Host, "localhost")
// flags.Parse([]string{"-a", "command1", "-d"}) assert.Equal(t, c.Config().Port, 8080)
//}
m := map[string]any{
"Host": "1.2.3.4",
}
c.Copy(m)
assert.Equal(t, c.Config().Host, "1.2.3.4")
}
...@@ -12,8 +12,8 @@ require ( ...@@ -12,8 +12,8 @@ require (
github.com/stretchr/testify v1.8.0 github.com/stretchr/testify v1.8.0
gitlab.schukai.com/oss/libraries/go/application/xflags v1.9.0 gitlab.schukai.com/oss/libraries/go/application/xflags v1.9.0
gitlab.schukai.com/oss/libraries/go/network/http-negotiation v1.3.0 gitlab.schukai.com/oss/libraries/go/network/http-negotiation v1.3.0
gitlab.schukai.com/oss/libraries/go/utilities/pathfinder v0.3.0 gitlab.schukai.com/oss/libraries/go/utilities/pathfinder v0.3.1
golang.org/x/exp v0.0.0-20221012211006-4de253d81b95 golang.org/x/exp v0.0.0-20221023144134-a1e5550cf13e
gopkg.in/yaml.v3 v3.0.1 gopkg.in/yaml.v3 v3.0.1
) )
...@@ -22,8 +22,8 @@ require ( ...@@ -22,8 +22,8 @@ require (
github.com/golang/protobuf v1.5.2 // indirect github.com/golang/protobuf v1.5.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
golang.org/x/net v0.0.0-20221014081412-f15817d10f9b // indirect golang.org/x/net v0.1.0 // indirect
golang.org/x/sys v0.0.0-20221013171732-95e765b1cc43 // indirect golang.org/x/sys v0.1.0 // indirect
google.golang.org/appengine v1.6.7 // indirect google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.1 // indirect google.golang.org/protobuf v1.28.1 // indirect
) )
...@@ -35,16 +35,24 @@ gitlab.schukai.com/oss/libraries/go/network/http-negotiation v1.3.0 h1:SZG0BW5ll ...@@ -35,16 +35,24 @@ gitlab.schukai.com/oss/libraries/go/network/http-negotiation v1.3.0 h1:SZG0BW5ll
gitlab.schukai.com/oss/libraries/go/network/http-negotiation v1.3.0/go.mod h1:RS2rKf5O+rmSBshHLOgjG7dxg5N2MhNYokZOBcuXdX8= gitlab.schukai.com/oss/libraries/go/network/http-negotiation v1.3.0/go.mod h1:RS2rKf5O+rmSBshHLOgjG7dxg5N2MhNYokZOBcuXdX8=
gitlab.schukai.com/oss/libraries/go/utilities/pathfinder v0.3.0 h1:mSxk2q/npskmHMmw1oF4moccjGav5dL6qmff2njUV7A= gitlab.schukai.com/oss/libraries/go/utilities/pathfinder v0.3.0 h1:mSxk2q/npskmHMmw1oF4moccjGav5dL6qmff2njUV7A=
gitlab.schukai.com/oss/libraries/go/utilities/pathfinder v0.3.0/go.mod h1:UvdD4NAf3gLKYafabJD7e9ZCOetzM9JZ9y4GkZukPVU= gitlab.schukai.com/oss/libraries/go/utilities/pathfinder v0.3.0/go.mod h1:UvdD4NAf3gLKYafabJD7e9ZCOetzM9JZ9y4GkZukPVU=
gitlab.schukai.com/oss/libraries/go/utilities/pathfinder v0.3.1 h1:oyElaqEiyr2XgaE1CYwD8LoeHsuR/vQD/p6k3jYbJFs=
gitlab.schukai.com/oss/libraries/go/utilities/pathfinder v0.3.1/go.mod h1:UvdD4NAf3gLKYafabJD7e9ZCOetzM9JZ9y4GkZukPVU=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/exp v0.0.0-20221012211006-4de253d81b95 h1:sBdrWpxhGDdTAYNqbgBLAR+ULAPPhfgncLr1X0lyWtg= golang.org/x/exp v0.0.0-20221012211006-4de253d81b95 h1:sBdrWpxhGDdTAYNqbgBLAR+ULAPPhfgncLr1X0lyWtg=
golang.org/x/exp v0.0.0-20221012211006-4de253d81b95/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE= golang.org/x/exp v0.0.0-20221012211006-4de253d81b95/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=
golang.org/x/exp v0.0.0-20221023144134-a1e5550cf13e h1:SkwG94eNiiYJhbeDE018Grw09HIN/KB9NlRmZsrzfWs=
golang.org/x/exp v0.0.0-20221023144134-a1e5550cf13e/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.0.0-20221014081412-f15817d10f9b h1:tvrvnPFcdzp294diPnrdZZZ8XUt2Tyj7svb7X52iDuU= golang.org/x/net v0.0.0-20221014081412-f15817d10f9b h1:tvrvnPFcdzp294diPnrdZZZ8XUt2Tyj7svb7X52iDuU=
golang.org/x/net v0.0.0-20221014081412-f15817d10f9b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk= golang.org/x/net v0.0.0-20221014081412-f15817d10f9b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/net v0.1.0 h1:hZ/3BUoy5aId7sCpA/Tc5lt8DkFgdVS2onTpJsZ/fl0=
golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20221013171732-95e765b1cc43 h1:OK7RB6t2WQX54srQQYSXMW8dF5C6/8+oA/s5QBmmto4= golang.org/x/sys v0.0.0-20221013171732-95e765b1cc43 h1:OK7RB6t2WQX54srQQYSXMW8dF5C6/8+oA/s5QBmmto4=
golang.org/x/sys v0.0.0-20221013171732-95e765b1cc43/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20221013171732-95e765b1cc43/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
......
merge.go 0 → 100644
package configuration
import "github.com/imdario/mergo"
func (s *Settings[C]) Merge(change C) *Settings[C] {
if s == nil {
panic("struct is not initialized")
}
s.Lock()
defer s.Unlock()
if err := mergo.Merge(&s.config, change); err != nil {
s.errors = append(s.errors, err)
}
return s
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment