Skip to content

Commit

Permalink
don't panic in NewDataBlob
Browse files Browse the repository at this point in the history
  • Loading branch information
yycptt committed Jan 12, 2023
1 parent e1d4067 commit 414e188
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions common/persistence/data_blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,19 @@ package persistence
import (
commonpb "go.temporal.io/api/common/v1"
enumspb "go.temporal.io/api/enums/v1"

"go.temporal.io/server/common/persistence/serialization"
)

// NewDataBlob returns a new DataBlob
// TODO: return an UnknowEncodingType error with the actual type string when encodingTypeStr is invalid
func NewDataBlob(data []byte, encodingTypeStr string) *commonpb.DataBlob {
if len(data) == 0 {
return nil
}

encodingType, ok := enumspb.EncodingType_value[encodingTypeStr]
if !ok || (enumspb.EncodingType(encodingType) != enumspb.ENCODING_TYPE_PROTO3 &&
enumspb.EncodingType(encodingType) != enumspb.ENCODING_TYPE_JSON) {
panic(serialization.NewUnknownEncodingTypeError(encodingTypeStr, enumspb.ENCODING_TYPE_JSON, enumspb.ENCODING_TYPE_PROTO3))
if !ok {
// encodingTypeStr not valid, an error will be returned on deserialization
encodingType = int32(enumspb.ENCODING_TYPE_UNSPECIFIED)
}

return &commonpb.DataBlob{
Expand Down

0 comments on commit 414e188

Please # to comment.