-
Notifications
You must be signed in to change notification settings - Fork 608
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* adding MetadataFromVersion func to pkg types * adding additional error context to MetadataFromVersion * order imports
- Loading branch information
1 parent
5c17048
commit 31b3c60
Showing
2 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package types | ||
|
||
import errorsmod "cosmossdk.io/errors" | ||
|
||
// MetadataFromVersion attempts to parse the given string into a fee version Metadata, | ||
// an error is returned if it fails to do so. | ||
func MetadataFromVersion(version string) (Metadata, error) { | ||
var metadata Metadata | ||
err := ModuleCdc.UnmarshalJSON([]byte(version), &metadata) | ||
if err != nil { | ||
return Metadata{}, errorsmod.Wrapf(ErrInvalidVersion, "failed to unmarshal metadata from version: %s", version) | ||
} | ||
|
||
return metadata, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package types_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/cosmos/ibc-go/v7/modules/apps/29-fee/types" | ||
ibcmock "github.com/cosmos/ibc-go/v7/testing/mock" | ||
) | ||
|
||
func TestMetadataFromVersion(t *testing.T) { | ||
testMetadata := types.Metadata{ | ||
AppVersion: ibcmock.Version, | ||
FeeVersion: types.Version, | ||
} | ||
|
||
versionBz, err := types.ModuleCdc.MarshalJSON(&testMetadata) | ||
require.NoError(t, err) | ||
|
||
metadata, err := types.MetadataFromVersion(string(versionBz)) | ||
require.NoError(t, err) | ||
require.Equal(t, ibcmock.Version, metadata.AppVersion) | ||
require.Equal(t, types.Version, metadata.FeeVersion) | ||
|
||
metadata, err = types.MetadataFromVersion("") | ||
require.Error(t, err) | ||
require.ErrorIs(t, err, types.ErrInvalidVersion) | ||
require.Empty(t, metadata) | ||
} |