mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-12-24 09:25:21 +00:00
6b21ad9922
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.
75 lines
1.8 KiB
Go
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
|
|
}
|