-
Notifications
You must be signed in to change notification settings - Fork 0
/
friendly_response_content_builder.go
142 lines (116 loc) · 3.98 KB
/
friendly_response_content_builder.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package types
import (
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"regexp"
"strings"
)
type FriendlyResponseContentBuilderI interface {
WriteText(string) FriendlyResponseContentBuilderI
WriteAddress(string) FriendlyResponseContentBuilderI
WriteCoins(coins sdk.Coins, denomsMetadata map[string]banktypes.Metadata) FriendlyResponseContentBuilderI
Build() (friendlySimple string, friendlyMachine string)
BuildIntoResponse(res GenericBackendResponse)
}
var _ FriendlyResponseContentBuilderI = &friendlyResponseContentBuilder{}
type friendlyResponseContentBuilder struct {
// friendlySimple is the simple friendly response content, without ability to inject HTML code.
friendlySimple strings.Builder
// friendlyMachine is the friendly response content, with ability to inject HTML using pattern replacement.
friendlyMachine strings.Builder
}
func NewFriendlyResponseContentBuilder() FriendlyResponseContentBuilderI {
return &friendlyResponseContentBuilder{
friendlySimple: strings.Builder{},
friendlyMachine: strings.Builder{},
}
}
func (f *friendlyResponseContentBuilder) WriteText(s string) FriendlyResponseContentBuilderI {
return f.addBoth(s)
}
var regexAlphaNumericOnly = regexp.MustCompile(`^[a-zA-Z\d]+$`)
func (f *friendlyResponseContentBuilder) WriteAddress(a string) FriendlyResponseContentBuilderI {
f.friendlySimple.WriteString(a)
if regexAlphaNumericOnly.MatchString(a) { // only write pattern if content is sanitized
f.addMachinePattern("address", a)
} else {
f.friendlyMachine.WriteString(a)
}
return f
}
func (f *friendlyResponseContentBuilder) WriteCoins(coins sdk.Coins, denomsMetadata map[string]banktypes.Metadata) FriendlyResponseContentBuilderI {
for i, coin := range coins {
if i > 0 {
f.addBoth(", ")
}
metadata, found := denomsMetadata[coin.Denom]
if !found || !isTextSatisfyCoinDenom(coin.Denom) {
f.addBoth("(raw) ").addBoth(coin.String())
continue
}
display, highestExponent := getHighestExponent(metadata)
if highestExponent == 0 || display == coin.Denom {
f.addBoth("(raw) ").addBoth(coin.String())
continue
}
if coin.Amount.IsZero() {
f.addBoth("0 ").addBoth(display)
continue
}
f.addBoth(getDisplayNumber(coin.Amount, highestExponent)).addBoth(" ").addBoth(display)
}
return f
}
func (f *friendlyResponseContentBuilder) Build() (friendlySimple string, friendlyMachine string) {
friendlySimple = f.friendlySimple.String()
friendlyMachine = f.friendlyMachine.String()
return
}
func (f *friendlyResponseContentBuilder) BuildIntoResponse(res GenericBackendResponse) {
friendlySimple, friendlyMachine := f.Build()
res["cts"] = friendlySimple
res["ctm"] = friendlyMachine
}
func (f *friendlyResponseContentBuilder) addMachinePattern(_type, content string) {
f.friendlyMachine.WriteString("{[{ .[")
f.friendlyMachine.WriteString(_type)
f.friendlyMachine.WriteString("].[")
f.friendlyMachine.WriteString(content)
f.friendlyMachine.WriteString("]. }]}")
}
func (f *friendlyResponseContentBuilder) addBoth(s string) *friendlyResponseContentBuilder {
f.friendlySimple.WriteString(s)
f.friendlyMachine.WriteString(s)
return f
}
var regexCoinDenom = regexp.MustCompile(`^([a-zA-Z\d]+/)?[a-zA-Z\d]+$`)
func isTextSatisfyCoinDenom(s string) bool {
return regexCoinDenom.MatchString(s)
}
func getHighestExponent(denomMetadata banktypes.Metadata) (displayOfHighest string, highestExponent uint32) {
for _, denomUnit := range denomMetadata.DenomUnits {
if denomUnit.Exponent > highestExponent {
displayOfHighest = denomUnit.Denom
highestExponent = denomUnit.Exponent
}
}
return
}
func getDisplayNumber(num math.Int, exponent uint32) string {
if num.IsZero() {
return "0"
}
str := math.LegacyNewDecFromIntWithPrec(num, int64(exponent)).String()
spl := strings.Split(str, ".")
if len(spl) == 1 {
return str
}
for strings.HasSuffix(spl[1], "0") {
spl[1] = spl[1][:len(spl[1])-1]
}
if len(spl[1]) == 0 {
return spl[0]
}
return spl[0] + "." + spl[1]
}