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

View file

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

View file

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

View file

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

View file

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

View file

@ -16,7 +16,7 @@ func Container() (x container.Container) {
x.Init() x.Init()
x.SetAttribute("some attribute", "value") x.SetAttribute("some attribute", "value")
x.SetOwner(*owner) x.SetOwner(owner)
x.SetBasicACL(BasicACL()) x.SetBasicACL(BasicACL())
x.SetPlacementPolicy(netmaptest.PlacementPolicy()) 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. // 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) 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. // Object is expected (but not required) to be blank. Object must not be nil.
func InitCreation(dst *Object, rf RequiredFields) { func InitCreation(dst *Object, rf RequiredFields) {
dst.SetContainerID(rf.Container) dst.SetContainerID(rf.Container)
dst.SetOwnerID(&rf.Owner) dst.SetOwnerID(rf.Owner)
} }
// NewFromV2 wraps v2 Object message to Object. // 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. // OwnerID returns identifier of the object owner and True.
func (o *Object) OwnerID() *user.ID { func (o *Object) OwnerID() user.ID {
var id user.ID var id user.ID
m := (*object.Object)(o).GetHeader().GetOwnerID() m := (*object.Object)(o).GetHeader().GetOwnerID()
@ -210,11 +210,11 @@ func (o *Object) OwnerID() *user.ID {
_ = id.ReadFromV2(*m) _ = id.ReadFromV2(*m)
} }
return &id return id
} }
// SetOwnerID sets identifier of the object owner. // 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) { o.setHeaderField(func(h *object.Header) {
var m refs.OwnerID var m refs.OwnerID
v.WriteToV2(&m) v.WriteToV2(&m)
@ -621,8 +621,10 @@ func (o *Object) UnmarshalJSON(data []byte) error {
return formatCheck((*object.Object)(o)) return formatCheck((*object.Object)(o))
} }
var errOIDNotSet = errors.New("object ID is not set") var (
var errCIDNotSet = errors.New("container ID is not set") errOIDNotSet = errors.New("object ID is not set")
errCIDNotSet = errors.New("container ID is not set")
)
func formatCheck(v2 *object.Object) error { func formatCheck(v2 *object.Object) error {
var ( var (

View file

@ -12,7 +12,7 @@ import (
func TestInitCreation(t *testing.T) { func TestInitCreation(t *testing.T) {
var o object.Object var o object.Object
cnr := cidtest.ID() cnr := cidtest.ID()
own := *usertest.ID() own := usertest.ID()
object.InitCreation(&o, object.RequiredFields{ object.InitCreation(&o, object.RequiredFields{
Container: cnr, Container: cnr,
@ -22,5 +22,5 @@ func TestInitCreation(t *testing.T) {
cID, set := o.ContainerID() cID, set := o.ContainerID()
require.True(t, set) require.True(t, set)
require.Equal(t, cnr, cID) 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 var owner user.ID
user.IDFromKey(&owner, pk.PrivateKey.PublicKey) user.IDFromKey(&owner, pk.PrivateKey.PublicKey)
hdr.SetOwnerID(&owner) hdr.SetOwnerID(owner)
expected := make([]byte, payloadSize) expected := make([]byte, payloadSize)
_, _ = rand.Read(expected) _, _ = rand.Read(expected)

View file

@ -27,7 +27,7 @@ func TestTransformer(t *testing.T) {
var owner user.ID var owner user.ID
user.IDFromKey(&owner, pk.PrivateKey.PublicKey) user.IDFromKey(&owner, pk.PrivateKey.PublicKey)
hdr.SetOwnerID(&owner) hdr.SetOwnerID(owner)
expectedPayload := make([]byte, maxSize*2+maxSize/2) expectedPayload := make([]byte, maxSize*2+maxSize/2)
_, _ = rand.Read(expectedPayload) _, _ = rand.Read(expectedPayload)
@ -41,7 +41,7 @@ func TestTransformer(t *testing.T) {
require.True(t, ok) require.True(t, ok)
require.Equal(t, cnr, childCnr) require.Equal(t, cnr, childCnr)
require.Equal(t, objectSDK.TypeRegular, tt.objects[i].Type()) 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() payload := tt.objects[i].Payload()
require.EqualValues(t, tt.objects[i].PayloadSize(), len(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{ cliPrm := sdkClient.PrmBalanceGet{
Account: &prm.account, Account: prm.account,
} }
start := time.Now() start := time.Now()
@ -508,7 +508,7 @@ func (c *clientWrapper) containerList(ctx context.Context, prm PrmContainerList)
} }
cliPrm := sdkClient.PrmContainerList{ cliPrm := sdkClient.PrmContainerList{
Account: &prm.ownerID, Account: prm.ownerID,
} }
start := time.Now() start := time.Now()

View file

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

View file

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

View file

@ -12,6 +12,10 @@ import (
"github.com/nspcc-dev/neo-go/pkg/util" "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 identifies users of the FrostFS system.
// //
// ID is mutually compatible with git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/refs.OwnerID // 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. // See also WriteToV2.
func (x *ID) ReadFromV2(m refs.OwnerID) error { func (x *ID) ReadFromV2(m refs.OwnerID) error {
w := m.GetValue() w := m.GetValue()
if len(w) != 25 { if len(w) != idSize {
return fmt.Errorf("invalid length %d, expected 25", len(w)) return fmt.Errorf("invalid length %d, expected %d", len(w), idSize)
} }
if w[0] != address.NEO3Prefix { if w[0] != address.NEO3Prefix {
@ -57,10 +61,10 @@ func (x ID) WriteToV2(m *refs.OwnerID) {
// SetScriptHash forms user ID from wallet address scripthash. // SetScriptHash forms user ID from wallet address scripthash.
func (x *ID) SetScriptHash(scriptHash util.Uint160) { func (x *ID) SetScriptHash(scriptHash util.Uint160) {
if cap(x.w) < 25 { if cap(x.w) < idSize {
x.w = make([]byte, 25) x.w = make([]byte, idSize)
} else if len(x.w) < 25 { } else if len(x.w) < idSize {
x.w = x.w[:25] x.w = x.w[:idSize]
} }
x.w[0] = address.Prefix x.w[0] = address.Prefix
@ -114,3 +118,8 @@ func (x ID) String() string {
func (x ID) Equals(x2 ID) bool { func (x ID) Equals(x2 ID) bool {
return bytes.Equal(x.w, x2.w) 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) { func TestV2_ID(t *testing.T) {
id := *usertest.ID() id := usertest.ID()
var m refs.OwnerID var m refs.OwnerID
var id2 ID var id2 ID
@ -91,7 +91,7 @@ func TestV2_ID(t *testing.T) {
} }
func TestID_EncodeToString(t *testing.T) { func TestID_EncodeToString(t *testing.T) {
id := *usertest.ID() id := usertest.ID()
s := id.EncodeToString() s := id.EncodeToString()
@ -110,8 +110,8 @@ func TestID_EncodeToString(t *testing.T) {
} }
func TestID_Equal(t *testing.T) { func TestID_Equal(t *testing.T) {
id1 := *usertest.ID() id1 := usertest.ID()
id2 := *usertest.ID() id2 := usertest.ID()
id3 := id1 id3 := id1
require.True(t, id1.Equals(id1)) // self-equality require.True(t, id1.Equals(id1)) // self-equality

View file

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