cosmos-sdk/x/ibc/24-host/parse.go
Federico Kunze 3368dae5f5
x/ibc: final gRPC migration (#7077)
* x/ibc: final gRPC migration

* grpc test

* query client consensus states

* remove dup type

* query constructors

* CLI queries

* missing cmd

* latest consensus state

* parser test

* register query service

* Update x/ibc/02-client/client/cli/query.go

Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com>

* Update x/ibc/module.go

Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com>

* few grpc tests

* fix tests

* final gRPC tests

* rm comment

* address @colin-axner comment

Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com>
2020-08-20 10:23:19 -04:00

64 lines
1.7 KiB
Go

package host
import (
"strings"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
// RemovePath is an util function to remove a path from a set.
func RemovePath(paths []string, path string) ([]string, bool) {
for i, p := range paths {
if p == path {
return append(paths[:i], paths[i+1:]...), true
}
}
return paths, false
}
// ParseConnectionPath returns the connection ID from a full path. It returns
// an error if the provided path is invalid.
func ParseConnectionPath(path string) (string, error) {
split := strings.Split(path, "/")
if len(split) != 2 {
return "", sdkerrors.Wrapf(ErrInvalidPath, "cannot parse connection path %s", path)
}
return split[1], nil
}
// ParseChannelPath returns the port and channel ID from a full path. It returns
// an error if the provided path is invalid.
func ParseChannelPath(path string) (string, string, error) {
split := strings.Split(path, "/")
if len(split) < 5 {
return "", "", sdkerrors.Wrapf(ErrInvalidPath, "cannot parse channel path %s", path)
}
if split[1] != "ports" || split[3] != "channels" {
return "", "", sdkerrors.Wrapf(ErrInvalidPath, "cannot parse channel path %s", path)
}
return split[2], split[4], nil
}
// MustParseConnectionPath returns the connection ID from a full path. Panics
// if the provided path is invalid.
func MustParseConnectionPath(path string) string {
connectionID, err := ParseConnectionPath(path)
if err != nil {
panic(err)
}
return connectionID
}
// MustParseChannelPath returns the port and channel ID from a full path. Panics
// if the provided path is invalid.
func MustParseChannelPath(path string) (string, string) {
portID, channelID, err := ParseChannelPath(path)
if err != nil {
panic(err)
}
return portID, channelID
}