ReadValue reads a polymorphic value with configurable reference tracking and type info reading. Parameters: - refMode: controls reference tracking behavior (RefModeNone, RefModeTracking, RefModeNullOnly) - readType: if true, reads type info from the buffer
(value reflect.Value, refMode RefMode, readType bool)
| 716 | // - refMode: controls reference tracking behavior (RefModeNone, RefModeTracking, RefModeNullOnly) |
| 717 | // - readType: if true, reads type info from the buffer |
| 718 | func (c *ReadContext) ReadValue(value reflect.Value, refMode RefMode, readType bool) { |
| 719 | if !value.IsValid() { |
| 720 | c.SetError(DeserializationError("invalid reflect.Value")) |
| 721 | return |
| 722 | } |
| 723 | |
| 724 | // Handle array targets (arrays are serialized as slices) |
| 725 | if value.Type().Kind() == reflect.Array { |
| 726 | c.ReadArrayValue(value, refMode, readType) |
| 727 | return |
| 728 | } |
| 729 | |
| 730 | // For any types, we need to read the actual type from the buffer first |
| 731 | if value.Type().Kind() == reflect.Interface { |
| 732 | // Handle ref tracking based on refMode |
| 733 | var refID int32 = int32(NotNullValueFlag) |
| 734 | if refMode == RefModeTracking { |
| 735 | var err error |
| 736 | refID, err = c.RefResolver().TryPreserveRefId(c.buffer) |
| 737 | if err != nil { |
| 738 | c.SetError(FromError(err)) |
| 739 | return |
| 740 | } |
| 741 | if refID < int32(NotNullValueFlag) { |
| 742 | // Reference found |
| 743 | obj := c.RefResolver().GetReadObject(refID) |
| 744 | if obj.IsValid() { |
| 745 | value.Set(obj) |
| 746 | } |
| 747 | return |
| 748 | } |
| 749 | } else if refMode == RefModeNullOnly { |
| 750 | flag := c.buffer.ReadInt8(c.Err()) |
| 751 | if flag == NullFlag { |
| 752 | return |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | // Read type info to determine the actual type |
| 757 | if !readType { |
| 758 | c.SetError(DeserializationError("cannot read any without type info")) |
| 759 | return |
| 760 | } |
| 761 | ctxErr := c.Err() |
| 762 | typeInfo := c.typeResolver.ReadTypeInfo(c.buffer, ctxErr) |
| 763 | if ctxErr.HasError() { |
| 764 | return |
| 765 | } |
| 766 | |
| 767 | // Create a new instance of the actual type |
| 768 | actualType := typeInfo.Type |
| 769 | if actualType == nil { |
| 770 | // Unknown type - skip the data using the serializer (skipStructSerializer) |
| 771 | if typeInfo.Serializer != nil { |
| 772 | typeInfo.Serializer.ReadData(c, reflect.Value{}) |
| 773 | } |
| 774 | // Leave interface value as nil for unknown types |
| 775 | return |