Skip to content

Commit

Permalink
Address some of the comments
Browse files Browse the repository at this point in the history
Signed-off-by: Yogesh Deshpande <[email protected]>
  • Loading branch information
yogeshbdeshpande committed Feb 6, 2024
1 parent f86fd9e commit ac5da92
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
17 changes: 8 additions & 9 deletions comid/integrityregisters.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ type IRegisterIndex interface{}

// IntegrityRegisters holds the Integrity Registers
type IntegrityRegisters struct {
Register map[IRegisterIndex]Digests
m map[IRegisterIndex]Digests
}

func NewIntegrityRegisters() *IntegrityRegisters {
reg := make(map[IRegisterIndex]Digests)
return &IntegrityRegisters{Register: reg}
return &IntegrityRegisters{m: make(map[IRegisterIndex]Digests)}
}

// AddDigests allows inserting a series of Digests at a specific RegisterIndex
Expand All @@ -38,27 +37,27 @@ func (i *IntegrityRegisters) AddDigests(index IRegisterIndex, digests Digests) e
return nil
}

// AddDigest allows inserting a Digest at a specific RegisterIndex
// AddDigest allows inserting a Digest at a specific registerIndex
func (i *IntegrityRegisters) AddDigest(index IRegisterIndex, digest swid.HashEntry) error {

if i.Register == nil {
if i.m == nil {
return fmt.Errorf("no register to add digest")
}
switch t := index.(type) {
case string, uint, uint64:
i.Register[t] = append(i.Register[t], digest)
i.m[t] = append(i.m[t], digest)
default:
return fmt.Errorf("unexpected type for index: %T", t)
}
return nil
}

func (i IntegrityRegisters) MarshalCBOR() ([]byte, error) {
return em.Marshal(i.Register)
return em.Marshal(i.m)
}

func (i *IntegrityRegisters) UnmarshalCBOR(data []byte) error {
return dm.Unmarshal(data, &i.Register)
return dm.Unmarshal(data, &i.m)
}

type keyTypeandVal struct {
Expand All @@ -69,7 +68,7 @@ type keyTypeandVal struct {
func (i IntegrityRegisters) MarshalJSON() ([]byte, error) {
jmap := make(map[string]json.RawMessage)
var newkey string
for key, val := range i.Register {
for key, val := range i.m {
var ktv keyTypeandVal
switch t := key.(type) {
case uint, uint64:
Expand Down
12 changes: 5 additions & 7 deletions comid/integrityregisters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,14 @@ func TestIntegrityRegisters_MarshalCBOR_UIntIndex_OK(t *testing.T) {
bytes, err := reg.MarshalCBOR()
require.NoError(t, err)
fmt.Printf("CBOR Payload = %x", bytes)

}

func TestIntegrityRegisters_UnmarshalCBOR_UIntIndex_OK(t *testing.T) {
bstr := MustHexDecode(nil, `a302818207583031323433353436353836393739303830313234333534363538363937393038303132343335343635383639373930383000828207583031323433353436353836393739303830313234333534363538363937393038303132343335343635383639373930383082075830313234333534363538363937393038303132343335343635383639373930383031323433353436353836393739303930018182075830313234333534363538363937393038303132343335343635383639373930383031323433353436353836393739303830`)
reg := NewIntegrityRegisters()
err := reg.UnmarshalCBOR(bstr)
require.NoError(t, err)
for index, digests := range reg.Register {
for index, digests := range reg.m {
_, ok := index.(uint64)
assert.True(t, ok)
err = digests.Valid()
Expand All @@ -100,15 +99,14 @@ func TestIntegrityRegisters_MarshalCBOR_TextIndex_OK(t *testing.T) {
bytes, err := reg.MarshalCBOR()
require.NoError(t, err)
fmt.Printf("CBOR Payload = %x", bytes)

}

func TestIntegrityRegisters_UnmarshalCBOR_TextIndex_OK(t *testing.T) {
bstr := MustHexDecode(nil, `a3613281820758303132343335343635383639373930383031323433353436353836393739303830313234333534363538363937393038306133818207583031323433353436353836393739303830313234333534363538363937393038303132343335343635383639373930383061318182075830313234333534363538363937393038303132343335343635383639373930383031323433353436353836393739303830`)
reg := NewIntegrityRegisters()
err := reg.UnmarshalCBOR(bstr)
require.NoError(t, err)
for index, digests := range reg.Register {
for index, digests := range reg.m {
_, ok := index.(string)
assert.True(t, ok)
err = digests.Valid()
Expand All @@ -129,7 +127,7 @@ func TestIntegrityRegisters_UnmarshalJSON_TextIndex_OK(t *testing.T) {
reg := NewIntegrityRegisters()
err := reg.UnmarshalJSON([]byte(j))
require.NoError(t, err)
for index, digests := range reg.Register {
for index, digests := range reg.m {
_, ok := index.(string)
assert.True(t, ok)
err = digests.Valid()
Expand All @@ -149,7 +147,7 @@ func TestIntegrityRegisters_UnmarshalJSON_UIntIndex_OK(t *testing.T) {
reg := NewIntegrityRegisters()
err := reg.UnmarshalJSON([]byte(j))
require.NoError(t, err)
for index, digests := range reg.Register {
for index, digests := range reg.m {
_, ok := index.(uint)
assert.True(t, ok)
err = digests.Valid()
Expand All @@ -168,7 +166,7 @@ func TestIntegrityRegisters_UnmarshalJSON_TextUInt_Index_OK(t *testing.T) {
reg := NewIntegrityRegisters()
err := reg.UnmarshalJSON([]byte(j))
require.NoError(t, err)
for index, digests := range reg.Register {
for index, digests := range reg.m {
_, uok := index.(uint)
_, sok := index.(string)
assert.True(t, (uok || sok))
Expand Down

0 comments on commit ac5da92

Please sign in to comment.