MCPcopy Create free account
hub / github.com/apache/fory / TestStructWithNestedSlice

Function TestStructWithNestedSlice

go/fory/fory_test.go:756–784  ·  view source on GitHub ↗

Test Fory's serialization and deserialization of a struct with a slice of nested structs.

(t *testing.T)

Source from the content-addressed store, hash-verified

754
755// Test Fory's serialization and deserialization of a struct with a slice of nested structs.
756func TestStructWithNestedSlice(t *testing.T) {
757 type Item struct {
758 Name string
759 }
760
761 type Example struct {
762 Items []Item
763 }
764
765 fory := NewFory(WithXlang(true), WithCompatible(false), WithRefTracking(true))
766 if err := fory.RegisterStructByName(Example{}, "Example"); err != nil {
767 panic(err)
768 }
769 if err := fory.RegisterStructByName(Item{}, "Item"); err != nil {
770 panic(err)
771 }
772
773 example := &Example{Items: []Item{
774 {Name: "test"},
775 }}
776 bytes, _ := fory.Marshal(example)
777 var deserialized2 any
778 if err := fory.Unmarshal(bytes, &deserialized2); err != nil {
779 panic(err)
780 }
781 // When unmarshaling to any, named structs are returned as pointers
782 // for circular reference support
783 require.Equal(t, deserialized2, example)
784}
785
786func convertRecursively(newVal, tmplVal reflect.Value) (reflect.Value, error) {
787 // Unwrap any any

Callers

nothing calls this directly

Calls 8

RegisterStructByNameMethod · 0.95
MarshalMethod · 0.95
UnmarshalMethod · 0.95
NewForyFunction · 0.85
WithXlangFunction · 0.85
WithCompatibleFunction · 0.85
WithRefTrackingFunction · 0.85
EqualMethod · 0.45

Tested by

no test coverage detected