generateReadTyped generates the strongly-typed ReadData method
(buf *bytes.Buffer, s *StructInfo)
| 27 | |
| 28 | // generateReadTyped generates the strongly-typed ReadData method |
| 29 | func generateReadTyped(buf *bytes.Buffer, s *StructInfo) error { |
| 30 | fmt.Fprintf(buf, "// ReadTyped provides strongly-typed deserialization with no reflection overhead\n") |
| 31 | fmt.Fprintf(buf, "func (g *%s_ForyGenSerializer) ReadTyped(ctx *fory.ReadContext, v *%s) error {\n", s.Name, s.Name) |
| 32 | fmt.Fprintf(buf, "\tbuf := ctx.Buffer()\n") |
| 33 | fmt.Fprintf(buf, "\terr := ctx.Err() // Get error pointer for deferred error checking\n\n") |
| 34 | |
| 35 | // ReadData and verify struct hash |
| 36 | fmt.Fprintf(buf, "\t// ReadData and verify struct hash\n") |
| 37 | fmt.Fprintf(buf, "\tif got := buf.ReadInt32(err); got != g.structHash {\n") |
| 38 | fmt.Fprintf(buf, "\t\tif ctx.HasError() {\n") |
| 39 | fmt.Fprintf(buf, "\t\t\treturn ctx.TakeError()\n") |
| 40 | fmt.Fprintf(buf, "\t\t}\n") |
| 41 | fmt.Fprintf(buf, "\t\treturn fory.HashMismatchError(got, g.structHash, \"%s\")\n", s.Name) |
| 42 | fmt.Fprintf(buf, "\t}\n\n") |
| 43 | |
| 44 | // ReadData fields in sorted order |
| 45 | fmt.Fprintf(buf, "\t// ReadData fields in same order as write\n") |
| 46 | for _, field := range s.Fields { |
| 47 | if err := generateFieldReadTyped(buf, field); err != nil { |
| 48 | return err |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | // Final error check for any accumulated errors |
| 53 | fmt.Fprintf(buf, "\n\t// Final deferred error check\n") |
| 54 | fmt.Fprintf(buf, "\tif ctx.HasError() {\n") |
| 55 | fmt.Fprintf(buf, "\t\treturn ctx.TakeError()\n") |
| 56 | fmt.Fprintf(buf, "\t}\n") |
| 57 | fmt.Fprintf(buf, "\treturn nil\n") |
| 58 | fmt.Fprintf(buf, "}\n\n") |
| 59 | return nil |
| 60 | } |
| 61 | |
| 62 | // generateReadInterface generates interface compatibility method (ReadData) |
| 63 | func generateReadInterface(buf *bytes.Buffer, s *StructInfo) error { |
no test coverage detected