v0.0.428
Some checks failed
Build Docker and Deploy / Run goext test-suite (push) Has been cancelled
Some checks failed
Build Docker and Deploy / Run goext test-suite (push) Has been cancelled
This commit is contained in:
parent
758e5a67b5
commit
8446b2da22
@ -1,5 +1,5 @@
|
|||||||
package goext
|
package goext
|
||||||
|
|
||||||
const GoextVersion = "0.0.427"
|
const GoextVersion = "0.0.428"
|
||||||
|
|
||||||
const GoextVersionTimestamp = "2024-04-07T15:10:21+0200"
|
const GoextVersionTimestamp = "2024-04-08T16:32:34+0200"
|
||||||
|
@ -2,6 +2,7 @@ package reflectext
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||||
"reflect"
|
"reflect"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -14,7 +15,16 @@ func ConvertStructToMap(v any, opts ...ConvertStructToMapOpt) any {
|
|||||||
if len(opts) > 0 {
|
if len(opts) > 0 {
|
||||||
opt = opts[0]
|
opt = opts[0]
|
||||||
}
|
}
|
||||||
return reflectToMap(reflect.ValueOf(v), opt)
|
|
||||||
|
res := reflectToMap(reflect.ValueOf(v), opt)
|
||||||
|
|
||||||
|
if v, ok := res.(map[string]any); ok {
|
||||||
|
return v
|
||||||
|
} else if langext.IsNil(res) {
|
||||||
|
return nil
|
||||||
|
} else {
|
||||||
|
panic("not an object")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func reflectToMap(fv reflect.Value, opt ConvertStructToMapOpt) any {
|
func reflectToMap(fv reflect.Value, opt ConvertStructToMapOpt) any {
|
||||||
|
42
reflectext/convertToMap_test.go
Normal file
42
reflectext/convertToMap_test.go
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
package reflectext
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestConvertStructToMap(t *testing.T) {
|
||||||
|
|
||||||
|
type tst struct {
|
||||||
|
FieldA int
|
||||||
|
FieldB string
|
||||||
|
FieldC time.Time
|
||||||
|
FieldD []float64
|
||||||
|
FieldE1 *int
|
||||||
|
FieldE2 **int
|
||||||
|
FieldE3 *int
|
||||||
|
FieldE4 **int
|
||||||
|
FieldE5 *int
|
||||||
|
FieldE6 **int
|
||||||
|
}
|
||||||
|
|
||||||
|
value := tst{
|
||||||
|
FieldA: 123,
|
||||||
|
FieldB: "hello",
|
||||||
|
FieldC: time.Date(2020, 05, 12, 8, 30, 0, 0, time.UTC),
|
||||||
|
FieldD: []float64{1, 2, 3, 4, 5, 6, 7},
|
||||||
|
FieldE1: nil,
|
||||||
|
FieldE2: nil,
|
||||||
|
FieldE3: langext.Ptr(12),
|
||||||
|
FieldE4: langext.DblPtr(12),
|
||||||
|
FieldE5: nil,
|
||||||
|
FieldE6: langext.DblPtrNil[int](),
|
||||||
|
}
|
||||||
|
|
||||||
|
valueOut := ConvertStructToMap(value, ConvertStructToMapOpt{KeepJsonMarshalTypes: true})
|
||||||
|
|
||||||
|
fmt.Printf("%+v\n", valueOut)
|
||||||
|
|
||||||
|
}
|
@ -47,9 +47,3 @@ func TestGetMapField(t *testing.T) {
|
|||||||
tst.AssertEqual(t, fmt.Sprint(GetMapField[PseudoInt](maany2, "Test")), "12 true")
|
tst.AssertEqual(t, fmt.Sprint(GetMapField[PseudoInt](maany2, "Test")), "12 true")
|
||||||
tst.AssertEqual(t, fmt.Sprint(GetMapField[PseudoInt](maany2, "Test2")), "0 false")
|
tst.AssertEqual(t, fmt.Sprint(GetMapField[PseudoInt](maany2, "Test2")), "0 false")
|
||||||
}
|
}
|
||||||
|
|
||||||
func main2() {
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user