From 43fd3cfb4fd55eb74bf7f68b8bc66f47a82bb655 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Wed, 2 Mar 2022 11:43:54 +0300 Subject: [PATCH] [#376] status: Remove pointer from `Detail` slice Signed-off-by: Evgenii Stratonikov --- object/status.go | 2 +- status/convert.go | 12 +++--------- status/marshal.go | 4 ++-- status/test/generate.go | 8 ++++---- status/types.go | 8 ++++---- 5 files changed, 14 insertions(+), 20 deletions(-) diff --git a/object/status.go b/object/status.go index 067c372d..dc37ad25 100644 --- a/object/status.go +++ b/object/status.go @@ -67,7 +67,7 @@ func WriteAccessDeniedDesc(st *status.Status, desc string) { d.SetID(detailAccessDeniedDesc) d.SetValue([]byte(desc)) - st.AppendDetails(&d) + st.AppendDetails(d) } } diff --git a/status/convert.go b/status/convert.go index 8195096d..50145ef9 100644 --- a/status/convert.go +++ b/status/convert.go @@ -71,27 +71,21 @@ func (x *Status) FromGRPCMessage(m grpc.Message) error { } var ( - ds []*Detail + ds []Detail dsV2 = v.GetDetails() ) if dsV2 != nil { ln := len(dsV2) - ds = make([]*Detail, 0, ln) + ds = make([]Detail, ln) for i := 0; i < ln; i++ { - var p *Detail - if dsV2[i] != nil { - p = new(Detail) - - if err := p.FromGRPCMessage(dsV2[i]); err != nil { + if err := ds[i].FromGRPCMessage(dsV2[i]); err != nil { return err } } - - ds = append(ds, p) } } diff --git a/status/marshal.go b/status/marshal.go index da6572e5..5e04f24f 100644 --- a/status/marshal.go +++ b/status/marshal.go @@ -88,7 +88,7 @@ func (x *Status) StableMarshal(buf []byte) ([]byte, error) { offset += n for i := range x.details { - n, err = protoutil.NestedStructureMarshal(statusDetailsFNum, buf[offset:], x.details[i]) + n, err = protoutil.NestedStructureMarshal(statusDetailsFNum, buf[offset:], &x.details[i]) if err != nil { return nil, err } @@ -104,7 +104,7 @@ func (x *Status) StableSize() (size int) { size += protoutil.StringSize(statusMsgFNum, x.msg) for i := range x.details { - size += protoutil.NestedStructureSize(statusDetailsFNum, x.details[i]) + size += protoutil.NestedStructureSize(statusDetailsFNum, &x.details[i]) } return size diff --git a/status/test/generate.go b/status/test/generate.go index 18612122..01df633a 100644 --- a/status/test/generate.go +++ b/status/test/generate.go @@ -17,13 +17,13 @@ func Detail(empty bool) *status.Detail { } // Details returns several status.Detail messages filled with static random values. -func Details(empty bool) []*status.Detail { - var res []*status.Detail +func Details(empty bool) []status.Detail { + var res []status.Detail if !empty { res = append(res, - Detail(false), - Detail(false), + *Detail(false), + *Detail(false), ) } diff --git a/status/types.go b/status/types.go index 6fe233b0..8bcb3af2 100644 --- a/status/types.go +++ b/status/types.go @@ -53,7 +53,7 @@ type Status struct { msg string - details []*Detail + details []Detail } // Code returns code of the Status. @@ -104,7 +104,7 @@ func (x *Status) NumberOfDetails() int { func (x *Status) IterateDetails(f func(*Detail) bool) { if x != nil { for i := range x.details { - if f(x.details[i]) { + if f(&x.details[i]) { break } } @@ -119,14 +119,14 @@ func (x *Status) ResetDetails() { } // AppendDetails appends the list of details to the Status. -func (x *Status) AppendDetails(ds ...*Detail) { +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) { +func SetStatusDetails(dst *Status, ds []Detail) { dst.ResetDetails() dst.AppendDetails(ds...) }