You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
docs(codec): remove factory from ProtoCodec (#108)
* docs(codec): switch ProtoCodec docs from factory to prototype
Update README and docs to reflect the new `NewProtoCodec[T, U](prototype T)`
constructor signature, replacing references to `ProtoRequestFactory` and factory
functions with a non-nil request prototype example.
This aligns documentation with current API behavior and clarifies that the
prototype is used to allocate fresh request messages for unmarshaling without
reflection.docs(codec): switch ProtoCodec docs from factory to prototype
Update README and docs to reflect the new `NewProtoCodec[T, U](prototype T)`
constructor signature, replacing references to `ProtoRequestFactory` and factory
functions with a non-nil request prototype example.
This aligns documentation with current API behavior and clarifies that the
prototype is used to allocate fresh request messages for unmarshaling without
reflection.
* feat(codec)!: remove prototype arg from NewProtoCodec
Simplify `ProtoCodec` construction by inferring the request message type from
generic `T` instead of requiring a non-nil prototype parameter. This reduces
boilerplate while still allowing fresh request allocation without reflection.
Also update README/docs/examples to reflect the new API and bump GitHub Actions
Go versions to `1.26.1` for tests, linting, benchmarks, and example builds.
BREAKING CHANGE: `codec.NewProtoCodec[T, U](prototype T)` is now
`codec.NewProtoCodec[T, U]()`.feat(codec)!: remove prototype arg from NewProtoCodec
Simplify `ProtoCodec` construction by inferring the request message type from
generic `T` instead of requiring a non-nil prototype parameter. This reduces
boilerplate while still allowing fresh request allocation without reflection.
Also update README/docs/examples to reflect the new API and bump GitHub Actions
Go versions to `1.26.1` for tests, linting, benchmarks, and example builds.
BREAKING CHANGE: `codec.NewProtoCodec[T, U](prototype T)` is now
`codec.NewProtoCodec[T, U]()`.
Handles Protocol Buffers encoding and decoding using Google's `protobuf` libraries (e.g., `google.golang.org/protobuf/proto`).
62
62
63
-
**Important:**Due to the nature of Protocol Buffers, the `ProtoCodec`requires a factory function (`codec.ProtoRequestFactory`) when being constructed. This function must return a new, zero-value instance of the specific *request* proto message type (`T`). This is needed internally to provide a concrete type for unmarshaling without relying on reflection.
63
+
**Important:**`ProtoCodec`infers the concrete requestmessage type from `T`, so it can allocate fresh zero-value messages for unmarshaling without reflection.
64
64
65
65
```go
66
66
import (
67
67
"github.com/Suhaibinator/SRouter/pkg/codec"
68
68
pb "path/to/your/generated/proto/package"// Import your generated proto package
69
69
)
70
70
71
-
// Define the factory function for your request proto message type
72
-
// It must return the specific pointer type (e.g., *pb.MyRequestProto)
73
-
varmyRequestFactory = func() *pb.MyRequestProto {
74
-
return &pb.MyRequestProto{}
75
-
}
76
-
77
-
// Create a new Proto codec, providing the factory function
71
+
// Create a new Proto codec.
78
72
// T is *pb.MyRequestProto, U is *pb.MyResponseProto (or appropriate response type)
-**`codec.NewJSONCodec[T, U]() *codec.JSONCodec[T, U]`**: Constructor for the built-in JSON codec.
196
-
-**`codec.NewProtoCodec[T, U](factory codec.ProtoRequestFactory[T]) *codec.ProtoCodec[T, U]`**: Constructor for the built-in Protocol Buffers codec, requiring a factory function for the request type `T`.
190
+
-**`codec.NewProtoCodec[T, U]() *codec.ProtoCodec[T, U]`**: Constructor for the built-in Protocol Buffers codec.
197
191
198
192
See the `examples/codec` directory for runnable examples using different codecs.
Copy file name to clipboardExpand all lines: docs/examples.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,7 +26,7 @@ Here's a brief overview of the available examples (refer to the source code and
26
26
-**`/examples/trace-logging`**: Demonstrates enabling and using trace IDs for correlating logs within a request lifecycle.
27
27
-**`/examples/cors-error-test`**: Demonstrates handling CORS preflight and error scenarios, including how SRouter writes CORS headers on error responses.
28
28
-**`/examples/source-types`**: Shows how to use different `SourceType` options (Body, Base64QueryParameter, Base64PathParameter, etc.) for generic routes.
29
-
-**`/examples/codec`**: Illustrates using different codecs, particularly `JSONCodec` and `ProtoCodec` (including the required factory function for proto).
29
+
-**`/examples/codec`**: Illustrates using different codecs, particularly `JSONCodec` and `ProtoCodec`.
30
30
-**`/examples/prometheus`**: Example of integrating SRouter's metrics system with Prometheus by providing a Prometheus-based implementation of the `metrics.MetricsRegistry` interface and showing how the application can expose the metrics via an HTTP handler.
31
31
-**`/examples/custom-metrics`**: Demonstrates implementing a custom `metrics.MetricsRegistry` or `metrics.MetricsMiddleware`.
32
32
-**`/examples/handler-error-middleware`**: Shows how middleware can access errors returned by generic handlers to make decisions (e.g., transaction rollback, custom error logging) using `scontext.GetHandlerErrorFromRequest`.
0 commit comments