chaokunyang commented on code in PR #2638: URL: https://github.com/apache/fory/pull/2638#discussion_r2366274284
########## go/fory/tests/generator_xlang_test.go: ########## @@ -0,0 +1,452 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package fory + +import ( + "fmt" + "reflect" + "testing" + + forygo "github.com/apache/fory/go/fory" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// TestActualCodegenName - Analyze actual type names used by codegen +func TestActualCodegenName(t *testing.T) { + fmt.Println("=== Analyzing Actual Codegen Type Names ===") + + // From source code analysis: + // RegisterSerializerFactory calculates: + // typeTag := pkgPath + "." + typeName + + validationDemoType := reflect.TypeOf(ValidationDemo{}) + pkgPath := validationDemoType.PkgPath() + typeName := validationDemoType.Name() + expectedTypeTag := pkgPath + "." + typeName + + fmt.Printf("ValidationDemo type analysis:\n") + fmt.Printf(" Package path: %s\n", pkgPath) + fmt.Printf(" Type name: %s\n", typeName) + fmt.Printf(" Actual typeTag: %s\n", expectedTypeTag) + + // Test with correct full type name + fmt.Println("\n=== Testing with Correct Full Type Name ===") + + // Create test data + codegenInstance := &ValidationDemo{ + A: 100, + B: "test_data", + C: 200, + D: 3.14159, + E: true, + } + + type ReflectStruct struct { + A int32 + B string + C int64 + D float64 + E bool + } + + reflectInstance := &ReflectStruct{ + A: 100, + B: "test_data", + C: 200, + D: 3.14159, + E: true, + } + + // Codegen mode (automatically uses full name) + foryForCodegen := forygo.NewFory(true) + + // Reflect mode (register with full name) + foryForReflect := forygo.NewFory(true) + err := foryForReflect.RegisterTagType(expectedTypeTag, ReflectStruct{}) + require.NoError(t, err, "Should be able to register ReflectStruct with full name") + + // Serialization test + codegenData, err := foryForCodegen.Marshal(codegenInstance) + require.NoError(t, err, "Codegen serialization should not fail") + + reflectData, err := foryForReflect.Marshal(reflectInstance) + require.NoError(t, err, "Reflect serialization should not fail") + + fmt.Printf("\nSerialization results:\n") + fmt.Printf(" Codegen data length: %d bytes\n", len(codegenData)) + fmt.Printf(" Reflect data length: %d bytes\n", len(reflectData)) + fmt.Printf(" Data identical: %t\n", reflect.DeepEqual(codegenData, reflectData)) + + if reflect.DeepEqual(codegenData, reflectData) { + fmt.Println("🎉 SUCCESS: Using full package path, both structs successfully map to the same name!") + } else { + fmt.Println("❌ Still different, may have other factors") + fmt.Printf(" Codegen hex: %x\n", codegenData) + fmt.Printf(" Reflect hex: %x\n", reflectData) + } + + // Verify cross serialization + fmt.Println("\n=== Cross Serialization Test ===") + + // Use reflect to deserialize codegen data + var reflectResult *ReflectStruct + err = foryForReflect.Unmarshal(codegenData, &reflectResult) + if err == nil && reflectResult != nil { + fmt.Printf("✅ Successfully used reflect to deserialize codegen data: %+v\n", reflectResult) + } else { + fmt.Printf("❌ Failed to use reflect to deserialize codegen data: %v\n", err) + } + + // Use codegen to deserialize reflect data + var codegenResult *ValidationDemo + err = foryForCodegen.Unmarshal(reflectData, &codegenResult) + if err == nil && codegenResult != nil { + fmt.Printf("✅ Successfully used codegen to deserialize reflect data: %+v\n", codegenResult) + } else { + fmt.Printf("❌ Failed to use codegen to deserialize reflect data: %v\n", err) Review Comment: we need assert here instead of print -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
