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

Function generateOptionValueWrite

go/fory/codegen/encoder.go:262–335  ·  view source on GitHub ↗
(buf *bytes.Buffer, elemType types.Type, valueExpr string)

Source from the content-addressed store, hash-verified

260}
261
262func generateOptionValueWrite(buf *bytes.Buffer, elemType types.Type, valueExpr string) error {
263 // Handle special named types first
264 if named, ok := elemType.(*types.Named); ok {
265 typeStr := named.String()
266 switch typeStr {
267 case "time.Time", "github.com/apache/fory/go/fory.Date":
268 fmt.Fprintf(buf, "\t\tctx.WriteValue(reflect.ValueOf(%s), fory.RefModeNone, true)\n", valueExpr)
269 return nil
270 }
271 if _, ok := named.Underlying().(*types.Struct); ok {
272 fmt.Fprintf(buf, "\t\tctx.WriteValue(reflect.ValueOf(%s), fory.RefModeNone, true)\n", valueExpr)
273 return nil
274 }
275 }
276
277 if basic, ok := elemType.Underlying().(*types.Basic); ok {
278 switch basic.Kind() {
279 case types.Bool:
280 fmt.Fprintf(buf, "\t\tbuf.WriteBool(%s)\n", valueExpr)
281 case types.Int8:
282 fmt.Fprintf(buf, "\t\tbuf.WriteByte_(byte(%s))\n", valueExpr)
283 case types.Int16:
284 fmt.Fprintf(buf, "\t\tbuf.WriteInt16(%s)\n", valueExpr)
285 case types.Int32:
286 fmt.Fprintf(buf, "\t\tbuf.WriteVarint32(%s)\n", valueExpr)
287 case types.Int:
288 fmt.Fprintf(buf, "\t\tbuf.WriteVarint64(int64(%s))\n", valueExpr)
289 case types.Int64:
290 fmt.Fprintf(buf, "\t\tbuf.WriteVarint64(%s)\n", valueExpr)
291 case types.Uint8:
292 fmt.Fprintf(buf, "\t\tbuf.WriteByte_(%s)\n", valueExpr)
293 case types.Uint16:
294 fmt.Fprintf(buf, "\t\tbuf.WriteInt16(int16(%s))\n", valueExpr)
295 case types.Uint32:
296 fmt.Fprintf(buf, "\t\tbuf.WriteInt32(int32(%s))\n", valueExpr)
297 case types.Uint:
298 fmt.Fprintf(buf, "\t\tbuf.WriteInt64(int64(%s))\n", valueExpr)
299 case types.Uint64:
300 fmt.Fprintf(buf, "\t\tbuf.WriteInt64(int64(%s))\n", valueExpr)
301 case types.Float32:
302 fmt.Fprintf(buf, "\t\tbuf.WriteFloat32(%s)\n", valueExpr)
303 case types.Float64:
304 fmt.Fprintf(buf, "\t\tbuf.WriteFloat64(%s)\n", valueExpr)
305 case types.String:
306 fmt.Fprintf(buf, "\t\tctx.WriteString(%s)\n", valueExpr)
307 default:
308 fmt.Fprintf(buf, "\t\t// TODO: unsupported basic type %s\n", basic.String())
309 }
310 return nil
311 }
312
313 if slice, ok := elemType.(*types.Slice); ok {
314 return generateSliceWriteInlineNoNull(buf, slice, valueExpr)
315 }
316 if mapType, ok := elemType.(*types.Map); ok {
317 return generateMapWriteInlineNoNull(buf, mapType, valueExpr)
318 }
319

Callers 1

generateOptionWriteTypedFunction · 0.85

Calls 5

KindMethod · 0.80
StringMethod · 0.45
EmptyMethod · 0.45

Tested by

no test coverage detected