/
move.go
64 lines (57 loc) · 1.47 KB
/
move.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package types
import (
"errors"
"fmt"
"github.com/coming-chat/go-sui/sui_types"
"strings"
)
type ResourceType struct {
Address *sui_types.SuiAddress
ModuleName string
FuncName string
SubType *ResourceType
}
func NewResourceType(str string) (*ResourceType, error) {
ltIdx := strings.Index(str, "<")
var subType *ResourceType
var err error
if ltIdx != -1 {
gtIdx := strings.LastIndex(str, ">")
if gtIdx != len(str)-1 {
return nil, errors.New("invalid type string literal")
}
subType, err = NewResourceType(str[ltIdx+1 : gtIdx])
if err != nil {
return nil, err
}
str = str[:ltIdx]
}
parts := strings.Split(str, "::")
if len(parts) != 3 {
return nil, errors.New("invalid type string literal")
}
addr, err := sui_types.NewAddressFromHex(parts[0])
if err != nil {
return nil, err
}
return &ResourceType{
Address: addr,
ModuleName: parts[1],
FuncName: parts[2],
SubType: subType,
}, nil
}
func (t *ResourceType) String() string {
if t.SubType != nil {
return fmt.Sprintf("%v::%v::%v<%v>", t.Address.String(), t.ModuleName, t.FuncName, t.SubType.String())
} else {
return fmt.Sprintf("%v::%v::%v", t.Address.String(), t.ModuleName, t.FuncName)
}
}
func (t *ResourceType) ShortString() string {
if t.SubType != nil {
return fmt.Sprintf("%v::%v::%v<%v>", t.Address.ShortString(), t.ModuleName, t.FuncName, t.SubType.ShortString())
} else {
return fmt.Sprintf("%v::%v::%v", t.Address.ShortString(), t.ModuleName, t.FuncName)
}
}