From 32dd0bb3f9c5c6c444473f6a7ef0f3dd3ed6b201 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Tue, 25 Jan 2022 15:11:32 +0300 Subject: [PATCH] [#369] status: Support WRONG_MAGIC_NUMBER code and detail Define constant for `WrongMagicNumber` local code. Define constant `DetailIDCorrect` for correct magic detail. Add `ResetDetails` and `AppendDetails` method pair. Replace `SetDetails` method with new `SetStatusDetails` function which can be implemented using new methods. Signed-off-by: Leonard Lyubich --- status/details.go | 8 ++++++++ status/status.go | 2 ++ status/test/generate.go | 2 +- status/types.go | 19 ++++++++++++++++--- 4 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 status/details.go diff --git a/status/details.go b/status/details.go new file mode 100644 index 00000000..5b8f4603 --- /dev/null +++ b/status/details.go @@ -0,0 +1,8 @@ +package status + +// details for WrongMagicNumber code. +const ( + // DetailIDCorrectMagic is an identifier of details with correct network magic + // which can be attached to WrongMagicNumber code. + DetailIDCorrectMagic = iota +) diff --git a/status/status.go b/status/status.go index d24ce575..4b1f3700 100644 --- a/status/status.go +++ b/status/status.go @@ -57,6 +57,8 @@ const ( const ( // Internal is a local Code value for INTERNAL failure status. Internal Code = iota + // WrongMagicNumber is a local Code value for WRONG_MAGIC_NUMBER failure status. + WrongMagicNumber ) const ( diff --git a/status/test/generate.go b/status/test/generate.go index c8b5a666..18612122 100644 --- a/status/test/generate.go +++ b/status/test/generate.go @@ -37,7 +37,7 @@ func Status(empty bool) *status.Status { if !empty { m.SetCode(765) m.SetMessage("some string") - m.SetDetails(Details(false)) + status.SetStatusDetails(m, Details(false)) } return m diff --git a/status/types.go b/status/types.go index 3e907d86..61a05d08 100644 --- a/status/types.go +++ b/status/types.go @@ -106,9 +106,22 @@ func (x *Status) IterateDetails(f func(*Detail) bool) { } } -// SetDetails sets Detail list of the Status. -func (x *Status) SetDetails(v []*Detail) { +// ResetDetails empties the detail list. +func (x *Status) ResetDetails() { if x != nil { - x.details = v + x.details = x.details[:0] } } + +// AppendDetails appends the list of details to the Status. +func (x *Status) AppendDetails(ds ...*Detail) { + if x != nil { + x.details = append(x.details, ds...) + } +} + +// SetStatusDetails sets Detail list of the Status. +func SetStatusDetails(dst *Status, ds []*Detail) { + dst.ResetDetails() + dst.AppendDetails(ds...) +}