-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added comparison methods #1356
base: master
Are you sure you want to change the base?
Added comparison methods #1356
Conversation
func (e DataEntries) String() string { | ||
var resStr string | ||
for _, entry := range e { | ||
entryStr := entry.GetKey() + entry.GetValueType().String() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No divider between key and value?
} | ||
|
||
// SortDataEntries sorts DataEntries slice by entry keys. | ||
func SortDataEntries(entries []DataEntry) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this function can be package private. Or, it can be declared as a method of DataEntries
slice
@@ -3092,6 +3114,15 @@ func (e DataEntries) PayloadSize() int { | |||
return pl | |||
} | |||
|
|||
func (e DataEntries) String() string { | |||
var resStr string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's better to use stings.Builder
for concatenating many strings.
return true, nil | ||
} | ||
|
||
func SortAtomicSnapshotsByType(snapshots TxSnapshot) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this function can be package private. Or, it can be declared as a method of TxSnapshot
slice. And, is it really necessary to create function when the type implements sort.Interface
?
@@ -2303,6 +2304,27 @@ func NewDataEntryFromValueBytes(valueBytes []byte) (DataEntry, error) { | |||
return entry, nil | |||
} | |||
|
|||
func CompareDataEntry(a, b DataEntry) bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it necessary to create such function? i think you can use the same approach as for AtomicSnapshots
. Create Equal
method for each DataEntry
type and use the new method for comparison.
addr2, _ := proto.NewAddressFromString("3P9o3uwx3fWZz3b5aaaaaaaaaaFoPW6z7HB") | ||
assetID1 := crypto.MustDigestFromBase58("BrjV5AB5S7qN5tLQFbU5tpLj5qeozfVvPxEpDkmmhNP") | ||
assetID2 := crypto.MustDigestFromBase58("5Zv8JLH8TTvq9iCo6HtB2K7CGpTJt6JTj5yvXaDVrxEJ") | ||
publicKey1, _ := crypto.NewPublicKeyFromBase58("5TBjL2VdL1XmXq5dC4SYMeH5sVCGmMTeBNNYqWCuEXMn") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And here
if err != nil { | ||
t.Errorf("Error comparing snapshots: %v", err) | ||
} | ||
if equal != tt.wantEqual { | ||
t.Errorf("Expected snapshots to be equal: %v, got: %v", tt.wantEqual, equal) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use testify
package.
@@ -3138,6 +3169,30 @@ func (e *DataEntries) UnmarshalJSON(data []byte) error { | |||
return nil | |||
} | |||
|
|||
// Equal compares two DataEntries slices and returns true if they are equal. | |||
func (e DataEntries) Equal(other DataEntries) (bool, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's necessary to say that this function mutates passed argument, i.e. elements order in the slice.
@@ -121,6 +150,28 @@ type DataEntriesSnapshot struct { // AccountData in pb | |||
|
|||
func (s DataEntriesSnapshot) Apply(a SnapshotApplier) error { return a.ApplyDataEntries(s) } | |||
|
|||
func (s DataEntriesSnapshot) Equal(otherSnapshot AtomicSnapshot) (bool, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's necessary to say that this function mutates passed argument, i.e. elements order in theDataEntries
slice.
} | ||
|
||
// Equal function assumes that TransactionsSnapshots are in same order in both original and other instances. | ||
func (bs BlockSnapshot) Equal(other BlockSnapshot) (bool, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's necessary to say that this function can mutate the passed argument
No description provided.