-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
339 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package bitfinex | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type ( | ||
JSONFloat64 float64 | ||
JSONInt64 int64 | ||
JSONTime time.Time | ||
) | ||
|
||
func (t *JSONFloat64) Float64() float64 { return float64(*t) } | ||
|
||
func (t *JSONFloat64) UnmarshalJSON(s []byte) (err error) { | ||
r := strings.Replace(string(s), `"`, ``, -1) | ||
if r == "" { | ||
return | ||
} | ||
|
||
q, err := strconv.ParseFloat(r, 64) | ||
if err != nil { | ||
return err | ||
} | ||
*(*float64)(t) = q | ||
return | ||
} | ||
|
||
func (t *JSONInt64) Int64() int64 { return int64(*t) } | ||
|
||
func (t *JSONInt64) UnmarshalJSON(s []byte) (err error) { | ||
r := strings.Replace(string(s), `"`, ``, -1) | ||
if r == "" { | ||
return | ||
} | ||
|
||
q, err := strconv.ParseInt(r, 10, 64) | ||
if err != nil { | ||
return err | ||
} | ||
*(*int64)(t) = q | ||
return | ||
} | ||
|
||
func (t *JSONTime) Time() time.Time { return time.Time(*t) } | ||
|
||
func (t *JSONTime) UnmarshalJSON(s []byte) (err error) { | ||
r := strings.Replace(string(s), `"`, ``, -1) | ||
if r == "" { | ||
return | ||
} | ||
|
||
q, err := strconv.ParseInt(r, 10, 64) | ||
if err != nil { | ||
return err | ||
} | ||
*(*time.Time)(t) = time.UnixMilli(q) | ||
return | ||
} | ||
|
||
func (t *JSONTime) MarshalJSON() ([]byte, error) { | ||
return []byte(strconv.FormatInt(time.Time(*t).UnixMilli(), 10)), nil | ||
} | ||
|
||
func (t *JSONTime) String() string { return (time.Time)(*t).String() } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
package bitfinex | ||
|
||
import ( | ||
"strconv" | ||
"testing" | ||
"time" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
func TestJSONFloat64(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
in string | ||
want float64 | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "zero", | ||
in: "0", | ||
want: 0, | ||
}, | ||
{ | ||
name: "negative", | ||
in: "-1.0", | ||
want: -1.0, | ||
}, | ||
{ | ||
name: "empty", | ||
in: "", | ||
want: 0, | ||
}, | ||
{ | ||
name: "float", | ||
in: "0.10203101249012", | ||
want: 0.10203101249012, | ||
}, | ||
{ | ||
name: "invalid", | ||
in: "invalid", | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
var f JSONFloat64 | ||
err := f.UnmarshalJSON([]byte(test.in)) | ||
if test.wantErr { | ||
if err == nil { | ||
t.Fatal("expected error") | ||
} | ||
return | ||
} | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if f.Float64() != test.want { | ||
t.Errorf("expected %f, got %f", test.want, f.Float64()) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestJSONInt64(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
in string | ||
want int64 | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "zero", | ||
in: "0", | ||
want: 0, | ||
}, | ||
{ | ||
name: "negative", | ||
in: "-1", | ||
want: -1, | ||
}, | ||
{ | ||
name: "empty", | ||
in: "", | ||
want: 0, | ||
}, | ||
{ | ||
name: "int", | ||
in: "123", | ||
want: 123, | ||
}, | ||
{ | ||
name: "invalid", | ||
in: "invalid", | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
var f JSONInt64 | ||
err := f.UnmarshalJSON([]byte(test.in)) | ||
if test.wantErr { | ||
if err == nil { | ||
t.Fatal("expected error") | ||
} | ||
return | ||
} | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if f.Int64() != test.want { | ||
t.Errorf("expected %d, got %d", test.want, f.Int64()) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestJSONTime(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
in string | ||
want time.Time | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "zero", | ||
in: "0", | ||
want: time.UnixMilli(0), | ||
}, | ||
{ | ||
name: "one", | ||
in: "1", | ||
want: time.UnixMilli(1), | ||
}, | ||
{ | ||
name: "empty", | ||
in: "", | ||
want: time.Time{}, | ||
}, | ||
{ | ||
name: "time", | ||
in: "1694061154503", | ||
want: time.UnixMilli(1694061154503), | ||
}, | ||
{ | ||
name: "invalid", | ||
in: "invalid", | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
var f JSONTime | ||
err := f.UnmarshalJSON([]byte(test.in)) | ||
if test.wantErr { | ||
if err == nil { | ||
t.Fatal("expected error") | ||
} | ||
return | ||
} | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if diff := cmp.Diff(f.Time(), test.want); diff != "" { | ||
t.Errorf("unexpected time (-got +want): %s", diff) | ||
} | ||
{ | ||
got, err := f.MarshalJSON() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if diff := cmp.Diff(string(got), strconv.FormatInt(test.want.UnixMilli(), 10)); diff != "" { | ||
t.Errorf("unexpected time (-got +want): %s", diff) | ||
} | ||
} | ||
if diff := cmp.Diff(f.String(), test.want.String()); diff != "" { | ||
t.Errorf("unexpected time (-got +want): %s", diff) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.