refactor: make fullBytes private and create a getter

This commit is contained in:
Derrick Hammer 2024-01-06 07:11:51 -05:00
parent 8c29a284ce
commit f45e297791
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
5 changed files with 25 additions and 21 deletions

View File

@ -47,7 +47,7 @@ func (cid *CID) getPrefixBytes() []byte {
func (cid *CID) ToBytes() []byte { func (cid *CID) ToBytes() []byte {
if cid.Type == types.CIDTypeBridge { if cid.Type == types.CIDTypeBridge {
return cid.Hash.FullBytes return cid.Hash.fullBytes
} else if cid.Type == types.CIDTypeRaw { } else if cid.Type == types.CIDTypeRaw {
sizeBytes := utils.EncodeEndian(cid.Size, 8) sizeBytes := utils.EncodeEndian(cid.Size, 8)
@ -58,10 +58,10 @@ func (cid *CID) ToBytes() []byte {
sizeBytes = []byte{0} sizeBytes = []byte{0}
} }
return utils.ConcatBytes(cid.getPrefixBytes(), cid.Hash.FullBytes, sizeBytes) return utils.ConcatBytes(cid.getPrefixBytes(), cid.Hash.fullBytes, sizeBytes)
} }
return utils.ConcatBytes(cid.getPrefixBytes(), cid.Hash.FullBytes) return utils.ConcatBytes(cid.getPrefixBytes(), cid.Hash.fullBytes)
} }
func Decode(cid string) (*CID, error) { func Decode(cid string) (*CID, error) {

View File

@ -76,7 +76,7 @@ func (c *EncryptedCID) ToBytes() []byte {
c.encryptionAlgorithm, c.encryptionAlgorithm,
byte(c.chunkSizeAsPowerOf2), byte(c.chunkSizeAsPowerOf2),
} }
data = append(data, c.encryptedBlobHash.FullBytes...) data = append(data, c.encryptedBlobHash.fullBytes...)
data = append(data, c.encryptionKey...) data = append(data, c.encryptionKey...)
data = append(data, utils.EncodeEndian(c.padding, 4)...) data = append(data, utils.EncodeEndian(c.padding, 4)...)
data = append(data, c.OriginalCID.ToBytes()...) data = append(data, c.OriginalCID.ToBytes()...)

View File

@ -19,22 +19,26 @@ var (
type MultihashCode = int type MultihashCode = int
type Multihash struct { type Multihash struct {
FullBytes []byte fullBytes []byte
}
func (m *Multihash) FullBytes() []byte {
return m.fullBytes
} }
var _ json.Marshaler = (*Multihash)(nil) var _ json.Marshaler = (*Multihash)(nil)
var _ json.Unmarshaler = (*Multihash)(nil) var _ json.Unmarshaler = (*Multihash)(nil)
func NewMultihash(fullBytes []byte) *Multihash { func NewMultihash(fullBytes []byte) *Multihash {
return &Multihash{FullBytes: fullBytes} return &Multihash{fullBytes: fullBytes}
} }
func (m *Multihash) FunctionType() types.HashType { func (m *Multihash) FunctionType() types.HashType {
return types.HashType(m.FullBytes[0]) return types.HashType(m.fullBytes[0])
} }
func (m *Multihash) HashBytes() []byte { func (m *Multihash) HashBytes() []byte {
return m.FullBytes[1:] return m.fullBytes[1:]
} }
func MultihashFromBase64Url(hash string) (*Multihash, error) { func MultihashFromBase64Url(hash string) (*Multihash, error) {
@ -53,26 +57,26 @@ func MultihashFromBase64Url(hash string) (*Multihash, error) {
} }
func (m *Multihash) ToBase64Url() (string, error) { func (m *Multihash) ToBase64Url() (string, error) {
return bases.ToBase64Url(m.FullBytes) return bases.ToBase64Url(m.fullBytes)
} }
func (m *Multihash) ToBase32() (string, error) { func (m *Multihash) ToBase32() (string, error) {
return bases.ToBase32(m.FullBytes) return bases.ToBase32(m.fullBytes)
} }
func (m *Multihash) ToString() (string, error) { func (m *Multihash) ToString() (string, error) {
if m.FunctionType() == types.HashType(types.CIDTypeBridge) { if m.FunctionType() == types.HashType(types.CIDTypeBridge) {
return string(m.FullBytes), nil // Assumes the bytes are valid UTF-8 return string(m.fullBytes), nil // Assumes the bytes are valid UTF-8
} }
return m.ToBase64Url() return m.ToBase64Url()
} }
func (m *Multihash) Equals(other *Multihash) bool { func (m *Multihash) Equals(other *Multihash) bool {
return bytes.Equal(m.FullBytes, other.FullBytes) return bytes.Equal(m.fullBytes, other.fullBytes)
} }
func (m *Multihash) HashCode() MultihashCode { func (m *Multihash) HashCode() MultihashCode {
return utils.HashCode(m.FullBytes[:4]) return utils.HashCode(m.fullBytes[:4])
} }
func (b *Multihash) UnmarshalJSON(data []byte) error { func (b *Multihash) UnmarshalJSON(data []byte) error {
@ -81,7 +85,7 @@ func (b *Multihash) UnmarshalJSON(data []byte) error {
return err return err
} }
b.FullBytes = decodedData b.fullBytes = decodedData
return nil return nil
} }
func (b Multihash) MarshalJSON() ([]byte, error) { func (b Multihash) MarshalJSON() ([]byte, error) {

View File

@ -21,7 +21,7 @@ func TestFromBase64Url(t *testing.T) {
{ {
name: "Valid Base64 URL Encoded String", name: "Valid Base64 URL Encoded String",
args: args{hash: testdata.MediaBase64CID}, args: args{hash: testdata.MediaBase64CID},
want: &Multihash{FullBytes: testdata.MediaCIDBytes}, want: &Multihash{fullBytes: testdata.MediaCIDBytes},
wantErr: false, wantErr: false,
}, },
{ {
@ -51,7 +51,7 @@ func TestFromBase64Url(t *testing.T) {
{ {
name: "Long String", name: "Long String",
args: args{hash: "uYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWF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hYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFh"},
want: &Multihash{FullBytes: []byte(strings.Repeat("a", 750))}, want: &Multihash{fullBytes: []byte(strings.Repeat("a", 750))},
wantErr: false, wantErr: false,
}, },
} }
@ -95,7 +95,7 @@ func TestMultihash_FunctionType(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
m := &Multihash{ m := &Multihash{
FullBytes: tt.fields.FullBytes, fullBytes: tt.fields.FullBytes,
} }
if got := m.FunctionType(); got != tt.want { if got := m.FunctionType(); got != tt.want {
t.Errorf("FunctionType() = %v, want %v", got, tt.want) t.Errorf("FunctionType() = %v, want %v", got, tt.want)
@ -131,7 +131,7 @@ func TestMultihash_ToBase32(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
m := &Multihash{ m := &Multihash{
FullBytes: tt.fields.FullBytes, fullBytes: tt.fields.FullBytes,
} }
got, err := m.ToBase32() got, err := m.ToBase32()
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
@ -172,7 +172,7 @@ func TestMultihash_ToBase64Url(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
m := &Multihash{ m := &Multihash{
FullBytes: tt.fields.FullBytes, fullBytes: tt.fields.FullBytes,
} }
got, err := m.ToBase64Url() got, err := m.ToBase64Url()
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
@ -198,7 +198,7 @@ func TestNewMultihash(t *testing.T) {
{ {
name: "Valid Base64 URL Encoded String", name: "Valid Base64 URL Encoded String",
args: args{fullBytes: testdata.RawCIDBytes}, args: args{fullBytes: testdata.RawCIDBytes},
want: &Multihash{FullBytes: testdata.RawCIDBytes}, want: &Multihash{fullBytes: testdata.RawCIDBytes},
}, },
} }
for _, tt := range tests { for _, tt := range tests {

View File

@ -43,7 +43,7 @@ func (fv *FileVersion) EncodeMsgpack(enc *msgpack.Encoder) error {
if len(fv.Hashes) > 0 { if len(fv.Hashes) > 0 {
hashesData := make([][]byte, len(fv.Hashes)) hashesData := make([][]byte, len(fv.Hashes))
for i, hash := range fv.Hashes { for i, hash := range fv.Hashes {
hashesData[i] = hash.FullBytes hashesData[i] = hash.FullBytes()
} }
fmap.Put(9, hashesData) fmap.Put(9, hashesData)
} }