Skip to content

Conversation

@chaokunyang
Copy link
Collaborator

@chaokunyang chaokunyang commented Dec 20, 2025

Why?

The Go serializer interface methods previously returned error values, requiring error checking at every call site. This pattern was inconsistent with the C++ implementation and led to verbose, repetitive error handling code throughout the codebase.

What does this PR do?

Refactors Go error handling to use a centralized delayed error pattern, similar to the C++ implementation:

Interface Changes

  • Serializer interface methods no longer return error:
    • Write(ctx *WriteContext, refMode RefMode, writeType bool, value reflect.Value)
    • WriteData(ctx *WriteContext, value reflect.Value)
    • Read(ctx *ReadContext, refMode RefMode, readType bool, value reflect.Value)
    • ReadData(ctx *ReadContext, type_ reflect.Type, value reflect.Value)
    • ReadWithTypeInfo(ctx *ReadContext, refMode RefMode, typeInfo *TypeInfo, value reflect.Value)

Error Handling Pattern

  • Errors are set via ctx.SetError(FromError(err)) or ctx.SetError(DeserializationErrorf(...))
  • Early return on error using if ctx.HasError() { return }
  • Final error check at API boundaries via ctx.CheckError() or ctx.TakeError()

Files Modified

  • Serializer implementations: array_primitive.go, map_primitive.go, struct.go, skip.go
  • Context files: reader.go, writer.go
  • Cleanup: enum.go, slice_primitive.go (removed unused fmt imports)
  • Code generator: codegen/generator.go, codegen/encoder.go, codegen/decoder.go
  • Generated test file: tests/structs_fory_gen.go

Benefits

  • Cleaner, more consistent code with less boilerplate
  • Aligns with C++ implementation pattern
  • Errors are accumulated in context and checked at API boundaries
  • Reduces cognitive overhead when writing serializers

Related issues

#2982 #3012

Closes #3026

Does this PR introduce any user-facing change?

  • Does this PR introduce any public API change?
    • Yes, the Serializer interface method signatures have changed (no longer return error)
    • This is an internal API change; the public Fory.Serialize() and Fory.Deserialize() APIs remain unchanged
  • Does this PR introduce any binary protocol compatibility change?
    • No, the wire format is unchanged

Benchmark

No performance regression expected - this is a refactoring of error handling flow, not serialization logic.

@chaokunyang chaokunyang merged commit 48b44e8 into apache:main Dec 20, 2025
56 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Go] Refactor go error by centralized error state and lazy raise

2 participants