Skip to content

Commit bef380b

Browse files
authored
Merge pull request #55 from monzo/fix-marshalling
Fix boolean pointer marshalling
2 parents f93c635 + 731df82 commit bef380b

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

marshaling.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,14 @@ func Marshal(e *Error) *pe.Error {
1515
}
1616
}
1717

18-
retryable := &pe.BoolValue{}
19-
if e.IsRetryable != nil {
20-
retryable.Value = *e.IsRetryable
21-
}
22-
23-
unexpected := &pe.BoolValue{}
24-
if e.IsUnexpected != nil {
25-
unexpected.Value = *e.IsUnexpected
26-
}
27-
2818
err := &pe.Error{
2919
Code: e.Code,
3020
Message: e.Message,
3121
MessageChain: e.MessageChain,
3222
Stack: stackToProto(e.StackFrames),
3323
Params: e.Params,
34-
Retryable: retryable,
35-
Unexpected: unexpected,
24+
Retryable: boolValue(e.IsRetryable),
25+
Unexpected: boolValue(e.IsUnexpected),
3626
MarshalCount: int32(e.MarshalCount + 1),
3727
}
3828
if err.Code == "" {
@@ -41,6 +31,13 @@ func Marshal(e *Error) *pe.Error {
4131
return err
4232
}
4333

34+
func boolValue(r *bool) *pe.BoolValue {
35+
if r == nil {
36+
return nil
37+
}
38+
return &pe.BoolValue{Value: *r}
39+
}
40+
4441
// Unmarshal a protobuf error into a local error
4542
func Unmarshal(p *pe.Error) *Error {
4643
if p == nil {

marshaling_test.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,14 @@ func TestMarshal(t *testing.T) {
184184
assert.Equal(t, tc.platErr.MarshalCount+1, int(protoError.MarshalCount))
185185

186186
if tc.platErr.IsRetryable == nil {
187-
assert.False(t, protoError.Retryable.Value)
187+
assert.Nil(t, protoError.Retryable)
188188
} else {
189189
assert.Equal(t, *tc.platErr.IsRetryable, protoError.Retryable.Value)
190190
assert.Equal(t, tc.protoErr.Retryable.Value, protoError.Retryable.Value)
191191
}
192192

193193
if tc.platErr.IsUnexpected == nil {
194-
assert.False(t, protoError.Unexpected.Value)
194+
assert.Nil(t, protoError.Unexpected)
195195
} else {
196196
assert.Equal(t, *tc.platErr.IsUnexpected, protoError.Unexpected.Value)
197197
assert.Equal(t, tc.protoErr.Unexpected.Value, protoError.Unexpected.Value)
@@ -200,6 +200,18 @@ func TestMarshal(t *testing.T) {
200200
if tc.platErr.MessageChain != nil {
201201
assert.Equal(t, tc.platErr.MessageChain, protoError.MessageChain)
202202
}
203+
t.Run("unmarshalling again keeps the same values", func(t *testing.T) {
204+
roundTripped := Unmarshal(protoError)
205+
// The `Code` can change (e.g. to "unknown") so we cannot assert on this
206+
assert.Equal(t, tc.platErr.Message, roundTripped.Message)
207+
assert.Equal(t, tc.platErr.MessageChain, roundTripped.MessageChain)
208+
209+
assert.EqualValues(t, tc.platErr.IsRetryable, roundTripped.IsRetryable)
210+
assert.EqualValues(t, tc.platErr.IsUnexpected, roundTripped.IsUnexpected)
211+
212+
// The marshal count is incremented by 1 on every marshal
213+
assert.Equal(t, tc.platErr.MarshalCount+1, roundTripped.MarshalCount)
214+
})
203215
}
204216
}
205217

0 commit comments

Comments
 (0)