gate.io: Enforce the use of 'convert.StringToFloat64' and permit its use in outbound requests (#1308)

Co-authored-by: Ryan O'Hara-Reid <ryan.oharareid@thrasher.io>
This commit is contained in:
Ryan O'Hara-Reid
2023-09-18 17:15:27 +10:00
committed by GitHub
parent 78fe6d5e79
commit 8b8d96c612
9 changed files with 1140 additions and 1100 deletions

View File

@@ -222,6 +222,15 @@ func (f *StringToFloat64) UnmarshalJSON(data []byte) error {
return nil
}
// MarshalJSON implements the json.Marshaler interface.
func (f StringToFloat64) MarshalJSON() ([]byte, error) {
if f == 0 {
return []byte(jsonStringIdent + jsonStringIdent), nil
}
val := strconv.FormatFloat(float64(f), 'f', -1, 64)
return []byte(jsonStringIdent + val + jsonStringIdent), nil
}
// Float64 returns the float64 value of the FloatString.
func (f *StringToFloat64) Float64() float64 {
return float64(*f)

View File

@@ -353,6 +353,24 @@ func TestStringToFloat64(t *testing.T) {
if err == nil {
t.Fatal("error cannot be nil")
}
data, err := json.Marshal(StringToFloat64(0))
if err != nil {
t.Fatal(err)
}
if string(data) != `""` {
t.Fatalf("expected empty string, got %v", string(data))
}
data, err = json.Marshal(StringToFloat64(1337.1337))
if err != nil {
t.Fatal(err)
}
if string(data) != `"1337.1337"` {
t.Fatalf("expected \"1337.1337\" string, got %v", string(data))
}
}
func TestStringToFloat64Decimal(t *testing.T) {