writeMapReadChunks generates the map chunk reading code with specified indentation
(buf *bytes.Buffer, mapType *types.Map, fieldAccess string, keyType, valueType types.Type, keyIsInterface, valueIsInterface bool, indent string)
| 896 | |
| 897 | // writeMapReadChunks generates the map chunk reading code with specified indentation |
| 898 | func writeMapReadChunks(buf *bytes.Buffer, mapType *types.Map, fieldAccess string, keyType, valueType types.Type, keyIsInterface, valueIsInterface bool, indent string) error { |
| 899 | fmt.Fprintf(buf, "%sif !buf.CheckReadable(mapLen, err) {\n", indent) |
| 900 | fmt.Fprintf(buf, "%s\treturn ctx.TakeError()\n", indent) |
| 901 | fmt.Fprintf(buf, "%s}\n", indent) |
| 902 | fmt.Fprintf(buf, "%s%s = make(%s, mapLen)\n", indent, fieldAccess, mapType.String()) |
| 903 | fmt.Fprintf(buf, "%smapSize := mapLen\n", indent) |
| 904 | |
| 905 | // ReadData chunks |
| 906 | fmt.Fprintf(buf, "%sfor mapSize > 0 {\n", indent) |
| 907 | fmt.Fprintf(buf, "%s\t// ReadData KV header\n", indent) |
| 908 | fmt.Fprintf(buf, "%s\tkvHeader := buf.ReadByte(err)\n", indent) |
| 909 | fmt.Fprintf(buf, "%s\tchunkSize := int(buf.ReadByte(err))\n", indent) |
| 910 | fmt.Fprintf(buf, "%s\tif ctx.HasError() {\n", indent) |
| 911 | fmt.Fprintf(buf, "%s\t\treturn ctx.TakeError()\n", indent) |
| 912 | fmt.Fprintf(buf, "%s\t}\n", indent) |
| 913 | fmt.Fprintf(buf, "%s\tif chunkSize == 0 || chunkSize > mapSize {\n", indent) |
| 914 | fmt.Fprintf(buf, "%s\t\tctx.SetError(fory.DeserializationErrorf(\"invalid map chunk size %%d for remaining length %%d\", chunkSize, mapSize))\n", indent) |
| 915 | fmt.Fprintf(buf, "%s\t\treturn ctx.TakeError()\n", indent) |
| 916 | fmt.Fprintf(buf, "%s\t}\n", indent) |
| 917 | |
| 918 | // Parse header flags |
| 919 | fmt.Fprintf(buf, "%s\ttrackKeyRef := (kvHeader & 0x1) != 0\n", indent) |
| 920 | fmt.Fprintf(buf, "%s\tkeyNotDeclared := (kvHeader & 0x4) != 0\n", indent) |
| 921 | fmt.Fprintf(buf, "%s\ttrackValueRef := (kvHeader & 0x8) != 0\n", indent) |
| 922 | fmt.Fprintf(buf, "%s\tvalueNotDeclared := (kvHeader & 0x20) != 0\n", indent) |
| 923 | fmt.Fprintf(buf, "%s\t_ = trackKeyRef\n", indent) |
| 924 | fmt.Fprintf(buf, "%s\t_ = keyNotDeclared\n", indent) |
| 925 | fmt.Fprintf(buf, "%s\t_ = trackValueRef\n", indent) |
| 926 | fmt.Fprintf(buf, "%s\t_ = valueNotDeclared\n", indent) |
| 927 | |
| 928 | // ReadData key-value pairs in this chunk |
| 929 | fmt.Fprintf(buf, "%s\tfor i := 0; i < chunkSize; i++ {\n", indent) |
| 930 | |
| 931 | // ReadData key |
| 932 | if keyIsInterface { |
| 933 | fmt.Fprintf(buf, "%s\t\tvar mapKey any\n", indent) |
| 934 | fmt.Fprintf(buf, "%s\t\tctx.ReadValue(reflect.ValueOf(&mapKey).Elem(), fory.RefModeTracking, true)\n", indent) |
| 935 | } else { |
| 936 | keyVarType := getGoTypeString(keyType) |
| 937 | fmt.Fprintf(buf, "%s\t\tvar mapKey %s\n", indent, keyVarType) |
| 938 | if err := generateMapKeyReadIndented(buf, keyType, "mapKey", indent+"\t\t"); err != nil { |
| 939 | return err |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | // ReadData value |
| 944 | if valueIsInterface { |
| 945 | fmt.Fprintf(buf, "%s\t\tvar mapValue any\n", indent) |
| 946 | fmt.Fprintf(buf, "%s\t\tctx.ReadValue(reflect.ValueOf(&mapValue).Elem(), fory.RefModeTracking, true)\n", indent) |
| 947 | } else { |
| 948 | valueVarType := getGoTypeString(valueType) |
| 949 | fmt.Fprintf(buf, "%s\t\tvar mapValue %s\n", indent, valueVarType) |
| 950 | if err := generateMapValueReadIndented(buf, valueType, "mapValue", indent+"\t\t"); err != nil { |
| 951 | return err |
| 952 | } |
| 953 | } |
| 954 | |
| 955 | // Set key-value pair in map |
no test coverage detected