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

Method RegisterStruct

go/fory/fory.go:273–298  ·  view source on GitHub ↗

RegisterStruct registers a struct type with a numeric ID for cross-language serialization. This is compatible with Java's fory.register(Class, int) method. type_ can be either a reflect.Type or an instance of the type typeID should be the user type ID in the range 0-0xfffffffe (0xffffffff is reserve

(type_ any, typeID uint32)

Source from the content-addressed store, hash-verified

271//
272//go:noinline
273func (f *Fory) RegisterStruct(type_ any, typeID uint32) error {
274 if err := validateUserTypeID(typeID); err != nil {
275 return err
276 }
277 var t reflect.Type
278 if rt, ok := type_.(reflect.Type); ok {
279 t = rt
280 } else {
281 t = reflect.TypeOf(type_)
282 if t.Kind() == reflect.Ptr {
283 t = t.Elem()
284 }
285 }
286
287 // Only struct types are supported via RegisterStruct
288 // For enums, use RegisterEnum
289 if t.Kind() != reflect.Struct {
290 return fmt.Errorf("RegisterStruct only supports struct types; for enum types use RegisterEnum. Got: %v", t.Kind())
291 }
292
293 // Determine the internal type ID based on config
294 var internalTypeID TypeId
295 internalTypeID = f.typeResolver.structTypeID(t, false)
296
297 return f.typeResolver.RegisterStruct(t, internalTypeID, typeID)
298}
299
300// RegisterUnion registers a union type with a numeric ID for cross-language serialization.
301// type_ can be either a reflect.Type or an instance of the union type.

Calls 3

validateUserTypeIDFunction · 0.85
KindMethod · 0.80
structTypeIDMethod · 0.80