(buf *bytes.Buffer, field *FieldInfo, fieldAccess string)
| 260 | } |
| 261 | |
| 262 | func generateOptionReadTyped(buf *bytes.Buffer, field *FieldInfo, fieldAccess string) error { |
| 263 | elemType := field.OptionalElem |
| 264 | if elemType == nil { |
| 265 | fmt.Fprintf(buf, "\tctx.ReadValue(reflect.ValueOf(&%s).Elem(), fory.RefModeTracking, true)\n", fieldAccess) |
| 266 | return nil |
| 267 | } |
| 268 | fmt.Fprintf(buf, "\t{\n") |
| 269 | if isReferencableType(elemType) { |
| 270 | fmt.Fprintf(buf, "\t\tvar optValue %s\n", elemType.String()) |
| 271 | fmt.Fprintf(buf, "\t\tif ctx.TrackRef() {\n") |
| 272 | fmt.Fprintf(buf, "\t\t\trefID, refErr := ctx.RefResolver().TryPreserveRefId(buf)\n") |
| 273 | fmt.Fprintf(buf, "\t\t\tif refErr != nil {\n") |
| 274 | fmt.Fprintf(buf, "\t\t\t\treturn refErr\n") |
| 275 | fmt.Fprintf(buf, "\t\t\t}\n") |
| 276 | fmt.Fprintf(buf, "\t\t\tif refID < int32(fory.NotNullValueFlag) {\n") |
| 277 | fmt.Fprintf(buf, "\t\t\t\tif refID == int32(fory.NullFlag) {\n") |
| 278 | fmt.Fprintf(buf, "\t\t\t\t\t%s = optional.None[%s]()\n", fieldAccess, elemType.String()) |
| 279 | fmt.Fprintf(buf, "\t\t\t\t\treturn nil\n") |
| 280 | fmt.Fprintf(buf, "\t\t\t\t}\n") |
| 281 | fmt.Fprintf(buf, "\t\t\t\tobj := ctx.RefResolver().GetReadObject(refID)\n") |
| 282 | fmt.Fprintf(buf, "\t\t\t\tif obj.IsValid() {\n") |
| 283 | fmt.Fprintf(buf, "\t\t\t\t\ttarget := reflect.ValueOf(&optValue).Elem()\n") |
| 284 | fmt.Fprintf(buf, "\t\t\t\t\tif obj.Type().AssignableTo(target.Type()) {\n") |
| 285 | fmt.Fprintf(buf, "\t\t\t\t\t\ttarget.Set(obj)\n") |
| 286 | fmt.Fprintf(buf, "\t\t\t\t\t\t%s = optional.Some(optValue)\n", fieldAccess) |
| 287 | fmt.Fprintf(buf, "\t\t\t\t\t\treturn nil\n") |
| 288 | fmt.Fprintf(buf, "\t\t\t\t\t}\n") |
| 289 | fmt.Fprintf(buf, "\t\t\t\t}\n") |
| 290 | fmt.Fprintf(buf, "\t\t\t\t%s = optional.None[%s]()\n", fieldAccess, elemType.String()) |
| 291 | fmt.Fprintf(buf, "\t\t\t\treturn nil\n") |
| 292 | fmt.Fprintf(buf, "\t\t\t}\n") |
| 293 | if err := generateOptionValueRead(buf, elemType, "optValue"); err != nil { |
| 294 | return err |
| 295 | } |
| 296 | fmt.Fprintf(buf, "\t\t\tif refID >= 0 {\n") |
| 297 | fmt.Fprintf(buf, "\t\t\t\tctx.RefResolver().SetReadObject(refID, reflect.ValueOf(optValue))\n") |
| 298 | fmt.Fprintf(buf, "\t\t\t}\n") |
| 299 | fmt.Fprintf(buf, "\t\t\t%s = optional.Some(optValue)\n", fieldAccess) |
| 300 | fmt.Fprintf(buf, "\t\t\treturn nil\n") |
| 301 | fmt.Fprintf(buf, "\t\t}\n") |
| 302 | } |
| 303 | fmt.Fprintf(buf, "\t\tflag := buf.ReadInt8(err)\n") |
| 304 | fmt.Fprintf(buf, "\t\tif flag == fory.NullFlag {\n") |
| 305 | fmt.Fprintf(buf, "\t\t\t%s = optional.None[%s]()\n", fieldAccess, elemType.String()) |
| 306 | fmt.Fprintf(buf, "\t\t} else {\n") |
| 307 | fmt.Fprintf(buf, "\t\t\tvar optValue %s\n", elemType.String()) |
| 308 | if err := generateOptionValueRead(buf, elemType, "optValue"); err != nil { |
| 309 | return err |
| 310 | } |
| 311 | fmt.Fprintf(buf, "\t\t\t%s = optional.Some(optValue)\n", fieldAccess) |
| 312 | fmt.Fprintf(buf, "\t\t}\n") |
| 313 | fmt.Fprintf(buf, "\t}\n") |
| 314 | return nil |
| 315 | } |
| 316 | |
| 317 | func generateOptionValueRead(buf *bytes.Buffer, elemType types.Type, valueExpr string) error { |
| 318 | // Handle special named types first |
no test coverage detected