writeMapChunksCode generates the map chunk writing code with specified indentation
(buf *bytes.Buffer, keyType, valueType types.Type, fieldAccess string, keyIsInterface, valueIsInterface bool, indent string)
| 623 | |
| 624 | // writeMapChunksCode generates the map chunk writing code with specified indentation |
| 625 | func writeMapChunksCode(buf *bytes.Buffer, keyType, valueType types.Type, fieldAccess string, keyIsInterface, valueIsInterface bool, indent string) error { |
| 626 | // WriteData chunks for non-empty map |
| 627 | fmt.Fprintf(buf, "%sif mapLen > 0 {\n", indent) |
| 628 | |
| 629 | // Calculate KV header based on types |
| 630 | fmt.Fprintf(buf, "%s\t// Calculate KV header flags\n", indent) |
| 631 | fmt.Fprintf(buf, "%s\tkvHeader := uint8(0)\n", indent) |
| 632 | |
| 633 | // Check if ref tracking is enabled |
| 634 | fmt.Fprintf(buf, "%s\tisRefTracking := ctx.TrackRef()\n", indent) |
| 635 | fmt.Fprintf(buf, "%s\t_ = isRefTracking // Mark as used to avoid warning\n", indent) |
| 636 | |
| 637 | // Set header flags based on type properties |
| 638 | if !keyIsInterface { |
| 639 | // For concrete key types, check if they're referencable |
| 640 | if isReferencableType(keyType) { |
| 641 | fmt.Fprintf(buf, "%s\tif isRefTracking {\n", indent) |
| 642 | fmt.Fprintf(buf, "%s\t\tkvHeader |= 0x1 // track key ref\n", indent) |
| 643 | fmt.Fprintf(buf, "%s\t}\n", indent) |
| 644 | } |
| 645 | } else { |
| 646 | // For any keys, always set not declared type flag |
| 647 | fmt.Fprintf(buf, "%s\tkvHeader |= 0x4 // key type not declared\n", indent) |
| 648 | } |
| 649 | |
| 650 | if !valueIsInterface { |
| 651 | // For concrete value types, check if they're referencable |
| 652 | if isReferencableType(valueType) { |
| 653 | fmt.Fprintf(buf, "%s\tif isRefTracking {\n", indent) |
| 654 | fmt.Fprintf(buf, "%s\t\tkvHeader |= 0x8 // track value ref\n", indent) |
| 655 | fmt.Fprintf(buf, "%s\t}\n", indent) |
| 656 | } |
| 657 | } else { |
| 658 | // For any values, always set not declared type flag |
| 659 | fmt.Fprintf(buf, "%s\tkvHeader |= 0x20 // value type not declared\n", indent) |
| 660 | } |
| 661 | |
| 662 | // WriteData map elements in chunks |
| 663 | fmt.Fprintf(buf, "%s\tchunkSize := 0\n", indent) |
| 664 | fmt.Fprintf(buf, "%s\t_ = buf.WriterIndex() // chunkHeaderOffset\n", indent) |
| 665 | fmt.Fprintf(buf, "%s\tbuf.WriteInt8(int8(kvHeader)) // KV header\n", indent) |
| 666 | fmt.Fprintf(buf, "%s\tchunkSizeOffset := buf.WriterIndex()\n", indent) |
| 667 | fmt.Fprintf(buf, "%s\tbuf.WriteInt8(0) // placeholder for chunk size\n", indent) |
| 668 | |
| 669 | fmt.Fprintf(buf, "%s\tfor mapKey, mapValue := range %s {\n", indent, fieldAccess) |
| 670 | |
| 671 | // WriteData key |
| 672 | if keyIsInterface { |
| 673 | fmt.Fprintf(buf, "%s\t\tctx.WriteValue(reflect.ValueOf(mapKey), fory.RefModeTracking, true)\n", indent) |
| 674 | } else { |
| 675 | if err := generateMapKeyWriteIndented(buf, keyType, "mapKey", indent+"\t\t"); err != nil { |
| 676 | return err |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | // WriteData value |
| 681 | if valueIsInterface { |
| 682 | fmt.Fprintf(buf, "%s\t\tctx.WriteValue(reflect.ValueOf(mapValue), fory.RefModeTracking, true)\n", indent) |
no test coverage detected