From 9fd2f517d94e4eaced38419ea816e9f042c44c25 Mon Sep 17 00:00:00 2001 From: Admin Else <74665496+admin-else@users.noreply.github.com> Date: Sun, 29 Mar 2026 17:34:41 +0200 Subject: [PATCH] Fix SmartConver for slices --- proto/smart_convert.go | 20 ++++++++++++++++---- proto/smart_convert_test.go | 11 +++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/proto/smart_convert.go b/proto/smart_convert.go index 5ed4956..75b11a0 100644 --- a/proto/smart_convert.go +++ b/proto/smart_convert.go @@ -14,6 +14,9 @@ import ( func SmartConvertibleTo(from, to reflect.Type) bool { switch from.Kind() { case reflect.Struct: + if to.Kind() != reflect.Struct { + return false + } if from.NumField() != to.NumField() { return false } @@ -23,7 +26,10 @@ func SmartConvertibleTo(from, to reflect.Type) bool { } } return true - case reflect.Ptr: + case reflect.Pointer, reflect.Slice: + if from.Kind() != to.Kind() { + return false + } return SmartConvertibleTo(from.Elem(), to.Elem()) default: return from.ConvertibleTo(to) @@ -37,15 +43,21 @@ func SmartConvert(from reflect.Value, to reflect.Type) (ret reflect.Value) { for i := range from.NumField() { ret.Field(i).Set(SmartConvert(from.Field(i), to.Field(i).Type)) } - return ret - case reflect.Ptr: + case reflect.Pointer: if from.IsNil() { return reflect.Zero(to) } - return SmartConvert(from.Elem(), to.Elem()).Addr() + ret = SmartConvert(from.Elem(), to.Elem()).Addr() + case reflect.Slice: + l := from.Len() + ret = reflect.MakeSlice(to, l, l) + for i := range l { + ret.Index(i).Set(SmartConvert(from.Index(i), to.Elem())) + } default: return from.Convert(to) } + return ret } // RegisterUntilLatest registers a handler for all versions between the passed in packet version and the latest supported version. diff --git a/proto/smart_convert_test.go b/proto/smart_convert_test.go index 377ee2b..a4bfd4f 100644 --- a/proto/smart_convert_test.go +++ b/proto/smart_convert_test.go @@ -18,3 +18,14 @@ func TestCanSmartConvert(t *testing.T) { ret := proto.SmartConvert(reflect.ValueOf(packet), reflect.TypeFor[*v1_21_8.PlayToClientPacketPosition]()) t.Logf("%#v", ret) } + +func TestCanSmartConvertTags(t *testing.T) { + packet := &v1_21_9.ConfigurationToClientPacketTags{} + targetType := reflect.TypeFor[*v1_21_8.ConfigurationToClientPacketTags]() + can := proto.SmartConvertibleTo(reflect.TypeOf(packet), targetType) + if !can { + t.Fatal("can't smart convert") + } + ret := proto.SmartConvert(reflect.ValueOf(packet), targetType) + t.Logf("%#v", ret) +}