From 6cf4d46a82aeb2f7fc92ff03f21ce2188bdc50fe Mon Sep 17 00:00:00 2001 From: Alejandro Visiedo Date: Wed, 29 Nov 2023 08:26:27 +0100 Subject: [PATCH] fix: refactor code with new dependencies Signed-off-by: Alejandro Visiedo --- cmd/xrhidgen/flag.go | 16 +++++++------ cmd/xrhidgen/internal.go | 2 +- identity.go | 50 ++++++++++++++++++++-------------------- internal.go | 10 ++++---- system.go | 12 +++++----- user.go | 12 +++++----- 6 files changed, 52 insertions(+), 50 deletions(-) diff --git a/cmd/xrhidgen/flag.go b/cmd/xrhidgen/flag.go index 910bc41..0085149 100644 --- a/cmd/xrhidgen/flag.go +++ b/cmd/xrhidgen/flag.go @@ -46,25 +46,27 @@ func (f *BoolFlag) Set(v string) error { func (f BoolFlag) IsBoolFlag() bool { return true } -type Float64Flag struct { - Value *float64 +type Float32Flag struct { + Value *float32 } -func (f Float64Flag) String() string { +func (f Float32Flag) String() string { if f.Value != nil { - return strconv.FormatFloat(*f.Value, 'G', -1, 64) + return strconv.FormatFloat(float64(*f.Value), 'G', -1, 32) } return "" } -func (f *Float64Flag) Set(v string) error { +func (f *Float32Flag) Set(v string) error { if f.Value == nil { - f.Value = new(float64) + f.Value = new(float32) } var err error - *f.Value, err = strconv.ParseFloat(v, 64) + var value float64 + value, err = strconv.ParseFloat(v, 32) if err != nil { return err } + *f.Value = float32(value) return nil } diff --git a/cmd/xrhidgen/internal.go b/cmd/xrhidgen/internal.go index 07e13f0..cb55f20 100644 --- a/cmd/xrhidgen/internal.go +++ b/cmd/xrhidgen/internal.go @@ -11,7 +11,7 @@ import ( ) var internalFlags struct { - authType Float64Flag + authType Float32Flag crossAccess BoolFlag orgID StringFlag } diff --git a/identity.go b/identity.go index 9a4831e..10f41ef 100644 --- a/identity.go +++ b/identity.go @@ -17,14 +17,14 @@ type Identity struct { // NewIdentity will build and return a partially populated Identity data // structure, using any values that are present in template. -func NewIdentity(template Identity) (*identity.Identity, error) { - var id identity.Identity +func NewIdentity(template Identity) (*identity.XRHID, error) { + var id identity.XRHID if template.AccountNumber != nil { - id.Identity.AccountNumber = template.AccountNumber + id.Identity.AccountNumber = *template.AccountNumber } else { if faker.Bool() { - id.Identity.AccountNumber = ptrstring(faker.DigitsWithSize(5)) + id.Identity.AccountNumber = faker.DigitsWithSize(5) } } @@ -35,10 +35,10 @@ func NewIdentity(template Identity) (*identity.Identity, error) { } if template.EmployeeAccountNumber != nil { - id.Identity.EmployeeAccountNumber = template.EmployeeAccountNumber + id.Identity.EmployeeAccountNumber = *template.EmployeeAccountNumber } else { if faker.Bool() { - id.Identity.EmployeeAccountNumber = ptrstring(faker.DigitsWithSize(5)) + id.Identity.EmployeeAccountNumber = faker.DigitsWithSize(5) } } @@ -49,9 +49,9 @@ func NewIdentity(template Identity) (*identity.Identity, error) { } if template.Type != nil { - id.Identity.Type = template.Type + id.Identity.Type = *template.Type } else { - id.Identity.Type = ptrstring(faker.String()) + id.Identity.Type = faker.String() } return &id, nil @@ -60,7 +60,7 @@ func NewIdentity(template Identity) (*identity.Identity, error) { // NewAssociateIdentity will build and return a fully populated Associate // identity record, using any values that are present in identityTemplate and // associateTemplate. -func NewAssociateIdentity(identityTemplate Identity, associateTemplate Associate) (*identity.Identity, error) { +func NewAssociateIdentity(identityTemplate Identity, associateTemplate Associate) (*identity.XRHID, error) { id, err := NewIdentity(identityTemplate) if err != nil { return nil, err @@ -71,9 +71,9 @@ func NewAssociateIdentity(identityTemplate Identity, associateTemplate Associate return nil, err } - id.Identity.Associate = associate + id.Identity.Associate = *associate - id.Identity.Type = ptrstring("Associate") + id.Identity.Type = "Associate" return id, nil } @@ -81,7 +81,7 @@ func NewAssociateIdentity(identityTemplate Identity, associateTemplate Associate // NewInternalIdentity will build and return a fully populated Internal identity // record, using any values that are present in identityTemplate and // internalTemplate. -func NewInternalIdentity(identityTemplate Identity, internalTemplate Internal) (*identity.Identity, error) { +func NewInternalIdentity(identityTemplate Identity, internalTemplate Internal) (*identity.XRHID, error) { id, err := NewIdentity(identityTemplate) if err != nil { return nil, err @@ -92,9 +92,9 @@ func NewInternalIdentity(identityTemplate Identity, internalTemplate Internal) ( return nil, err } - id.Identity.Internal = internal + id.Identity.Internal = *internal - id.Identity.Type = ptrstring("Internal") + id.Identity.Type = "Internal" return id, nil } @@ -102,7 +102,7 @@ func NewInternalIdentity(identityTemplate Identity, internalTemplate Internal) ( // NewSystemIdentity will build and return a fully populated System identity // record, using any values that are present in identityTemplate and // systemTemplate. -func NewSystemIdentity(identityTemplate Identity, systemTemplate System) (*identity.Identity, error) { +func NewSystemIdentity(identityTemplate Identity, systemTemplate System) (*identity.XRHID, error) { id, err := NewIdentity(identityTemplate) if err != nil { return nil, err @@ -113,10 +113,10 @@ func NewSystemIdentity(identityTemplate Identity, systemTemplate System) (*ident return nil, err } - id.Identity.System = system + id.Identity.System = *system - id.Identity.Type = ptrstring("System") - id.Identity.Internal = &identity.Internal{ + id.Identity.Type = "System" + id.Identity.Internal = identity.Internal{ OrgID: id.Identity.OrgID, } @@ -125,7 +125,7 @@ func NewSystemIdentity(identityTemplate Identity, systemTemplate System) (*ident // NewX509Identity will build and return a fully populated X509 identity record, // using any values that are present in identityTemplate and x509Template. -func NewX509Identity(identityTemplate Identity, x509Template X509) (*identity.Identity, error) { +func NewX509Identity(identityTemplate Identity, x509Template X509) (*identity.XRHID, error) { id, err := NewIdentity(identityTemplate) if err != nil { return nil, err @@ -136,16 +136,16 @@ func NewX509Identity(identityTemplate Identity, x509Template X509) (*identity.Id return nil, err } - id.Identity.X509 = x509 + id.Identity.X509 = *x509 - id.Identity.Type = ptrstring("X509") + id.Identity.Type = "X509" return id, nil } // NewUserIdentity will build and return a fully populated User identity record, // using any values that are present in identityTemplate and userTemplate. -func NewUserIdentity(identityTemplate Identity, userTemplate User) (*identity.Identity, error) { +func NewUserIdentity(identityTemplate Identity, userTemplate User) (*identity.XRHID, error) { id, err := NewIdentity(identityTemplate) if err != nil { return nil, err @@ -156,10 +156,10 @@ func NewUserIdentity(identityTemplate Identity, userTemplate User) (*identity.Id return nil, err } - id.Identity.User = user + id.Identity.User = *user - id.Identity.Type = ptrstring("User") - id.Identity.Internal = &identity.Internal{ + id.Identity.Type = "User" + id.Identity.Internal = identity.Internal{ OrgID: id.Identity.OrgID, } diff --git a/internal.go b/internal.go index 864c7da..2f7cbd7 100644 --- a/internal.go +++ b/internal.go @@ -8,7 +8,7 @@ import ( // Internal holds values to be used as input when generating an internal // identity record. type Internal struct { - AuthTime *float64 + AuthTime *float32 CrossAccess *bool OrgID *string } @@ -19,15 +19,15 @@ func NewInternal(template Internal) (*identity.Internal, error) { var id identity.Internal if template.AuthTime != nil { - id.AuthTime = template.AuthTime + id.AuthTime = *template.AuthTime } else { - id.AuthTime = ptrfloat64(float64(faker.Duration())) + id.AuthTime = float32(faker.Duration()) } if template.CrossAccess != nil { - id.CrossAccess = template.CrossAccess + id.CrossAccess = *template.CrossAccess } else { - id.CrossAccess = ptrbool(faker.Bool()) + id.CrossAccess = faker.Bool() } if template.OrgID != nil { diff --git a/system.go b/system.go index 6dd374c..a8d2294 100644 --- a/system.go +++ b/system.go @@ -19,21 +19,21 @@ func NewSystem(template System) (*identity.System, error) { var id identity.System if template.CertType != nil { - id.CertType = template.CertType + id.CertType = *template.CertType } else { - id.CertType = ptrstring(faker.Pick("", "consumer", "system")) + id.CertType = faker.Pick("", "consumer", "system") } if template.ClusterID != nil { - id.ClusterID = template.ClusterID + id.ClusterId = *template.ClusterID } else { - id.ClusterID = ptrstring(faker.String()) + id.ClusterId = faker.String() } if template.CN != nil { - id.CN = *template.CN + id.CommonName = *template.CN } else { - id.CN = faker.String() + id.CommonName = faker.String() } return &id, nil diff --git a/user.go b/user.go index ab02c56..0360b73 100644 --- a/user.go +++ b/user.go @@ -36,21 +36,21 @@ func NewUser(template User) (*identity.User, error) { } if template.IsActive != nil { - id.IsActive = *template.IsActive + id.Active = *template.IsActive } else { - id.IsActive = faker.Bool() + id.Active = faker.Bool() } if template.IsInternal != nil { - id.IsInternal = *template.IsInternal + id.Internal = *template.IsInternal } else { - id.IsInternal = faker.Bool() + id.Internal = faker.Bool() } if template.IsOrgAdmin != nil { - id.IsOrgAdmin = *template.IsOrgAdmin + id.OrgAdmin = *template.IsOrgAdmin } else { - id.IsOrgAdmin = faker.Bool() + id.OrgAdmin = faker.Bool() } if template.LastName != nil {