neo-go/pkg/smartcontract/binding/override.go
Anna Shaleva 6b21ad9922 *: replace interface{} with any keyword
Everywhere including examples, external interop APIs, bindings generators
code and in other valuable places. A couple of `interface{}` usages are
intentionally left in the CHANGELOG.md, documentation and tests.
2023-04-04 13:22:42 +03:00

75 lines
1.8 KiB
Go

package binding
import (
"strings"
)
// Override contains a package and a type to replace manifest method parameter type with.
type Override struct {
// Package contains a fully-qualified package name.
Package string
// TypeName contains type name together with a package alias.
TypeName string
}
// NewOverrideFromString parses s and returns method parameter type override spec.
func NewOverrideFromString(s string) Override {
var over Override
index := strings.LastIndexByte(s, '.')
if index == -1 {
over.TypeName = s
return over
}
// Arrays and maps can have fully-qualified types as elements.
last := strings.LastIndexAny(s, "]*")
isCompound := last != -1 && last < index
if isCompound {
over.Package = s[last+1 : index]
} else {
over.Package = s[:index]
}
switch over.Package {
case "iterator", "storage":
over.Package = "github.com/nspcc-dev/neo-go/pkg/interop/" + over.Package
case "ledger", "management":
over.Package = "github.com/nspcc-dev/neo-go/pkg/interop/native/" + over.Package
}
slashIndex := strings.LastIndexByte(s, '/')
if isCompound {
over.TypeName = s[:last+1] + s[slashIndex+1:]
} else {
over.TypeName = s[slashIndex+1:]
}
return over
}
// UnmarshalYAML implements the YAML Unmarshaler interface.
func (o *Override) UnmarshalYAML(unmarshal func(any) error) error {
var s string
err := unmarshal(&s)
if err != nil {
return err
}
*o = NewOverrideFromString(s)
return err
}
// MarshalYAML implements the YAML marshaler interface.
func (o Override) MarshalYAML() (any, error) {
if o.Package == "" {
return o.TypeName, nil
}
index := strings.LastIndexByte(o.TypeName, '.')
last := strings.LastIndexAny(o.TypeName, "]*")
if last == -1 {
return o.Package + o.TypeName[index:], nil
}
return o.TypeName[:last+1] + o.Package + o.TypeName[index:], nil
}