-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnormalize.go
122 lines (97 loc) · 3.29 KB
/
normalize.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package utils
import (
"regexp"
"strings"
)
// NormalizeType provides methods for normalizing type names.
type NormalizeType struct{}
// NormalizationInfo represents the result of a type normalization.
// It includes the normalized type name and a flag indicating whether
// the type name was actually normalized.
type NormalizationInfo struct {
TypeName string
Normalized bool
}
// NewNormalizeType creates and returns a new NormalizeType instance.
func NewNormalizeType() *NormalizeType {
return &NormalizeType{}
}
// Normalize attempts to normalize the given type name.
// It returns the NormalizationInfo which contains the normalized type name and a flag indicating
// if the provided type name was actually normalized.
func (n *NormalizeType) Normalize(typeName string) NormalizationInfo {
normalizedTypeName, isNormalized := n.normalizeTypeNameWithStatus(typeName)
return NormalizationInfo{
TypeName: normalizedTypeName,
Normalized: isNormalized,
}
}
// isBuiltInType checks if the provided type name is one of the recognized built-in types.
func (n *NormalizeType) isBuiltInType(typeName string) bool {
cases := []string{
"uint", "int", "bool", "bytes", "string", "address", "addresspayable", "tuple", "enum",
}
for _, bcase := range cases {
if strings.Contains(typeName, bcase) {
return true
}
}
return false
}
// normalizeTypeNameWithStatus attempts to normalize the provided type name.
// It returns the normalized type name and a flag indicating if it was normalized.
func (n *NormalizeType) normalizeTypeNameWithStatus(typeName string) (string, bool) {
isArray, _ := regexp.MatchString(`\[\d+\]`, typeName)
isSlice := strings.HasPrefix(typeName, "[]")
isSliceRight := strings.HasSuffix(typeName, "[]")
if isArray || isSlice || isSliceRight {
if !n.isBuiltInType(typeName) {
return typeName, false
}
switch {
case isArray:
numberPart := typeName[strings.Index(typeName, "[")+1 : strings.Index(typeName, "]")]
typePart := typeName[strings.Index(typeName, "]")+1:]
return "[" + numberPart + "]" + n.normalizeTypeName(typePart), true
case isSlice:
typePart := typeName[2:]
return "[]" + n.normalizeTypeName(typePart), true
case isSliceRight:
typePart := typeName[:len(typeName)-2]
return n.normalizeTypeName(typePart) + "[]", true
}
}
switch {
case strings.HasPrefix(typeName, "uint"):
if typeName == "uint" {
return "uint256", true
}
return typeName, true
case strings.HasPrefix(typeName, "int"):
if typeName == "int" {
return "int256", true
}
return typeName, true
case strings.HasPrefix(typeName, "enum"):
return "uint8", true
case strings.HasPrefix(typeName, "bool"):
return typeName, true
case strings.HasPrefix(typeName, "bytes"):
return typeName, true
case typeName == "string":
return "string", true
case typeName == "address":
return "address", true
case typeName == "addresspayable":
return "address", true
case typeName == "tuple":
return "tuple", true
default:
return typeName, false // Custom struct types or unrecognized types are not considered normalized
}
}
// normalizeTypeName provides the normalized version of the provided type name.
func (n *NormalizeType) normalizeTypeName(typeName string) string {
normalizedTypeName, _ := n.normalizeTypeNameWithStatus(typeName)
return normalizedTypeName
}