Releases: webrpc/gen-golang
v0.12.4
Try to fix error on webrpc repo command failed: template: main.go.tmpl:8:32: executing "main" at <eq .Opts.types "false">: error calling eq: incompatible types for comparison
v0.12.3
Simplify Enums from JSONMarshaler to TextMarshaler interface (#29) * Simplify Enums from JSONMarshaler to TextMarshaler interface Compared to JSONMarshaler interface, this removes: 1. an extra bytes.NewBufferString() buffer during the marshaling 2. an extra json.Unmarshal() call during the unmarshaling 3. need for "bytes" import in the server-only code * Regenerate examples
v0.12.2
Implement -types=true|false option (true by default) (#28)
v0.12.1
Fix server imports & indentation (#25)
v0.12.0
Implement faster JSON (un)marshaling via jsoniter pkg (#24)
webrpc-gen -schema=proto.ridl -target=golang -client -json=jsoniter -out=client.gen.go
Fixes webrpc/webrpc#20
v0.11.4
Fix uppercase method input/return arguments in server code too (#23) Follow up for #22. This change matches the TypeScript generator: https://github.com/webrpc/gen-typescript/blob/master/types.go.tmpl#L51-L63 Given the following schema: service ExampleService - GetUser(UserID: uint64) => (USER: User) The golang generator wrongly generated "userID" and "uSER" arguments over JSON.
v0.11.3
Fix uppercase method input/return arguments (#22) This change matches the TypeScript generator: https://github.com/webrpc/gen-typescript/blob/master/types.go.tmpl#L51-L63 Given the following schema: service ExampleService - GetUser(UserID: uint64) => (USER: User) The golang generator wrongly generated "userID" and "uSER" arguments over JSON.
v0.11.2
"go.type.import" meta, which lets you choose an import of the given "go.field.type".
v0.11.1
Implement -importTypesFrom=<flag>
opt flag.
v0.11.0 schema errors (breaking API changes)
- Tested with [email protected] ✅
See full [email protected] release.
[email protected] breaking changes
Note: You can turn on -legacyErrors=true
flag on golang generator (ie. webrpc-gen -target=golang -legacyErrors=true -pkg=proto
) in order to preserve the deprecated functions and sentinel errors (see below). This will allow you to migrate your codebase to the new custom schema errors gradually.
The following werbrpc error functions and sentinel errors are now deprecated or removed:
proto.WrapError() // Deprecated.
proto.Errorf() // Deprecated.
proto.HTTPStatusFromErrorCode()
proto.IsErrorCode()
proto.ErrCanceled // Deprecated.
proto.ErrUnknown // Deprecated.
proto.ErrFail // Deprecated.
proto.ErrInvalidArgument // Deprecated.
proto.ErrDeadlineExceeded // Deprecated.
proto.ErrNotFound // Deprecated.
proto.ErrBadRoute // Deprecated.
proto.ErrAlreadyExists // Deprecated.
proto.ErrPermissionDenied // Deprecated.
proto.ErrUnauthenticated // Deprecated.
proto.ErrResourceExhausted // Deprecated.
proto.ErrFailedPrecondition // Deprecated.
proto.ErrAborted // Deprecated.
proto.ErrOutOfRange // Deprecated.
proto.ErrUnimplemented // Deprecated.
proto.ErrInternal // Deprecated.
proto.ErrUnavailable // Deprecated.
proto.ErrDataLoss // Deprecated.
proto.ErrNone // Deprecated.
The schema errors can now be returned from the RPC endpoints via:
func (s *RPC) RemoveUser(ctx context.Context, userID int64) (bool, error) {
r, _ := ctx.Value(proto.HTTPRequestCtxKey).(*http.Request)
if s.IsRateLimited(r) {
- return false, proto.Errorf(proto.ErrUnavailable, "rate limited")
+ return false, proto.ErrRateLimited // HTTP 429 per RIDL schema
}
_, err := s.DB.RemoveUser(ctx, userID)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
- return false, proto.Errorf(proto.ErrNotFound, "no such user(%v)", userID)
+ return false, proto.ErrUserNotFound
}
- return false, proto.WrapError(proto.ErrInternal, err, "")
+ return false, proto.ErrorWithCause(proto.ErrDatabaseDown, err)
}
return true, nil
}
You can also return any other Go error and webrpc will render generic proto.ErrWebrpcEndpoint
error automatically along with HTTP 400
status code.
return fmt.Errorf("some error")
The RPC client(s) can now assert the schema error type by their unique error code:
if err, ok := rpc.RemoveUser(ctx, userID); err != nil {
if errors.Is(proto.ErrRateLimited) {
// slow down; retry with back-off strategy
}
if errors.Is(proto.ErrUserNotFound) {
// handle
}
// etc.
}