[#1238] Adopt neofs-cli for non pointer slices in SDK

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2022-03-15 14:16:46 +03:00 committed by Alex Vanin
parent 8ae869b95e
commit 7f3195b197
11 changed files with 55 additions and 78 deletions

View file

@ -56,7 +56,7 @@ type ListContainersRes struct {
} }
// IDList returns list of identifiers of user's containers. // IDList returns list of identifiers of user's containers.
func (x ListContainersRes) IDList() []*cid.ID { func (x ListContainersRes) IDList() []cid.ID {
return x.cliRes.Containers() return x.cliRes.Containers()
} }

View file

@ -74,7 +74,7 @@ func TestParseTable(t *testing.T) {
actualRecord := eaclTable.Records()[len(eaclTable.Records())-1] actualRecord := eaclTable.Records()[len(eaclTable.Records())-1]
equalRecords(t, expectedRecord, actualRecord) equalRecords(t, expectedRecord, &actualRecord)
} }
}) })
} }

View file

@ -600,9 +600,9 @@ func getSessionToken(path string) (*session.Token, error) {
return tok, nil return tok, nil
} }
func prettyPrintContainerList(cmd *cobra.Command, list []*cid.ID) { func prettyPrintContainerList(cmd *cobra.Command, list []cid.ID) {
for i := range list { for i := range list {
cmd.Println(list[i]) cmd.Println(&list[i]) // stringer defined on pointer
} }
} }
@ -644,8 +644,8 @@ func parseContainerPolicy(policyString string) (*netmap.PlacementPolicy, error)
return nil, errors.New("can't parse placement policy") return nil, errors.New("can't parse placement policy")
} }
func parseAttributes(attributes []string) ([]*container.Attribute, error) { func parseAttributes(attributes []string) ([]container.Attribute, error) {
result := make([]*container.Attribute, 0, len(attributes)+2) // name + timestamp attributes result := make([]container.Attribute, len(attributes), len(attributes)+2) // name + timestamp attributes
for i := range attributes { for i := range attributes {
kvPair := strings.Split(attributes[i], attributeDelimiter) kvPair := strings.Split(attributes[i], attributeDelimiter)
@ -653,27 +653,22 @@ func parseAttributes(attributes []string) ([]*container.Attribute, error) {
return nil, errors.New("invalid container attribute") return nil, errors.New("invalid container attribute")
} }
parsedAttribute := container.NewAttribute() result[i].SetKey(kvPair[0])
parsedAttribute.SetKey(kvPair[0]) result[i].SetValue(kvPair[1])
parsedAttribute.SetValue(kvPair[1])
result = append(result, parsedAttribute)
} }
if !containerNoTimestamp { if !containerNoTimestamp {
timestamp := container.NewAttribute() index := len(result)
timestamp.SetKey(container.AttributeTimestamp) result = append(result, container.Attribute{})
timestamp.SetValue(strconv.FormatInt(time.Now().Unix(), 10)) result[index].SetKey(container.AttributeTimestamp)
result[index].SetValue(strconv.FormatInt(time.Now().Unix(), 10))
result = append(result, timestamp)
} }
if containerName != "" { if containerName != "" {
cnrName := container.NewAttribute() index := len(result)
cnrName.SetKey(container.AttributeName) result = append(result, container.Attribute{})
cnrName.SetValue(containerName) result[index].SetKey(container.AttributeName)
result[index].SetValue(containerName)
result = append(result, cnrName)
} }
return result, nil return result, nil

View file

@ -414,22 +414,24 @@ func putObject(cmd *cobra.Command, _ []string) {
expiresOn, _ := cmd.Flags().GetUint64(putExpiresOnFlag) expiresOn, _ := cmd.Flags().GetUint64(putExpiresOnFlag)
if expiresOn > 0 { if expiresOn > 0 {
var expAttr *object.Attribute var expAttrFound bool
expAttrValue := strconv.FormatUint(expiresOn, 10)
for _, a := range attrs { for i := range attrs {
if a.Key() == objectV2.SysAttributeExpEpoch { if attrs[i].Key() == objectV2.SysAttributeExpEpoch {
expAttr = a attrs[i].SetValue(expAttrValue)
expAttrFound = true
break break
} }
} }
if expAttr == nil { if !expAttrFound {
expAttr = object.NewAttribute() index := len(attrs)
expAttr.SetKey(objectV2.SysAttributeExpEpoch) attrs = append(attrs, object.Attribute{})
attrs = append(attrs, expAttr) attrs[index].SetKey(objectV2.SysAttributeExpEpoch)
attrs[index].SetValue(expAttrValue)
} }
expAttr.SetValue(strconv.FormatUint(expiresOn, 10))
} }
obj := object.New() obj := object.New()
@ -719,7 +721,7 @@ func parseSearchFilters(cmd *cobra.Command) (object.SearchFilters, error) {
return fs, nil return fs, nil
} }
func parseObjectAttrs(cmd *cobra.Command) ([]*object.Attribute, error) { func parseObjectAttrs(cmd *cobra.Command) ([]object.Attribute, error) {
var rawAttrs []string var rawAttrs []string
raw := cmd.Flag("attributes").Value.String() raw := cmd.Flag("attributes").Value.String()
@ -727,33 +729,31 @@ func parseObjectAttrs(cmd *cobra.Command) ([]*object.Attribute, error) {
rawAttrs = strings.Split(raw, ",") rawAttrs = strings.Split(raw, ",")
} }
attrs := make([]*object.Attribute, 0, len(rawAttrs)+2) // name + timestamp attributes attrs := make([]object.Attribute, len(rawAttrs), len(rawAttrs)+2) // name + timestamp attributes
for i := range rawAttrs { for i := range rawAttrs {
kv := strings.SplitN(rawAttrs[i], "=", 2) kv := strings.SplitN(rawAttrs[i], "=", 2)
if len(kv) != 2 { if len(kv) != 2 {
return nil, fmt.Errorf("invalid attribute format: %s", rawAttrs[i]) return nil, fmt.Errorf("invalid attribute format: %s", rawAttrs[i])
} }
attr := object.NewAttribute() attrs[i].SetKey(kv[0])
attr.SetKey(kv[0]) attrs[i].SetValue(kv[1])
attr.SetValue(kv[1])
attrs = append(attrs, attr)
} }
disableFilename, _ := cmd.Flags().GetBool("disable-filename") disableFilename, _ := cmd.Flags().GetBool("disable-filename")
if !disableFilename { if !disableFilename {
filename := filepath.Base(cmd.Flag("file").Value.String()) filename := filepath.Base(cmd.Flag("file").Value.String())
attr := object.NewAttribute() index := len(attrs)
attr.SetKey(object.AttributeFileName) attrs = append(attrs, object.Attribute{})
attr.SetValue(filename) attrs[index].SetKey(object.AttributeFileName)
attrs = append(attrs, attr) attrs[index].SetValue(filename)
} }
disableTime, _ := cmd.Flags().GetBool("disable-timestamp") disableTime, _ := cmd.Flags().GetBool("disable-timestamp")
if !disableTime { if !disableTime {
attr := object.NewAttribute() index := len(attrs)
attr.SetKey(object.AttributeTimestamp) attrs = append(attrs, object.Attribute{})
attr.SetValue(strconv.FormatInt(time.Now().Unix(), 10)) attrs[index].SetKey(object.AttributeTimestamp)
attrs = append(attrs, attr) attrs[index].SetValue(strconv.FormatInt(time.Now().Unix(), 10))
} }
return attrs, nil return attrs, nil
@ -894,7 +894,7 @@ func printSplitHeader(cmd *cobra.Command, obj *object.Object) error {
} }
for _, child := range obj.Children() { for _, child := range obj.Children() {
cmd.Printf("Split ChildID: %s\n", child) cmd.Printf("Split ChildID: %s\n", &child) // stringer defined on pointer
} }
if signature := obj.Signature(); signature != nil { if signature := obj.Signature(); signature != nil {

View file

@ -170,15 +170,11 @@ func putSG(cmd *cobra.Command, _ []string) {
cid, err := getCID(cmd) cid, err := getCID(cmd)
exitOnErr(cmd, err) exitOnErr(cmd, err)
members := make([]*oidSDK.ID, 0, len(sgMembers)) members := make([]oidSDK.ID, len(sgMembers))
for i := range sgMembers { for i := range sgMembers {
id := oidSDK.NewID() err = members[i].Parse(sgMembers[i])
err = id.Parse(sgMembers[i])
exitOnErr(cmd, errf("could not parse object ID: %w", err)) exitOnErr(cmd, errf("could not parse object ID: %w", err))
members = append(members, id)
} }
var ( var (
@ -265,7 +261,7 @@ func getSG(cmd *cobra.Command, _ []string) {
cmd.Println("Members:") cmd.Println("Members:")
for i := range members { for i := range members {
cmd.Printf("\t%s\n", members[i]) cmd.Printf("\t%s\n", &members[i]) // stringer defined on pointer
} }
} }
} }

View file

@ -180,14 +180,10 @@ func (v *FormatValidator) ValidateContent(o *object.Object) error {
idList := tombstone.Members() idList := tombstone.Members()
addrList := make([]*addressSDK.Address, 0, len(idList)) addrList := make([]*addressSDK.Address, 0, len(idList))
for _, id := range idList { for i := range idList {
if id == nil {
return fmt.Errorf("(%T) empty member in tombstone", v)
}
a := addressSDK.NewAddress() a := addressSDK.NewAddress()
a.SetContainerID(cid) a.SetContainerID(cid)
a.SetObjectID(id) a.SetObjectID(&idList[i])
addrList = append(addrList, a) addrList = append(addrList, a)
} }
@ -208,12 +204,6 @@ func (v *FormatValidator) ValidateContent(o *object.Object) error {
if err := sg.Unmarshal(o.Payload()); err != nil { if err := sg.Unmarshal(o.Payload()); err != nil {
return fmt.Errorf("(%T) could not unmarshal SG content: %w", v, err) return fmt.Errorf("(%T) could not unmarshal SG content: %w", v, err)
} }
for _, id := range sg.Members() {
if id == nil {
return fmt.Errorf("(%T) empty member in SG", v)
}
}
case object.TypeLock: case object.TypeLock:
if len(o.Payload()) == 0 { if len(o.Payload()) == 0 {
return errors.New("empty payload in lock") return errors.New("empty payload in lock")

View file

@ -94,14 +94,10 @@ func (s *Shard) refillMetabase() error {
memberIDs := tombstone.Members() memberIDs := tombstone.Members()
tombMembers := make([]*addressSDK.Address, 0, len(memberIDs)) tombMembers := make([]*addressSDK.Address, 0, len(memberIDs))
for _, id := range memberIDs { for i := range memberIDs {
if id == nil {
return errors.New("empty member in tombstone")
}
a := addressSDK.NewAddress() a := addressSDK.NewAddress()
a.SetContainerID(cid) a.SetContainerID(cid)
a.SetObjectID(id) a.SetObjectID(&memberIDs[i])
tombMembers = append(tombMembers, a) tombMembers = append(tombMembers, a)
} }

View file

@ -79,7 +79,7 @@ func stateFromAPI(s netmapAPI.NodeState) control.NetmapStatus {
} }
} }
func attributesFromAPI(apiAttrs []*netmapAPI.NodeAttribute) []*control.NodeInfo_Attribute { func attributesFromAPI(apiAttrs []netmapAPI.NodeAttribute) []*control.NodeInfo_Attribute {
attrs := make([]*control.NodeInfo_Attribute, 0, len(apiAttrs)) attrs := make([]*control.NodeInfo_Attribute, 0, len(apiAttrs))
for _, apiAttr := range apiAttrs { for _, apiAttr := range apiAttrs {

View file

@ -95,7 +95,7 @@ func traverseSplitChain(r HeadReceiver, addr *addressSDK.Address, h SplitMemberH
addr.SetContainerID(cid) addr.SetContainerID(cid)
addr.SetObjectID(res.Link()) addr.SetObjectID(res.Link())
chain := make([]*oidSDK.ID, 0) chain := make([]oidSDK.ID, 0)
if _, err := traverseSplitChain(r, addr, func(member *object.Object, reverseDirection bool) (stop bool) { if _, err := traverseSplitChain(r, addr, func(member *object.Object, reverseDirection bool) (stop bool) {
children := member.Children() children := member.Children()
@ -114,7 +114,7 @@ func traverseSplitChain(r HeadReceiver, addr *addressSDK.Address, h SplitMemberH
var reverseChain []*object.Object var reverseChain []*object.Object
for i := range chain { for i := range chain {
addr.SetObjectID(chain[i]) addr.SetObjectID(&chain[i])
if stop, err := traverseSplitChain(r, addr, func(member *object.Object, reverseDirection bool) (stop bool) { if stop, err := traverseSplitChain(r, addr, func(member *object.Object, reverseDirection bool) (stop bool) {
if !reverseDirection { if !reverseDirection {

View file

@ -136,7 +136,7 @@ func CommonPrmFromV2(req interface {
return nil, err return nil, err
} }
default: default:
xhdr := sessionsdk.NewXHeaderFromV2(xHdrs[i]) xhdr := sessionsdk.NewXHeaderFromV2(&xHdrs[i])
prm.xhdrs = append(prm.xhdrs, xhdr) prm.xhdrs = append(prm.xhdrs, xhdr)
} }

View file

@ -15,10 +15,10 @@ import (
// with information about members collected via HeadReceiver. // with information about members collected via HeadReceiver.
// //
// Resulting storage group consists of physically stored objects only. // Resulting storage group consists of physically stored objects only.
func CollectMembers(r objutil.HeadReceiver, cid *cid.ID, members []*oidSDK.ID) (*storagegroup.StorageGroup, error) { func CollectMembers(r objutil.HeadReceiver, cid *cid.ID, members []oidSDK.ID) (*storagegroup.StorageGroup, error) {
var ( var (
sumPhySize uint64 sumPhySize uint64
phyMembers []*oidSDK.ID phyMembers []oidSDK.ID
phyHashes [][]byte phyHashes [][]byte
addr = addressSDK.NewAddress() addr = addressSDK.NewAddress()
sg = storagegroup.New() sg = storagegroup.New()
@ -27,10 +27,10 @@ func CollectMembers(r objutil.HeadReceiver, cid *cid.ID, members []*oidSDK.ID) (
addr.SetContainerID(cid) addr.SetContainerID(cid)
for i := range members { for i := range members {
addr.SetObjectID(members[i]) addr.SetObjectID(&members[i])
if err := objutil.IterateAllSplitLeaves(r, addr, func(leaf *object.Object) { if err := objutil.IterateAllSplitLeaves(r, addr, func(leaf *object.Object) {
phyMembers = append(phyMembers, leaf.ID()) phyMembers = append(phyMembers, *leaf.ID())
sumPhySize += leaf.PayloadSize() sumPhySize += leaf.PayloadSize()
phyHashes = append(phyHashes, leaf.PayloadHomomorphicHash().Sum()) phyHashes = append(phyHashes, leaf.PayloadHomomorphicHash().Sum())
}); err != nil { }); err != nil {