Skip to content

Commit

Permalink
Move the indication of destination message type into Codec (#599)
Browse files Browse the repository at this point in the history
This is so that a custom `Codec` can choose what details to reveal to
clients, in cases where emitting the destination message type is
considered to leak too much information.

See #584 for more context.
  • Loading branch information
jhump authored Oct 4, 2023
1 parent 8a0c89f commit 969bcab
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
12 changes: 10 additions & 2 deletions codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,11 @@ func (c *protoBinaryCodec) Unmarshal(data []byte, message any) error {
if !ok {
return errNotProto(message)
}
return proto.Unmarshal(data, protoMessage)
err := proto.Unmarshal(data, protoMessage)
if err != nil {
return fmt.Errorf("unmarshal into %T: %w", message, err)
}
return nil
}

func (c *protoBinaryCodec) MarshalStable(message any) ([]byte, error) {
Expand Down Expand Up @@ -174,7 +178,11 @@ func (c *protoJSONCodec) Unmarshal(binary []byte, message any) error {
// Discard unknown fields so clients and servers aren't forced to always use
// exactly the same version of the schema.
options := protojson.UnmarshalOptions{DiscardUnknown: true}
return options.Unmarshal(binary, protoMessage)
err := options.Unmarshal(binary, protoMessage)
if err != nil {
return fmt.Errorf("unmarshal into %T: %w", message, err)
}
return nil
}

func (c *protoJSONCodec) MarshalStable(message any) ([]byte, error) {
Expand Down
2 changes: 1 addition & 1 deletion envelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func (r *envelopeReader) Unmarshal(message any) *Error {
}

if err := r.codec.Unmarshal(data.Bytes(), message); err != nil {
return errorf(CodeInvalidArgument, "unmarshal into %T: %w", message, err)
return errorf(CodeInvalidArgument, "unmarshal message: %w", err)
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion protocol_connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -1099,7 +1099,7 @@ func (u *connectUnaryUnmarshaler) UnmarshalFunc(message any, unmarshal func([]by
data = decompressed
}
if err := unmarshal(data.Bytes(), message); err != nil {
return errorf(CodeInvalidArgument, "unmarshal into %T: %w", message, err)
return errorf(CodeInvalidArgument, "unmarshal message: %w", err)
}
return nil
}
Expand Down

0 comments on commit 969bcab

Please sign in to comment.