Skip to content

Commit

Permalink
fix: refactor code with new dependencies
Browse files Browse the repository at this point in the history
Signed-off-by: Alejandro Visiedo <[email protected]>
  • Loading branch information
avisiedo committed Jan 5, 2024
1 parent 262ed7d commit 6cf4d46
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 50 deletions.
16 changes: 9 additions & 7 deletions cmd/xrhidgen/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion cmd/xrhidgen/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

var internalFlags struct {
authType Float64Flag
authType Float32Flag
crossAccess BoolFlag
orgID StringFlag
}
Expand Down
50 changes: 25 additions & 25 deletions identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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)
}
}

Expand All @@ -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
Expand All @@ -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
Expand All @@ -71,17 +71,17 @@ 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
}

// 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
Expand All @@ -92,17 +92,17 @@ 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
}

// 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
Expand All @@ -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,
}

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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,
}

Expand Down
10 changes: 5 additions & 5 deletions internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down
12 changes: 6 additions & 6 deletions system.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 6cf4d46

Please sign in to comment.