• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

unpackdev / solgo / 6576924099

19 Oct 2023 03:34PM UTC coverage: 78.98% (-0.05%) from 79.029%
6576924099

push

github

web-flow
Numerous updates/fixes including introduction of normalization utility (#131)

139 of 281 new or added lines in 15 files covered. (49.47%)

12 existing lines in 4 files now uncovered.

15770 of 19967 relevant lines covered (78.98%)

0.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

40.28
/utils/normalize.go
1
package utils
2

3
import (
4
        "regexp"
5
        "strings"
6
)
7

8
// NormalizeType provides methods for normalizing type names.
9
type NormalizeType struct{}
10

11
// NormalizationInfo represents the result of a type normalization.
12
// It includes the normalized type name and a flag indicating whether
13
// the type name was actually normalized.
14
type NormalizationInfo struct {
15
        TypeName   string
16
        Normalized bool
17
}
18

19
// NewNormalizeType creates and returns a new NormalizeType instance.
20
func NewNormalizeType() *NormalizeType {
1✔
21
        return &NormalizeType{}
1✔
22
}
1✔
23

24
// Normalize attempts to normalize the given type name.
25
// It returns the NormalizationInfo which contains the normalized type name and a flag indicating
26
// if the provided type name was actually normalized.
27
func (n *NormalizeType) Normalize(typeName string) NormalizationInfo {
1✔
28
        normalizedTypeName, isNormalized := n.normalizeTypeNameWithStatus(typeName)
1✔
29
        return NormalizationInfo{
1✔
30
                TypeName:   normalizedTypeName,
1✔
31
                Normalized: isNormalized,
1✔
32
        }
1✔
33
}
1✔
34

35
// isBuiltInType checks if the provided type name is one of the recognized built-in types.
NEW
36
func (n *NormalizeType) isBuiltInType(typeName string) bool {
×
NEW
37
        cases := []string{
×
NEW
38
                "uint", "int", "bool", "bytes", "string", "address", "addresspayable", "tuple", "enum",
×
NEW
39
        }
×
NEW
40

×
NEW
41
        for _, bcase := range cases {
×
NEW
42
                if strings.Contains(typeName, bcase) {
×
NEW
43
                        return true
×
NEW
44
                }
×
45
        }
46

NEW
47
        return false
×
48
}
49

50
// normalizeTypeNameWithStatus attempts to normalize the provided type name.
51
// It returns the normalized type name and a flag indicating if it was normalized.
52
func (n *NormalizeType) normalizeTypeNameWithStatus(typeName string) (string, bool) {
1✔
53
        isArray, _ := regexp.MatchString(`\[\d+\]`, typeName)
1✔
54
        isSlice := strings.HasPrefix(typeName, "[]")
1✔
55
        isSliceRight := strings.HasSuffix(typeName, "[]")
1✔
56

1✔
57
        if isArray || isSlice || isSliceRight {
1✔
NEW
58
                if !n.isBuiltInType(typeName) {
×
NEW
59
                        return typeName, false
×
NEW
60
                }
×
61

NEW
62
                switch {
×
NEW
63
                case isArray:
×
NEW
64
                        numberPart := typeName[strings.Index(typeName, "[")+1 : strings.Index(typeName, "]")]
×
NEW
65
                        typePart := typeName[strings.Index(typeName, "]")+1:]
×
NEW
66

×
NEW
67
                        return "[" + numberPart + "]" + n.normalizeTypeName(typePart), true
×
68

NEW
69
                case isSlice:
×
NEW
70
                        typePart := typeName[2:]
×
NEW
71
                        return "[]" + n.normalizeTypeName(typePart), true
×
72

NEW
73
                case isSliceRight:
×
NEW
74
                        typePart := typeName[:len(typeName)-2]
×
NEW
75
                        return n.normalizeTypeName(typePart) + "[]", true
×
76
                }
77
        }
78

79
        switch {
1✔
80
        case strings.HasPrefix(typeName, "uint"):
1✔
81
                if typeName == "uint" {
2✔
82
                        return "uint256", true
1✔
83
                }
1✔
NEW
84
                return typeName, true
×
85

86
        case strings.HasPrefix(typeName, "int"):
1✔
87
                if typeName == "int" {
2✔
88
                        return "int256", true
1✔
89
                }
1✔
NEW
90
                return typeName, true
×
91

92
        case strings.HasPrefix(typeName, "enum"):
1✔
93
                return "uint8", true
1✔
94

NEW
95
        case strings.HasPrefix(typeName, "bool"):
×
NEW
96
                return typeName, true
×
97

NEW
98
        case strings.HasPrefix(typeName, "bytes"):
×
NEW
99
                return typeName, true
×
100

NEW
101
        case typeName == "string":
×
NEW
102
                return "string", true
×
103

NEW
104
        case typeName == "address":
×
NEW
105
                return "address", true
×
106

NEW
107
        case typeName == "addresspayable":
×
NEW
108
                return "address", true
×
109

NEW
110
        case typeName == "tuple":
×
NEW
111
                return "tuple", true
×
112

113
        default:
1✔
114
                return typeName, false // Custom struct types or unrecognized types are not considered normalized
1✔
115
        }
116
}
117

118
// normalizeTypeName provides the normalized version of the provided type name.
NEW
119
func (n *NormalizeType) normalizeTypeName(typeName string) string {
×
NEW
120
        normalizedTypeName, _ := n.normalizeTypeNameWithStatus(typeName)
×
NEW
121
        return normalizedTypeName
×
NEW
122
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc