From e15dd105fed135b8ab293cf6c65d34c3d854747f Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Fri, 11 Jun 2021 14:57:21 +0300 Subject: [PATCH] [#307] v2/acl/test: Do not allocate memory if `!empty` Move all memory allocation and field settings in `Generate...(empty bool)` functions behind `if !empty` check. Do not create empty slices if `empty == true`. Signed-off-by: Pavel Karpy --- v2/acl/test/generate.go | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/v2/acl/test/generate.go b/v2/acl/test/generate.go index 3b1e1a7..0f72119 100644 --- a/v2/acl/test/generate.go +++ b/v2/acl/test/generate.go @@ -8,7 +8,10 @@ import ( func GenerateBearerToken(empty bool) *acl.BearerToken { m := new(acl.BearerToken) - m.SetBody(GenerateBearerTokenBody(empty)) + if !empty { + m.SetBody(GenerateBearerTokenBody(false)) + } + m.SetSignature(accountingtest.GenerateSignature(empty)) return m @@ -17,9 +20,11 @@ func GenerateBearerToken(empty bool) *acl.BearerToken { func GenerateBearerTokenBody(empty bool) *acl.BearerTokenBody { m := new(acl.BearerTokenBody) - m.SetOwnerID(accountingtest.GenerateOwnerID(empty)) - m.SetEACL(GenerateTable(empty)) - m.SetLifetime(GenerateTokenLifetime(empty)) + if !empty { + m.SetOwnerID(accountingtest.GenerateOwnerID(false)) + m.SetEACL(GenerateTable(false)) + m.SetLifetime(GenerateTokenLifetime(false)) + } return m } @@ -27,15 +32,18 @@ func GenerateBearerTokenBody(empty bool) *acl.BearerTokenBody { func GenerateTable(empty bool) *acl.Table { m := new(acl.Table) - m.SetRecords(GenerateRecords(empty)) - m.SetContainerID(accountingtest.GenerateContainerID(empty)) + if !empty { + m.SetRecords(GenerateRecords(false)) + m.SetContainerID(accountingtest.GenerateContainerID(false)) + } + m.SetVersion(accountingtest.GenerateVersion(empty)) return m } func GenerateRecords(empty bool) []*acl.Record { - rs := make([]*acl.Record, 0) + var rs []*acl.Record if !empty { rs = append(rs, @@ -53,16 +61,15 @@ func GenerateRecord(empty bool) *acl.Record { if !empty { m.SetAction(acl.ActionAllow) m.SetOperation(acl.OperationGet) + m.SetFilters(GenerateFilters(false)) + m.SetTargets(GenerateTargets(false)) } - m.SetFilters(GenerateFilters(empty)) - m.SetTargets(GenerateTargets(empty)) - return m } func GenerateFilters(empty bool) []*acl.HeaderFilter { - fs := make([]*acl.HeaderFilter, 0) + var fs []*acl.HeaderFilter if !empty { fs = append(fs, @@ -88,7 +95,7 @@ func GenerateFilter(empty bool) *acl.HeaderFilter { } func GenerateTargets(empty bool) []*acl.Target { - ts := make([]*acl.Target, 0) + var ts []*acl.Target if !empty { ts = append(ts,