generateWriteMethod generates the Write method that handles ref/type flags
(buf *bytes.Buffer, s *StructInfo)
| 455 | |
| 456 | // generateWriteMethod generates the Write method that handles ref/type flags |
| 457 | func generateWriteMethod(buf *bytes.Buffer, s *StructInfo) error { |
| 458 | fmt.Fprintf(buf, "// Write is the entry point for serialization with ref/type handling\n") |
| 459 | fmt.Fprintf(buf, "func (g *%s_ForyGenSerializer) Write(ctx *fory.WriteContext, refMode fory.RefMode, writeType bool, hasGenerics bool, value reflect.Value) {\n", s.Name) |
| 460 | fmt.Fprintf(buf, "\tg.initHash(ctx.TypeResolver())\n") |
| 461 | fmt.Fprintf(buf, "\t_ = hasGenerics // not used for struct serializers\n") |
| 462 | fmt.Fprintf(buf, "\tswitch refMode {\n") |
| 463 | fmt.Fprintf(buf, "\tcase fory.RefModeTracking:\n") |
| 464 | fmt.Fprintf(buf, "\t\tif !value.IsValid() || (value.Kind() == reflect.Ptr && value.IsNil()) {\n") |
| 465 | fmt.Fprintf(buf, "\t\t\tctx.Buffer().WriteInt8(-3) // NullFlag\n") |
| 466 | fmt.Fprintf(buf, "\t\t\treturn\n") |
| 467 | fmt.Fprintf(buf, "\t\t}\n") |
| 468 | fmt.Fprintf(buf, "\t\trefWritten, err := ctx.RefResolver().WriteRefOrNull(ctx.Buffer(), value)\n") |
| 469 | fmt.Fprintf(buf, "\t\tif err != nil {\n") |
| 470 | fmt.Fprintf(buf, "\t\t\tctx.SetError(fory.FromError(err))\n") |
| 471 | fmt.Fprintf(buf, "\t\t\treturn\n") |
| 472 | fmt.Fprintf(buf, "\t\t}\n") |
| 473 | fmt.Fprintf(buf, "\t\tif refWritten {\n") |
| 474 | fmt.Fprintf(buf, "\t\t\treturn\n") |
| 475 | fmt.Fprintf(buf, "\t\t}\n") |
| 476 | fmt.Fprintf(buf, "\tcase fory.RefModeNullOnly:\n") |
| 477 | fmt.Fprintf(buf, "\t\tif !value.IsValid() || (value.Kind() == reflect.Ptr && value.IsNil()) {\n") |
| 478 | fmt.Fprintf(buf, "\t\t\tctx.Buffer().WriteInt8(-3) // NullFlag\n") |
| 479 | fmt.Fprintf(buf, "\t\t\treturn\n") |
| 480 | fmt.Fprintf(buf, "\t\t}\n") |
| 481 | fmt.Fprintf(buf, "\t\tctx.Buffer().WriteInt8(-1) // NotNullValueFlag\n") |
| 482 | fmt.Fprintf(buf, "\t}\n") |
| 483 | fmt.Fprintf(buf, "\tif writeType {\n") |
| 484 | fmt.Fprintf(buf, "\t\ttypeInfo, err := ctx.TypeResolver().GetTypeInfo(value, true)\n") |
| 485 | fmt.Fprintf(buf, "\t\tif err != nil {\n") |
| 486 | fmt.Fprintf(buf, "\t\t\tctx.SetError(fory.FromError(err))\n") |
| 487 | fmt.Fprintf(buf, "\t\t\treturn\n") |
| 488 | fmt.Fprintf(buf, "\t\t}\n") |
| 489 | fmt.Fprintf(buf, "\t\tctx.TypeResolver().WriteTypeInfo(ctx.Buffer(), typeInfo, ctx.Err())\n") |
| 490 | fmt.Fprintf(buf, "\t}\n") |
| 491 | fmt.Fprintf(buf, "\tg.WriteData(ctx, value)\n") |
| 492 | fmt.Fprintf(buf, "}\n\n") |
| 493 | return nil |
| 494 | } |
| 495 | |
| 496 | // generateReadMethod generates the Read method that handles ref/type flags |
| 497 | func generateReadMethod(buf *bytes.Buffer, s *StructInfo) error { |
no outgoing calls
no test coverage detected