jeudi 27 avril 2023

Deep copy in golang

Trying to optimize the performance replacing the calls with json serde included with direct calls. But got issues with type conversion - although types are identical and conversion via json is possible, direct conversion is not. Conversion using json is not ok for performance reasons: we've got huge sctructures from https://www.tmforum.org/ API's Is there any package with deep copy (deep convert) functionality and optimal performance?

Tried jsoniter and conversion using json - performance is not ok and can be obviously optimized.

Need a DeepConvert func which can pass the following test:

func DeepCopy(from any, targetType reflect.Type) (any, error) {

}

type SubStructFrom struct { IntField int StrField string IntArrayField []int }

type DeepStructFrom struct { IntField int StrField string SubStructField SubStructFrom SubStructArray []SubStructFrom }

type SubStructTo struct { IntField int StrField string IntArrayField []int }

type DeepStructTo struct { IntField int StrField string SubStructField SubStructTo SubStructArray []SubStructTo }

func DeepCopy_test(t *testing.T) { from := DeepStructFrom{ IntField: 3, StrField: "", SubStructField: SubStructFrom{ IntField: 7, StrField: "Str7", IntArrayField: []int{7, 6, 5}, }, SubStructArray: []SubStructFrom{SubStructFrom{ IntField: 1, StrField: "Str1", IntArrayField: []int{0, 1, 2}, }, SubStructFrom{ IntField: 2, StrField: "Str2", IntArrayField: []int{1, 2, 3}, }}, } to, _ := DeepCopy(from, reflect.TypeOf(DeepStructTo{})) require.Equal(t, 3, to.(DeepStructTo).IntField) }





Aucun commentaire:

Enregistrer un commentaire