From b24589b62d499666d719aeeb0af24188190925e5 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Tue, 17 May 2022 16:32:03 +0300 Subject: [PATCH] [#1386] eacl/v2: Use raw structs where possible Signed-off-by: Evgenii Stratonikov --- pkg/services/object/acl/eacl/v2/headers.go | 35 +++++++++++++------ pkg/services/object/acl/eacl/v2/object.go | 40 ++++++---------------- pkg/services/object/acl/eacl/v2/opts.go | 4 +-- pkg/services/object/acl/eacl/v2/xheader.go | 4 +-- 4 files changed, 39 insertions(+), 44 deletions(-) diff --git a/pkg/services/object/acl/eacl/v2/headers.go b/pkg/services/object/acl/eacl/v2/headers.go index 33c4a62e2..9c3dd27a3 100644 --- a/pkg/services/object/acl/eacl/v2/headers.go +++ b/pkg/services/object/acl/eacl/v2/headers.go @@ -1,6 +1,7 @@ package v2 import ( + "errors" "fmt" "github.com/nspcc-dev/neofs-api-go/v2/acl" @@ -12,6 +13,7 @@ import ( "github.com/nspcc-dev/neofs-sdk-go/object" objectSDKAddress "github.com/nspcc-dev/neofs-sdk-go/object/address" objectSDKID "github.com/nspcc-dev/neofs-sdk-go/object/id" + "github.com/nspcc-dev/neofs-sdk-go/owner" sessionSDK "github.com/nspcc-dev/neofs-sdk-go/session" ) @@ -55,6 +57,10 @@ func NewMessageHeaderSource(opts ...Option) (eaclSDK.TypedHeaderSource, error) { opts[i](cfg) } + if cfg.msg == nil { + return nil, errors.New("message is not provided") + } + objHdrs, err := cfg.objectHeaders() if err != nil { return nil, err @@ -93,7 +99,7 @@ func (h *cfg) objectHeaders() ([]eaclSDK.Header, error) { switch m := h.msg.(type) { default: panic(fmt.Sprintf("unexpected message type %T", h.msg)) - case *requestXHeaderSource: + case requestXHeaderSource: switch req := m.req.(type) { case *objectV2.GetRequest: return h.localObjectHeaders(h.addr) @@ -148,9 +154,9 @@ func (h *cfg) objectHeaders() ([]eaclSDK.Header, error) { } } - return []eaclSDK.Header{cidHeader(&cnr)}, nil + return []eaclSDK.Header{cidHeader(cnr)}, nil } - case *responseXHeaderSource: + case responseXHeaderSource: switch resp := m.resp.(type) { default: hs, _ := h.localObjectHeaders(h.addr) @@ -205,17 +211,24 @@ func (h *cfg) localObjectHeaders(addr *objectSDKAddress.Address) ([]eaclSDK.Head return headersFromObject(obj, addr), nil } -func cidHeader(idCnr *cid.ID) eaclSDK.Header { - return &sysObjHdr{ +func cidHeader(idCnr cid.ID) sysObjHdr { + return sysObjHdr{ k: acl.FilterObjectContainerID, - v: cidValue(idCnr), + v: idCnr.String(), } } -func oidHeader(oid *objectSDKID.ID) eaclSDK.Header { - return &sysObjHdr{ +func oidHeader(oid objectSDKID.ID) sysObjHdr { + return sysObjHdr{ k: acl.FilterObjectID, - v: idValue(oid), + v: oid.String(), + } +} + +func ownerIDHeader(ownerID *owner.ID) sysObjHdr { + return sysObjHdr{ + k: acl.FilterObjectOwnerID, + v: ownerID.String(), } } @@ -223,10 +236,10 @@ func addressHeaders(addr *objectSDKAddress.Address) []eaclSDK.Header { cnr, _ := addr.ContainerID() res := make([]eaclSDK.Header, 1, 2) - res[0] = cidHeader(&cnr) + res[0] = cidHeader(cnr) if oid, ok := addr.ObjectID(); ok { - res = append(res, oidHeader(&oid)) + res = append(res, oidHeader(oid)) } return res diff --git a/pkg/services/object/acl/eacl/v2/object.go b/pkg/services/object/acl/eacl/v2/object.go index c0d52a2bc..531b5c473 100644 --- a/pkg/services/object/acl/eacl/v2/object.go +++ b/pkg/services/object/acl/eacl/v2/object.go @@ -4,38 +4,23 @@ import ( "strconv" "github.com/nspcc-dev/neofs-api-go/v2/acl" - cid "github.com/nspcc-dev/neofs-sdk-go/container/id" eaclSDK "github.com/nspcc-dev/neofs-sdk-go/eacl" "github.com/nspcc-dev/neofs-sdk-go/object" objectSDKAddress "github.com/nspcc-dev/neofs-sdk-go/object/address" - objectSDKID "github.com/nspcc-dev/neofs-sdk-go/object/id" - "github.com/nspcc-dev/neofs-sdk-go/owner" ) type sysObjHdr struct { k, v string } -func (s *sysObjHdr) Key() string { +func (s sysObjHdr) Key() string { return s.k } -func (s *sysObjHdr) Value() string { +func (s sysObjHdr) Value() string { return s.v } -func idValue(id *objectSDKID.ID) string { - return id.String() -} - -func cidValue(id *cid.ID) string { - return id.String() -} - -func ownerIDValue(id *owner.ID) string { - return id.String() -} - func u64Value(v uint64) string { return strconv.FormatUint(v, 10) } @@ -52,30 +37,27 @@ func headersFromObject(obj *object.Object, addr *objectSDKAddress.Address) []eac id, _ := addr.ObjectID() res = append(res, - cidHeader(&cnr), + cidHeader(cnr), // owner ID - &sysObjHdr{ - k: acl.FilterObjectOwnerID, - v: ownerIDValue(obj.OwnerID()), - }, + ownerIDHeader(obj.OwnerID()), // creation epoch - &sysObjHdr{ + sysObjHdr{ k: acl.FilterObjectCreationEpoch, v: u64Value(obj.CreationEpoch()), }, // payload size - &sysObjHdr{ + sysObjHdr{ k: acl.FilterObjectPayloadLength, v: u64Value(obj.PayloadSize()), }, - oidHeader(&id), + oidHeader(id), // object version - &sysObjHdr{ + sysObjHdr{ k: acl.FilterObjectVersion, v: obj.Version().String(), }, // object type - &sysObjHdr{ + sysObjHdr{ k: acl.FilterObjectType, v: obj.Type().String(), }, @@ -83,7 +65,7 @@ func headersFromObject(obj *object.Object, addr *objectSDKAddress.Address) []eac cs, ok := obj.PayloadChecksum() if ok { - res = append(res, &sysObjHdr{ + res = append(res, sysObjHdr{ k: acl.FilterObjectPayloadHash, v: cs.String(), }) @@ -91,7 +73,7 @@ func headersFromObject(obj *object.Object, addr *objectSDKAddress.Address) []eac cs, ok = obj.PayloadHomomorphicHash() if ok { - res = append(res, &sysObjHdr{ + res = append(res, sysObjHdr{ k: acl.FilterObjectHomomorphicHash, v: cs.String(), }) diff --git a/pkg/services/object/acl/eacl/v2/opts.go b/pkg/services/object/acl/eacl/v2/opts.go index 656b0831e..3d07a41e2 100644 --- a/pkg/services/object/acl/eacl/v2/opts.go +++ b/pkg/services/object/acl/eacl/v2/opts.go @@ -21,7 +21,7 @@ func WithLocalObjectStorage(v *engine.StorageEngine) Option { func WithServiceRequest(v Request) Option { return func(c *cfg) { - c.msg = &requestXHeaderSource{ + c.msg = requestXHeaderSource{ req: v, } } @@ -29,7 +29,7 @@ func WithServiceRequest(v Request) Option { func WithServiceResponse(resp Response, req Request) Option { return func(c *cfg) { - c.msg = &responseXHeaderSource{ + c.msg = responseXHeaderSource{ resp: resp, req: req, } diff --git a/pkg/services/object/acl/eacl/v2/xheader.go b/pkg/services/object/acl/eacl/v2/xheader.go index 8991f2b3e..62364e818 100644 --- a/pkg/services/object/acl/eacl/v2/xheader.go +++ b/pkg/services/object/acl/eacl/v2/xheader.go @@ -18,7 +18,7 @@ type responseXHeaderSource struct { req Request } -func (s *requestXHeaderSource) GetXHeaders() []session.XHeader { +func (s requestXHeaderSource) GetXHeaders() []session.XHeader { ln := 0 xHdrs := make([][]session.XHeader, 0) @@ -41,7 +41,7 @@ func (s *requestXHeaderSource) GetXHeaders() []session.XHeader { return res } -func (s *responseXHeaderSource) GetXHeaders() []session.XHeader { +func (s responseXHeaderSource) GetXHeaders() []session.XHeader { ln := 0 xHdrs := make([][]session.XHeader, 0)