[#190] sdk-go: Pass user.ID by value

Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
This commit is contained in:
Dmitrii Stepanov 2023-11-21 11:35:10 +03:00
parent 1c07098740
commit 157a9930e8
17 changed files with 53 additions and 42 deletions

View file

@ -107,7 +107,7 @@ func TestToken_ForUser(t *testing.T) {
require.Zero(t, m.GetBody())
// set value
usr := *usertest.ID()
usr := usertest.ID()
var usrV2 refs.OwnerID
usr.WriteToV2(&usrV2)
@ -243,11 +243,11 @@ func TestToken_AssertContainer(t *testing.T) {
func TestToken_AssertUser(t *testing.T) {
var val bearer.Token
usr := *usertest.ID()
usr := usertest.ID()
require.True(t, val.AssertUser(usr))
val.ForUser(*usertest.ID())
val.ForUser(usertest.ID())
require.False(t, val.AssertUser(usr))
val.ForUser(usr)
@ -332,7 +332,7 @@ func TestToken_ReadFromV2(t *testing.T) {
val.WriteToV2(&m2)
require.Equal(t, m, m2)
usr, usr2 := *usertest.ID(), *usertest.ID()
usr, usr2 := usertest.ID(), usertest.ID()
require.True(t, val.AssertUser(usr))
require.True(t, val.AssertUser(usr2))

View file

@ -13,7 +13,7 @@ func Token() (t bearer.Token) {
t.SetExp(3)
t.SetNbf(2)
t.SetIat(1)
t.ForUser(*usertest.ID())
t.ForUser(usertest.ID())
t.SetEACLTable(*eacltest.Table())
return t

View file

@ -19,7 +19,7 @@ import (
type PrmBalanceGet struct {
XHeaders []string
Account *user.ID
Account user.ID
}
// SetAccount sets identifier of the FrostFS account for which the balance is requested.
@ -27,11 +27,11 @@ type PrmBalanceGet struct {
//
// Deprecated: Use PrmBalanceGet.Account instead.
func (x *PrmBalanceGet) SetAccount(id user.ID) {
x.Account = &id
x.Account = id
}
func (x *PrmBalanceGet) buildRequest(c *Client) (*v2accounting.BalanceRequest, error) {
if x.Account == nil {
if x.Account.IsEmpty() {
return nil, errorAccountNotSet
}

View file

@ -19,7 +19,7 @@ import (
type PrmContainerList struct {
XHeaders []string
Account *user.ID
Account user.ID
}
// SetAccount sets identifier of the FrostFS account to list the containers.
@ -27,11 +27,11 @@ type PrmContainerList struct {
//
// Deprecated: Use PrmContainerList.Account instead.
func (x *PrmContainerList) SetAccount(id user.ID) {
x.Account = &id
x.Account = id
}
func (x *PrmContainerList) buildRequest(c *Client) (*v2container.ListRequest, error) {
if x.Account == nil {
if x.Account.IsEmpty() {
return nil, errorAccountNotSet
}

View file

@ -78,7 +78,7 @@ func TestContainer_Owner(t *testing.T) {
val = containertest.Container()
owner := *usertest.ID()
owner := usertest.ID()
val.SetOwner(owner)

View file

@ -16,7 +16,7 @@ func Container() (x container.Container) {
x.Init()
x.SetAttribute("some attribute", "value")
x.SetOwner(*owner)
x.SetOwner(owner)
x.SetBasicACL(BasicACL())
x.SetPlacementPolicy(netmaptest.PlacementPolicy())

View file

@ -125,7 +125,7 @@ func (r *Record) AddObjectContainerIDFilter(m Match, id cid.ID) {
}
// AddObjectOwnerIDFilter adds filter by object owner ID.
func (r *Record) AddObjectOwnerIDFilter(m Match, id *user.ID) {
func (r *Record) AddObjectOwnerIDFilter(m Match, id user.ID) {
r.addObjectReservedFilter(m, fKeyObjOwnerID, id)
}

View file

@ -39,7 +39,7 @@ type RequiredFields struct {
// Object is expected (but not required) to be blank. Object must not be nil.
func InitCreation(dst *Object, rf RequiredFields) {
dst.SetContainerID(rf.Container)
dst.SetOwnerID(&rf.Owner)
dst.SetOwnerID(rf.Owner)
}
// NewFromV2 wraps v2 Object message to Object.
@ -201,8 +201,8 @@ func (o *Object) SetContainerID(v cid.ID) {
})
}
// OwnerID returns identifier of the object owner.
func (o *Object) OwnerID() *user.ID {
// OwnerID returns identifier of the object owner and True.
func (o *Object) OwnerID() user.ID {
var id user.ID
m := (*object.Object)(o).GetHeader().GetOwnerID()
@ -210,11 +210,11 @@ func (o *Object) OwnerID() *user.ID {
_ = id.ReadFromV2(*m)
}
return &id
return id
}
// SetOwnerID sets identifier of the object owner.
func (o *Object) SetOwnerID(v *user.ID) {
func (o *Object) SetOwnerID(v user.ID) {
o.setHeaderField(func(h *object.Header) {
var m refs.OwnerID
v.WriteToV2(&m)
@ -621,8 +621,10 @@ func (o *Object) UnmarshalJSON(data []byte) error {
return formatCheck((*object.Object)(o))
}
var errOIDNotSet = errors.New("object ID is not set")
var errCIDNotSet = errors.New("container ID is not set")
var (
errOIDNotSet = errors.New("object ID is not set")
errCIDNotSet = errors.New("container ID is not set")
)
func formatCheck(v2 *object.Object) error {
var (

View file

@ -12,7 +12,7 @@ import (
func TestInitCreation(t *testing.T) {
var o object.Object
cnr := cidtest.ID()
own := *usertest.ID()
own := usertest.ID()
object.InitCreation(&o, object.RequiredFields{
Container: cnr,
@ -22,5 +22,5 @@ func TestInitCreation(t *testing.T) {
cID, set := o.ContainerID()
require.True(t, set)
require.Equal(t, cnr, cID)
require.Equal(t, &own, o.OwnerID())
require.Equal(t, own, o.OwnerID())
}

View file

@ -34,7 +34,7 @@ func TestTransformerSizeHintCorrectness(t *testing.T) {
var owner user.ID
user.IDFromKey(&owner, pk.PrivateKey.PublicKey)
hdr.SetOwnerID(&owner)
hdr.SetOwnerID(owner)
expected := make([]byte, payloadSize)
_, _ = rand.Read(expected)

View file

@ -27,7 +27,7 @@ func TestTransformer(t *testing.T) {
var owner user.ID
user.IDFromKey(&owner, pk.PrivateKey.PublicKey)
hdr.SetOwnerID(&owner)
hdr.SetOwnerID(owner)
expectedPayload := make([]byte, maxSize*2+maxSize/2)
_, _ = rand.Read(expectedPayload)
@ -41,7 +41,7 @@ func TestTransformer(t *testing.T) {
require.True(t, ok)
require.Equal(t, cnr, childCnr)
require.Equal(t, objectSDK.TypeRegular, tt.objects[i].Type())
require.Equal(t, &owner, tt.objects[i].OwnerID())
require.Equal(t, owner, tt.objects[i].OwnerID())
payload := tt.objects[i].Payload()
require.EqualValues(t, tt.objects[i].PayloadSize(), len(payload))

View file

@ -422,7 +422,7 @@ func (c *clientWrapper) balanceGet(ctx context.Context, prm PrmBalanceGet) (acco
}
cliPrm := sdkClient.PrmBalanceGet{
Account: &prm.account,
Account: prm.account,
}
start := time.Now()
@ -508,7 +508,7 @@ func (c *clientWrapper) containerList(ctx context.Context, prm PrmContainerList)
}
cliPrm := sdkClient.PrmContainerList{
Account: &prm.ownerID,
Account: prm.ownerID,
}
start := time.Now()

View file

@ -37,7 +37,7 @@ func TestContainerProtocolV2(t *testing.T) {
restoreID()
// Owner
usr := *usertest.ID()
usr := usertest.ID()
var usrV2 refs.OwnerID
usr.WriteToV2(&usrV2)
restoreUser := func() {

View file

@ -53,7 +53,7 @@ func TestObjectProtocolV2(t *testing.T) {
restoreID()
// Owner
usr := *usertest.ID()
usr := usertest.ID()
var usrV2 refs.OwnerID
usr.WriteToV2(&usrV2)
restoreUser := func() {

View file

@ -12,6 +12,10 @@ import (
"github.com/nspcc-dev/neo-go/pkg/util"
)
const idSize = 25
var zeroSlice = bytes.Repeat([]byte{0}, idSize)
// ID identifies users of the FrostFS system.
//
// ID is mutually compatible with git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/refs.OwnerID
@ -30,8 +34,8 @@ type ID struct {
// See also WriteToV2.
func (x *ID) ReadFromV2(m refs.OwnerID) error {
w := m.GetValue()
if len(w) != 25 {
return fmt.Errorf("invalid length %d, expected 25", len(w))
if len(w) != idSize {
return fmt.Errorf("invalid length %d, expected %d", len(w), idSize)
}
if w[0] != address.NEO3Prefix {
@ -57,10 +61,10 @@ func (x ID) WriteToV2(m *refs.OwnerID) {
// SetScriptHash forms user ID from wallet address scripthash.
func (x *ID) SetScriptHash(scriptHash util.Uint160) {
if cap(x.w) < 25 {
x.w = make([]byte, 25)
} else if len(x.w) < 25 {
x.w = x.w[:25]
if cap(x.w) < idSize {
x.w = make([]byte, idSize)
} else if len(x.w) < idSize {
x.w = x.w[:idSize]
}
x.w[0] = address.Prefix
@ -114,3 +118,8 @@ func (x ID) String() string {
func (x ID) Equals(x2 ID) bool {
return bytes.Equal(x.w, x2.w)
}
// IsEmpty returns True, if ID is empty value.
func (x ID) IsEmpty() bool {
return bytes.Equal(zeroSlice, x.w)
}

View file

@ -48,7 +48,7 @@ func TestID_SetScriptHash(t *testing.T) {
}
func TestV2_ID(t *testing.T) {
id := *usertest.ID()
id := usertest.ID()
var m refs.OwnerID
var id2 ID
@ -91,7 +91,7 @@ func TestV2_ID(t *testing.T) {
}
func TestID_EncodeToString(t *testing.T) {
id := *usertest.ID()
id := usertest.ID()
s := id.EncodeToString()
@ -110,8 +110,8 @@ func TestID_EncodeToString(t *testing.T) {
}
func TestID_Equal(t *testing.T) {
id1 := *usertest.ID()
id2 := *usertest.ID()
id1 := usertest.ID()
id2 := usertest.ID()
id3 := id1
require.True(t, id1.Equals(id1)) // self-equality

View file

@ -6,7 +6,7 @@ import (
)
// ID returns random user.ID.
func ID() *user.ID {
func ID() user.ID {
key, err := keys.NewPrivateKey()
if err != nil {
panic(err)
@ -15,5 +15,5 @@ func ID() *user.ID {
var x user.ID
user.IDFromKey(&x, key.PrivateKey.PublicKey)
return &x
return x
}