1 Commits
0.0.1 ... 0.0.2

Author SHA1 Message Date
George Suntres
65eec3c3b1 Add StructToStruct 2026-03-29 13:12:53 -04:00
4 changed files with 31 additions and 1 deletions

1
go.mod
View File

@@ -3,6 +3,7 @@ module git.gsuntres.com/general/commons
go 1.25.0
require (
github.com/go-viper/mapstructure/v2 v2.5.0
github.com/ianlancetaylor/jsonschema v0.0.0-20251021232724-46ecbf32a0a5
go.mongodb.org/mongo-driver/v2 v2.5.0
)

2
go.sum
View File

@@ -1,5 +1,7 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-viper/mapstructure/v2 v2.5.0 h1:vM5IJoUAy3d7zRSVtIwQgBj7BiWtMPfmPEgAXnvj1Ro=
github.com/go-viper/mapstructure/v2 v2.5.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/ianlancetaylor/jsonschema v0.0.0-20251021232724-46ecbf32a0a5 h1:x2QxKV4w/sBEwwUUmBH/8cFjeOBZqwaB5dz5rcuFspU=

View File

@@ -5,9 +5,14 @@ import (
"reflect"
"strings"
"github.com/go-viper/mapstructure/v2"
"go.mongodb.org/mongo-driver/v2/bson"
)
func StructToStruct(source any, target any) {
mapstructure.Decode(source, &target)
}
// StructToMapRecursive given a struct or a primitive will return the equivalent
// map[string]any of the struct or the primitive as is.
func StructToMapRecursive(obj any) any {

View File

@@ -4,6 +4,28 @@ import (
"testing"
)
func TestStructToStruct(t *testing.T) {
type O struct {
Name string
Age int
Active bool
}
o := &O{
Name: "Nick",
Age: 15,
Active: true,
}
var oo O
StructToStruct(o, &oo)
if oo.Name != "Nick" || oo.Age != 15 || !oo.Active {
t.Fatalf("failed to copy %v", oo)
}
}
func TestStructToMapRecursive(t *testing.T) {
type O struct {
Name string