Skip to content

Commit

Permalink
refactor!: rename Entity.EntityName to Entity.Name
Browse files Browse the repository at this point in the history
Rename the EntityName field inside Entity to just Name to remove the
unnecessary duplication and align with Go best practices.

BREAKING CHANGE: field EntityName has been renamed to just Name inside
both comid.Entity and corim.Entity.

Signed-off-by: Sergei Trofimov <[email protected]>
  • Loading branch information
setrofim committed Jul 16, 2024
1 parent b89d933 commit 9900079
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 42 deletions.
6 changes: 3 additions & 3 deletions comid/comid.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ func (o *Comid) AddEntity(name string, regID *string, roles ...Role) *Comid {
}

e := Entity{
EntityName: MustNewStringEntityName(name),
RegID: uri,
Roles: rs,
Name: MustNewStringEntityName(name),
RegID: uri,
Roles: rs,
}

if o.Entities == nil {
Expand Down
16 changes: 8 additions & 8 deletions comid/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import (

// Entity stores an entity-map capable of CBOR and JSON serializations.
type Entity struct {
EntityName *EntityName `cbor:"0,keyasint" json:"name"`
RegID *TaggedURI `cbor:"1,keyasint,omitempty" json:"regid,omitempty"`
Roles Roles `cbor:"2,keyasint" json:"roles"`
Name *EntityName `cbor:"0,keyasint" json:"name"`
RegID *TaggedURI `cbor:"1,keyasint,omitempty" json:"regid,omitempty"`
Roles Roles `cbor:"2,keyasint" json:"roles"`

Extensions
}
Expand All @@ -41,13 +41,13 @@ func (o *Entity) GetExtensions() extensions.IMapValue {
return o.Extensions.IMapValue
}

// SetEntityName is used to set the EntityName field of Entity using supplied name
func (o *Entity) SetEntityName(name string) *Entity {
// SetName is used to set the Name field of Entity using supplied name
func (o *Entity) SetName(name string) *Entity {
if o != nil {
if name == "" {
return nil
}
o.EntityName = MustNewStringEntityName(name)
o.Name = MustNewStringEntityName(name)
}
return o
}
Expand All @@ -74,11 +74,11 @@ func (o *Entity) SetRoles(roles ...Role) *Entity {

// Valid checks for validity of the fields within each Entity
func (o Entity) Valid() error {
if o.EntityName == nil {
if o.Name == nil {
return fmt.Errorf("invalid entity: empty entity-name")
}

if err := o.EntityName.Valid(); err != nil {
if err := o.Name.Valid(); err != nil {
return fmt.Errorf("invalid entity: %w", err)
}

Expand Down
10 changes: 5 additions & 5 deletions comid/entity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestEntity_Valid_empty(t *testing.T) {
func TestEntity_Valid_name_but_no_roles(t *testing.T) {
tv := Entity{}

require.NotNil(t, tv.SetEntityName("ACME Ltd."))
require.NotNil(t, tv.SetName("ACME Ltd."))

err := tv.Valid()
assert.EqualError(t, err, "invalid entity: empty roles")
Expand All @@ -31,7 +31,7 @@ func TestEntity_Valid_name_but_no_roles(t *testing.T) {
func TestEntity_Valid_name_regid_but_no_roles(t *testing.T) {
tv := Entity{}

require.NotNil(t, tv.SetEntityName("ACME Ltd."))
require.NotNil(t, tv.SetName("ACME Ltd."))
require.NotNil(t, tv.SetRegID("https://acme.example"))

err := tv.Valid()
Expand All @@ -41,7 +41,7 @@ func TestEntity_Valid_name_regid_but_no_roles(t *testing.T) {
func TestEntity_Valid_name_regid_and_roles(t *testing.T) {
tv := Entity{}

require.NotNil(t, tv.SetEntityName("ACME Ltd."))
require.NotNil(t, tv.SetName("ACME Ltd."))
require.NotNil(t, tv.SetRegID("https://acme.example"))
require.NotNil(t, tv.SetRoles(RoleTagCreator))

Expand All @@ -62,7 +62,7 @@ func TestEntities_Valid_ok(t *testing.T) {
e := &Entity{}

require.NotNil(t,
e.SetEntityName("ACME Ltd.").
e.SetName("ACME Ltd.").
SetRegID("https://acme.example").
SetRoles(RoleTagCreator, RoleCreator),
)
Expand All @@ -77,7 +77,7 @@ func TestEntities_Valid_ok(t *testing.T) {
func TestEntity_SetEntityName_empty(t *testing.T) {
e := Entity{}

assert.Nil(t, e.SetEntityName(""))
assert.Nil(t, e.SetName(""))
}

func TestEntity_SetRegID_empty(t *testing.T) {
Expand Down
16 changes: 8 additions & 8 deletions corim/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import (

// Entity stores an entity-map capable of CBOR and JSON serializations.
type Entity struct {
EntityName *EntityName `cbor:"0,keyasint" json:"name"`
RegID *comid.TaggedURI `cbor:"1,keyasint,omitempty" json:"regid,omitempty"`
Roles Roles `cbor:"2,keyasint" json:"roles"`
Name *EntityName `cbor:"0,keyasint" json:"name"`
RegID *comid.TaggedURI `cbor:"1,keyasint,omitempty" json:"regid,omitempty"`
Roles Roles `cbor:"2,keyasint" json:"roles"`

Extensions
}
Expand Down Expand Up @@ -46,14 +46,14 @@ func (o *Entity) GetExtensions() extensions.IMapValue {
return o.Extensions.IMapValue
}

// SetEntityName is used to set the EntityName field of Entity using supplied name
func (o *Entity) SetEntityName(name any) *Entity {
// SetName is used to set the EntityName field of Entity using supplied name
func (o *Entity) SetName(name any) *Entity {

if o != nil {
if name == "" {
return nil
}
o.EntityName = MustNewStringEntityName(name)
o.Name = MustNewStringEntityName(name)
}
return o
}
Expand Down Expand Up @@ -87,11 +87,11 @@ func (o *Entity) SetRoles(roles ...Role) *Entity {

// Valid checks for validity of the fields within each Entity
func (o Entity) Valid() error {
if o.EntityName == nil {
if o.Name == nil {
return fmt.Errorf("invalid entity: empty entity-name")
}

if err := o.EntityName.Valid(); err != nil {
if err := o.Name.Valid(); err != nil {
return fmt.Errorf("invalid entity: %w", err)
}

Expand Down
18 changes: 9 additions & 9 deletions corim/entity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestEntity_Valid_uninitialized(t *testing.T) {

func TestEntity_Valid_empty_name(t *testing.T) {
tv := Entity{
EntityName: MustNewStringEntityName(""),
Name: MustNewStringEntityName(""),
}

err := tv.Valid()
Expand All @@ -35,8 +35,8 @@ func TestEntity_Valid_non_nil_empty_URI(t *testing.T) {
emptyRegID := comid.TaggedURI("")

tv := Entity{
EntityName: MustNewStringEntityName("ACME Ltd."),
RegID: &emptyRegID,
Name: MustNewStringEntityName("ACME Ltd."),
RegID: &emptyRegID,
}

err := tv.Valid()
Expand All @@ -48,8 +48,8 @@ func TestEntity_Valid_missing_roles(t *testing.T) {
regID := comid.TaggedURI("http://acme.example")

tv := Entity{
EntityName: MustNewStringEntityName("ACME Ltd."),
RegID: &regID,
Name: MustNewStringEntityName("ACME Ltd."),
RegID: &regID,
}

err := tv.Valid()
Expand All @@ -61,9 +61,9 @@ func TestEntity_Valid_unknown_role(t *testing.T) {
regID := comid.TaggedURI("http://acme.example")

tv := Entity{
EntityName: MustNewStringEntityName("ACME Ltd."),
RegID: &regID,
Roles: Roles{Role(666)},
Name: MustNewStringEntityName("ACME Ltd."),
RegID: &regID,
Roles: Roles{Role(666)},
}

err := tv.Valid()
Expand All @@ -73,7 +73,7 @@ func TestEntity_Valid_unknown_role(t *testing.T) {

func TestEntities_Valid_ok(t *testing.T) {
e := NewEntity().
SetEntityName("ACME Ltd.").
SetName("ACME Ltd.").
SetRegID("http://acme.example").
SetRoles(RoleManifestCreator)
require.NotNil(t, e)
Expand Down
2 changes: 1 addition & 1 deletion corim/example_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func Example_profile_unmarshal() {
}

fmt.Printf("Language: %s\n", *extractedComid.Language)
fmt.Printf("Entity: %s\n", *extractedComid.Entities.Values[0].EntityName)
fmt.Printf("Entity: %s\n", *extractedComid.Entities.Values[0].Name)
fmt.Printf(" %s\n", extractedComid.Entities.Values[0].
Extensions.MustGetString("Address"))

Expand Down
8 changes: 4 additions & 4 deletions corim/extensions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type TestExtensions struct {
}

func (o TestExtensions) ConstrainEntity(ent *Entity) error {
if ent.EntityName.String() != "Futurama" {
if ent.Name.String() != "Futurama" {
return errors.New(`EntityName must be "Futurama"`) // nolint:golint
}

Expand All @@ -35,7 +35,7 @@ func (o TestExtensions) ConstrainSigner(_ *Signer) error {

func TestEntityExtensions_Valid(t *testing.T) {
ent := NewEntity()
ent.SetEntityName("The Simpsons")
ent.SetName("The Simpsons")
ent.SetRoles(RoleManifestCreator)

err := ent.Valid()
Expand All @@ -48,7 +48,7 @@ func TestEntityExtensions_Valid(t *testing.T) {
err = ent.Valid()
assert.EqualError(t, err, `EntityName must be "Futurama"`)

ent.SetEntityName("Futurama")
ent.SetName("Futurama")
err = ent.Valid()
assert.NoError(t, err)

Expand Down Expand Up @@ -84,7 +84,7 @@ func TestEntityExtensions_CBOR(t *testing.T) {
err = cbor.Unmarshal(data, &ent)
assert.NoError(t, err)

assert.Equal(t, ent.EntityName.String(), "acme")
assert.Equal(t, ent.Name.String(), "acme")

address, err := ent.Get("address")
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion corim/unsignedcorim.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func (o *UnsignedCorim) SetRimValidity(notAfter time.Time, notBefore *time.Time)
func (o *UnsignedCorim) AddEntity(name string, regID *string, roles ...Role) *UnsignedCorim {
if o != nil {
e := NewEntity().
SetEntityName(name).
SetName(name).
SetRoles(roles...)

if regID != nil {
Expand Down
6 changes: 3 additions & 3 deletions corim/unsignedcorim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,9 @@ func TestUnsignedCorim_AddEntity_full(t *testing.T) {
Entities: &Entities{
Values: []Entity{
{
EntityName: MustNewStringEntityName(name),
Roles: Roles{role},
RegID: &taggedRegID,
Name: MustNewStringEntityName(name),
Roles: Roles{role},
RegID: &taggedRegID,
},
},
},
Expand Down

0 comments on commit 9900079

Please sign in to comment.