From 9c5d3d9dfaffa3d3bc6724905bc5f80ef30080eb Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Fri, 11 Mar 2022 12:16:08 +0300 Subject: [PATCH] [#168] client: Adopt replacement of pointer slices with struct slices Signed-off-by: Alex Vanin --- client/common.go | 9 +++------ client/container.go | 16 ++++++++-------- client/object_hash.go | 3 +-- client/object_search.go | 8 ++++---- client/object_search_test.go | 4 ++-- client/reputation.go | 7 ++----- client/status/common.go | 2 +- 7 files changed, 21 insertions(+), 28 deletions(-) diff --git a/client/common.go b/client/common.go index 56de0a3..759606e 100644 --- a/client/common.go +++ b/client/common.go @@ -71,14 +71,11 @@ func (x *prmCommonMeta) WithXHeaders(hs ...string) { func (x prmCommonMeta) writeToMetaHeader(h *v2session.RequestMetaHeader) { if len(x.xHeaders) > 0 { - hs := make([]*v2session.XHeader, 0, len(x.xHeaders)/2) + hs := make([]v2session.XHeader, len(x.xHeaders)/2) for i := 0; i < len(x.xHeaders); i += 2 { - var h v2session.XHeader - h.SetKey(x.xHeaders[i]) - h.SetValue(x.xHeaders[i+1]) - - hs = append(hs, &h) + hs[i].SetKey(x.xHeaders[i]) + hs[i].SetValue(x.xHeaders[i+1]) } h.SetXHeaders(hs) diff --git a/client/container.go b/client/container.go index 2cea90b..d86dd54 100644 --- a/client/container.go +++ b/client/container.go @@ -255,17 +255,17 @@ func (x *PrmContainerList) SetAccount(id owner.ID) { type ResContainerList struct { statusRes - ids []*cid.ID + ids []cid.ID } // Containers returns list of identifiers of the account-owned containers. // // Client doesn't retain value so modification is safe. -func (x ResContainerList) Containers() []*cid.ID { +func (x ResContainerList) Containers() []cid.ID { return x.ids } -func (x *ResContainerList) setContainers(ids []*cid.ID) { +func (x *ResContainerList) setContainers(ids []cid.ID) { x.ids = ids } @@ -319,10 +319,10 @@ func (c *Client) ContainerList(ctx context.Context, prm PrmContainerList) (*ResC cc.result = func(r responseV2) { resp := r.(*v2container.ListResponse) - ids := make([]*cid.ID, 0, len(resp.GetBody().GetContainerIDs())) + ids := make([]cid.ID, len(resp.GetBody().GetContainerIDs())) - for _, cidV2 := range resp.GetBody().GetContainerIDs() { - ids = append(ids, cid.NewFromV2(cidV2)) + for i, cidV2 := range resp.GetBody().GetContainerIDs() { + ids[i] = *cid.NewFromV2(&cidV2) } res.setContainers(ids) @@ -695,9 +695,9 @@ func (c *Client) ContainerAnnounceUsedSpace(ctx context.Context, prm PrmAnnounce } // convert list of SDK announcement structures into NeoFS-API v2 list - v2announce := make([]*v2container.UsedSpaceAnnouncement, 0, len(prm.announcements)) + v2announce := make([]v2container.UsedSpaceAnnouncement, len(prm.announcements)) for i := range prm.announcements { - v2announce = append(v2announce, prm.announcements[i].ToV2()) + v2announce[i] = *prm.announcements[i].ToV2() } // prepare body of the NeoFS-API v2 request and request itself diff --git a/client/object_hash.go b/client/object_hash.go index 44b187c..a0d5b9c 100644 --- a/client/object_hash.go +++ b/client/object_hash.go @@ -71,10 +71,9 @@ func (x *PrmObjectHash) SetRangeList(r ...uint64) { panic("odd number of range parameters") } - rs := make([]*v2object.Range, ln/2) + rs := make([]v2object.Range, ln/2) for i := 0; i < ln/2; i++ { - rs[i] = new(v2object.Range) rs[i].SetOffset(r[2*i]) rs[i].SetLength(r[2*i+1]) } diff --git a/client/object_search.go b/client/object_search.go index 7e93ccf..38f6399 100644 --- a/client/object_search.go +++ b/client/object_search.go @@ -95,7 +95,7 @@ type ObjectListReader struct { // initially bound to contextCall bodyResp v2object.SearchResponseBody - tail []*v2refs.ObjectID + tail []v2refs.ObjectID } // UseKey specifies private key to sign the requests. @@ -130,7 +130,7 @@ func (x *ObjectListReader) Read(buf []oid.ID) (int, bool) { } for i := 0; i < read; i++ { - buf[i] = *oid.NewIDFromV2(x.tail[i]) // need smth better + buf[i] = *oid.NewIDFromV2(&x.tail[i]) // need smth better } x.tail = x.tail[read:] @@ -140,7 +140,7 @@ func (x *ObjectListReader) Read(buf []oid.ID) (int, bool) { } var ok bool - var ids []*v2refs.ObjectID + var ids []v2refs.ObjectID var i, ln, rem int for { @@ -164,7 +164,7 @@ func (x *ObjectListReader) Read(buf []oid.ID) (int, bool) { } for i = 0; i < ln; i++ { - buf[read+i] = *oid.NewIDFromV2(ids[i]) // need smth better + buf[read+i] = *oid.NewIDFromV2(&ids[i]) // need smth better } read += ln diff --git a/client/object_search_test.go b/client/object_search_test.go index 3ce52c1..7809729 100644 --- a/client/object_search_test.go +++ b/client/object_search_test.go @@ -175,9 +175,9 @@ func testListReaderResponse(t *testing.T) (*ObjectListReader, func(id []oid.ID) resp := new(object.SearchResponse) resp.SetBody(new(object.SearchResponseBody)) - v2id := make([]*refs.ObjectID, len(id)) + v2id := make([]refs.ObjectID, len(id)) for i := range id { - v2id[i] = id[i].ToV2() + v2id[i] = *id[i].ToV2() } resp.GetBody().SetIDList(v2id) err := signatureV2.SignServiceMessage(&p.PrivateKey, resp) diff --git a/client/reputation.go b/client/reputation.go index a1b4b46..c6806a5 100644 --- a/client/reputation.go +++ b/client/reputation.go @@ -65,11 +65,8 @@ func (c *Client) AnnounceLocalTrust(ctx context.Context, prm PrmAnnounceLocalTru reqBody := new(v2reputation.AnnounceLocalTrustRequestBody) reqBody.SetEpoch(prm.epoch) - trusts := make([]*reputation.Trust, 0, len(prm.trusts)) - - for i := range prm.trusts { - trusts = append(trusts, &prm.trusts[i]) - } + trusts := make([]reputation.Trust, len(prm.trusts)) + copy(trusts, prm.trusts) reqBody.SetTrusts(reputation.TrustsToV2(trusts)) diff --git a/client/status/common.go b/client/status/common.go index e5051c0..05c5ae2 100644 --- a/client/status/common.go +++ b/client/status/common.go @@ -99,7 +99,7 @@ func (x *WrongMagicNumber) WriteCorrectMagic(magic uint64) { d.SetValue(buf) // attach the detail - x.v2.AppendDetails(&d) + x.v2.AppendDetails(d) } // CorrectMagic returns network magic returned by the server.