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

Method RegisterStructByName

go/fory/fory.go:363–381  ·  view source on GitHub ↗

RegisterStructByName registers a struct type by name for cross-language serialization. type_ can be either a reflect.Type or an instance of the type. name can include a namespace prefix separated by "." (e.g., "example.Foo"). Note: For enum types, use RegisterEnumByName instead. go:noinline

(type_ any, name string)

Source from the content-addressed store, hash-verified

361//
362//go:noinline
363func (f *Fory) RegisterStructByName(type_ any, name string) error {
364 var t reflect.Type
365 if rt, ok := type_.(reflect.Type); ok {
366 t = rt
367 } else {
368 t = reflect.TypeOf(type_)
369 if t.Kind() == reflect.Ptr {
370 t = t.Elem()
371 }
372 }
373 if t.Kind() != reflect.Struct {
374 return fmt.Errorf("RegisterStructByName only supports struct types; for enum types use RegisterEnumByName. Got: %v", t.Kind())
375 }
376 namespace, typeName, err := splitRegisteredName(name)
377 if err != nil {
378 return err
379 }
380 return f.typeResolver.registerStructByName(t, namespace, typeName)
381}
382
383// RegisterEnum registers an enum type with a numeric ID for cross-language serialization.
384// In Go, enums are typically defined as int-based types (e.g., type Color int32).

Calls 3

splitRegisteredNameFunction · 0.85
KindMethod · 0.80
registerStructByNameMethod · 0.80

Tested by 15

TestRegisterByIdFunction · 0.76
TestSerializeStructFunction · 0.76
BenchmarkMarshalFunction · 0.76
BenchmarkUnmarshalFunction · 0.76