MCPcopy Create free account
hub / github.com/apache/fory / generateReadTyped

Function generateReadTyped

go/fory/codegen/decoder.go:29–60  ·  view source on GitHub ↗

generateReadTyped generates the strongly-typed ReadData method

(buf *bytes.Buffer, s *StructInfo)

Source from the content-addressed store, hash-verified

27
28// generateReadTyped generates the strongly-typed ReadData method
29func 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)
63func generateReadInterface(buf *bytes.Buffer, s *StructInfo) error {

Callers 1

generateStructSerializerFunction · 0.85

Calls 1

generateFieldReadTypedFunction · 0.85

Tested by

no test coverage detected