cosmossdk.io/client/v2@v2.0.0-beta.1/internal/util/util.go (about)

     1  package util
     2  
     3  import (
     4  	"regexp"
     5  	"runtime/debug"
     6  	"strings"
     7  
     8  	"google.golang.org/protobuf/reflect/protoreflect"
     9  	"google.golang.org/protobuf/reflect/protoregistry"
    10  	"google.golang.org/protobuf/types/dynamicpb"
    11  
    12  	"cosmossdk.io/client/v2/internal/strcase"
    13  )
    14  
    15  // get build info to verify later if comment is supported
    16  // this is a hack in because of the global api module package
    17  // later versions unsupported by the current version can be added
    18  var buildInfo, _ = debug.ReadBuildInfo()
    19  
    20  // DescriptorName returns the name of the descriptor in kebab case.
    21  func DescriptorKebabName(descriptor protoreflect.Descriptor) string {
    22  	return strcase.ToKebab(string(descriptor.Name()))
    23  }
    24  
    25  // DescriptorDocs returns the leading comments of the descriptor.
    26  // TODO this does not work, to fix.
    27  func DescriptorDocs(descriptor protoreflect.Descriptor) string {
    28  	return descriptor.ParentFile().SourceLocations().ByDescriptor(descriptor).LeadingComments
    29  }
    30  
    31  func ResolveMessageType(resolver protoregistry.MessageTypeResolver, descriptor protoreflect.MessageDescriptor) protoreflect.MessageType {
    32  	typ, err := resolver.FindMessageByName(descriptor.FullName())
    33  	if err == nil {
    34  		return typ
    35  	}
    36  
    37  	return dynamicpb.NewMessageType(descriptor)
    38  }
    39  
    40  // IsSupportedVersion is used to determine in which version of a module / sdk a rpc was introduced.
    41  // It returns false if the rpc has comment for an higher version than the current one.
    42  func IsSupportedVersion(input string) bool {
    43  	return isSupportedVersion(input, buildInfo)
    44  }
    45  
    46  // isSupportedVersion is used to determine in which version of a module / sdk a rpc was introduced.
    47  // It returns false if the rpc has comment for an higher version than the current one.
    48  // It takes a buildInfo as argument to be able to test it.
    49  func isSupportedVersion(input string, buildInfo *debug.BuildInfo) bool {
    50  	if input == "" || buildInfo == nil {
    51  		return true
    52  	}
    53  
    54  	moduleName, version := parseSinceComment(input)
    55  	for _, dep := range buildInfo.Deps {
    56  		if !strings.Contains(dep.Path, moduleName) {
    57  			continue
    58  		}
    59  
    60  		return version <= dep.Version
    61  	}
    62  
    63  	return true // if cannot find the module consider it's supported
    64  }
    65  
    66  var sinceCommentRegex = regexp.MustCompile(`\/\/\s*since: (\S+) (\S+)`)
    67  
    68  // parseSinceComment parses the `// Since: cosmos-sdk v0.xx` comment on rpc.
    69  func parseSinceComment(input string) (string, string) {
    70  	var (
    71  		moduleName string
    72  		version    string
    73  	)
    74  
    75  	input = strings.ToLower(input)
    76  	input = strings.ReplaceAll(input, "cosmos sdk", "cosmos-sdk")
    77  
    78  	matches := sinceCommentRegex.FindStringSubmatch(input)
    79  	if len(matches) >= 3 {
    80  		moduleName, version = matches[1], matches[2]
    81  
    82  		if !strings.HasPrefix(version, "v") {
    83  			version = "v" + version
    84  		}
    85  	}
    86  
    87  	return moduleName, version
    88  }