(Engine) Variety of engine updates (#390)

* drop common uuid v4 func and imported package as needed

* removed common functions regarding json marshal and unmarshal and used the json package directly. WRT unmarshal it was calling reflect and converted to string which is also checked in the JSON package so it was doing a double up, this will be a tiny gain as it was directly used in the requester package for all our outbound requests.

* add in string

* explicitly throw away return error value

* atleast return the error that websocket initialise returns

* return error when not connected

* fix comment

* Adds comments

* move package declarations

* drop append whenever we call supported

* remove unused import

* Change incorrect spelling

* fix tests

* fix go import issue
This commit is contained in:
Ryan O'Hara-Reid
2019-12-03 10:06:08 +11:00
committed by Adrian Gallagher
parent c27b8657e2
commit 0c5d75b22c
70 changed files with 393 additions and 462 deletions

View File

@@ -1,6 +1,7 @@
package currency
import (
"encoding/json"
"errors"
"fmt"
"strings"
@@ -11,7 +12,7 @@ import (
func (r Role) String() string {
switch r {
case Unset:
return UnsetRollString
return UnsetRoleString
case Fiat:
return FiatCurrencyString
case Cryptocurrency:
@@ -25,21 +26,21 @@ func (r Role) String() string {
}
}
// MarshalJSON conforms Roll to the marshaller interface
// MarshalJSON conforms Role to the marshaller interface
func (r Role) MarshalJSON() ([]byte, error) {
return common.JSONEncode(r.String())
return json.Marshal(r.String())
}
// UnmarshalJSON conforms Roll to the unmarshaller interface
// UnmarshalJSON conforms Role to the unmarshaller interface
func (r *Role) UnmarshalJSON(d []byte) error {
var incoming string
err := common.JSONDecode(d, &incoming)
err := json.Unmarshal(d, &incoming)
if err != nil {
return err
}
switch incoming {
case UnsetRollString:
case UnsetRoleString:
*r = Unset
case FiatCurrencyString:
*r = Fiat
@@ -400,7 +401,7 @@ func (c Code) Upper() Code {
// UnmarshalJSON comforms type to the umarshaler interface
func (c *Code) UnmarshalJSON(d []byte) error {
var newcode string
err := common.JSONDecode(d, &newcode)
err := json.Unmarshal(d, &newcode)
if err != nil {
return err
}
@@ -411,9 +412,9 @@ func (c *Code) UnmarshalJSON(d []byte) error {
// MarshalJSON conforms type to the marshaler interface
func (c Code) MarshalJSON() ([]byte, error) {
if c.Item == nil {
return common.JSONEncode("")
return json.Marshal("")
}
return common.JSONEncode(c.String())
return json.Marshal(c.String())
}
// IsEmpty returns true if the code is empty

View File

@@ -1,15 +1,14 @@
package currency
import (
"encoding/json"
"testing"
"github.com/thrasher-corp/gocryptotrader/common"
)
func TestRoleString(t *testing.T) {
if Unset.String() != UnsetRollString {
if Unset.String() != UnsetRoleString {
t.Errorf("Role String() error expected %s but received %s",
UnsetRollString,
UnsetRoleString,
Unset)
}
@@ -47,7 +46,7 @@ func TestRoleString(t *testing.T) {
}
func TestRoleMarshalJSON(t *testing.T) {
d, err := common.JSONEncode(Fiat)
d, err := json.Marshal(Fiat)
if err != nil {
t.Error("Role MarshalJSON() error", err)
}
@@ -79,23 +78,23 @@ func TestRoleUnmarshalJSON(t *testing.T) {
RoleFive: Contract,
}
e, err := common.JSONEncode(1337)
e, err := json.Marshal(1337)
if err != nil {
t.Fatal("Role UnmarshalJSON() error", err)
}
var incoming AllTheRoles
err = common.JSONDecode(e, &incoming)
err = json.Unmarshal(e, &incoming)
if err == nil {
t.Fatal("Role UnmarshalJSON() Expected error")
}
e, err = common.JSONEncode(outgoing)
e, err = json.Marshal(outgoing)
if err != nil {
t.Fatal("Role UnmarshalJSON() error", err)
}
err = common.JSONDecode(e, &incoming)
err = json.Unmarshal(e, &incoming)
if err != nil {
t.Fatal("Role UnmarshalJSON() error", err)
}
@@ -365,17 +364,17 @@ func TestCodeUpper(t *testing.T) {
func TestCodeUnmarshalJSON(t *testing.T) {
var unmarshalHere Code
expected := "BRO"
encoded, err := common.JSONEncode(expected)
encoded, err := json.Marshal(expected)
if err != nil {
t.Fatal("Currency Code UnmarshalJSON error", err)
}
err = common.JSONDecode(encoded, &unmarshalHere)
err = json.Unmarshal(encoded, &unmarshalHere)
if err != nil {
t.Fatal("Currency Code UnmarshalJSON error", err)
}
err = common.JSONDecode(encoded, &unmarshalHere)
err = json.Unmarshal(encoded, &unmarshalHere)
if err != nil {
t.Fatal("Currency Code UnmarshalJSON error", err)
}
@@ -396,7 +395,7 @@ func TestCodeMarshalJSON(t *testing.T) {
expectedJSON := `{"sweetCodes":"BRO"}`
encoded, err := common.JSONEncode(quickstruct)
encoded, err := json.Marshal(quickstruct)
if err != nil {
t.Fatal("Currency Code UnmarshalJSON error", err)
}
@@ -413,7 +412,7 @@ func TestCodeMarshalJSON(t *testing.T) {
Codey: Code{}, // nil code
}
encoded, err = common.JSONEncode(quickstruct)
encoded, err = json.Marshal(quickstruct)
if err != nil {
t.Fatal("Currency Code UnmarshalJSON error", err)
}

View File

@@ -5,7 +5,7 @@ import (
"time"
)
// Bitmasks const for currency rolls
// Bitmasks const for currency roles
const (
Unset Role = 0
Fiat Role = 1 << (iota - 1)
@@ -13,14 +13,14 @@ const (
Token
Contract
UnsetRollString = "roleUnset"
UnsetRoleString = "roleUnset"
FiatCurrencyString = "fiatCurrency"
CryptocurrencyString = "cryptocurrency"
TokenString = "token"
ContractString = "contract"
)
// Role defines a bitmask for the full currency rolls either; fiat,
// Role defines a bitmask for the full currency roles either; fiat,
// cryptocurrency, token, or contract
type Role uint8

View File

@@ -1,9 +1,8 @@
package currency
import (
"encoding/json"
"strings"
"github.com/thrasher-corp/gocryptotrader/common"
)
// NewCurrenciesFromStringArray returns a Currencies object from strings
@@ -48,7 +47,7 @@ func (c Currencies) Join() string {
// UnmarshalJSON comforms type to the umarshaler interface
func (c *Currencies) UnmarshalJSON(d []byte) error {
var configCurrencies string
err := common.JSONDecode(d, &configCurrencies)
err := json.Unmarshal(d, &configCurrencies)
if err != nil {
return err
}
@@ -64,7 +63,7 @@ func (c *Currencies) UnmarshalJSON(d []byte) error {
// MarshalJSON conforms type to the marshaler interface
func (c Currencies) MarshalJSON() ([]byte, error) {
return common.JSONEncode(c.Join())
return json.Marshal(c.Join())
}
// Match returns if the full list equals the supplied list

View File

@@ -1,25 +1,24 @@
package currency
import (
"encoding/json"
"testing"
"github.com/thrasher-corp/gocryptotrader/common"
)
func TestCurrenciesUnmarshalJSON(t *testing.T) {
var unmarshalHere Currencies
expected := "btc,usd,ltc,bro,things"
encoded, err := common.JSONEncode(expected)
encoded, err := json.Marshal(expected)
if err != nil {
t.Fatal("Currencies UnmarshalJSON() error", err)
}
err = common.JSONDecode(encoded, &unmarshalHere)
err = json.Unmarshal(encoded, &unmarshalHere)
if err != nil {
t.Fatal("Currencies UnmarshalJSON() error", err)
}
err = common.JSONDecode(encoded, &unmarshalHere)
err = json.Unmarshal(encoded, &unmarshalHere)
if err != nil {
t.Fatal("Currencies UnmarshalJSON() error", err)
}
@@ -37,7 +36,7 @@ func TestCurrenciesMarshalJSON(t *testing.T) {
C: NewCurrenciesFromStringArray([]string{"btc", "usd", "ltc", "bro", "things"}),
}
encoded, err := common.JSONEncode(quickStruct)
encoded, err := json.Marshal(quickStruct)
if err != nil {
t.Fatal("Currencies MarshalJSON() error", err)
}

View File

@@ -1,10 +1,9 @@
package currency
import (
"encoding/json"
"fmt"
"strings"
"github.com/thrasher-corp/gocryptotrader/common"
)
// NewPairDelimiter splits the desired currency string at delimeter, the returns
@@ -113,7 +112,7 @@ func (p Pair) Upper() Pair {
// UnmarshalJSON comforms type to the umarshaler interface
func (p *Pair) UnmarshalJSON(d []byte) error {
var pair string
err := common.JSONDecode(d, &pair)
err := json.Unmarshal(d, &pair)
if err != nil {
return err
}
@@ -124,7 +123,7 @@ func (p *Pair) UnmarshalJSON(d []byte) error {
// MarshalJSON conforms type to the marshaler interface
func (p Pair) MarshalJSON() ([]byte, error) {
return common.JSONEncode(p.String())
return json.Marshal(p.String())
}
// Format changes the currency based on user preferences overriding the default

View File

@@ -1,9 +1,8 @@
package currency
import (
"encoding/json"
"testing"
"github.com/thrasher-corp/gocryptotrader/common"
)
const (
@@ -37,17 +36,17 @@ func TestPairUnmarshalJSON(t *testing.T) {
var unmarshalHere Pair
configPair := NewPairDelimiter("btc_usd", "_")
encoded, err := common.JSONEncode(configPair)
encoded, err := json.Marshal(configPair)
if err != nil {
t.Fatal("Pair UnmarshalJSON() error", err)
}
err = common.JSONDecode(encoded, &unmarshalHere)
err = json.Unmarshal(encoded, &unmarshalHere)
if err != nil {
t.Fatal("Pair UnmarshalJSON() error", err)
}
err = common.JSONDecode(encoded, &unmarshalHere)
err = json.Unmarshal(encoded, &unmarshalHere)
if err != nil {
t.Fatal("Pair UnmarshalJSON() error", err)
}
@@ -65,7 +64,7 @@ func TestPairMarshalJSON(t *testing.T) {
Pair{Base: BTC, Quote: USD, Delimiter: "-"},
}
encoded, err := common.JSONEncode(quickstruct)
encoded, err := json.Marshal(quickstruct)
if err != nil {
t.Fatal("Pair MarshalJSON() error", err)
}

View File

@@ -1,10 +1,10 @@
package currency
import (
"encoding/json"
"math/rand"
"strings"
"github.com/thrasher-corp/gocryptotrader/common"
log "github.com/thrasher-corp/gocryptotrader/logger"
)
@@ -68,7 +68,7 @@ func (p Pairs) Format(delimiter, index string, uppercase bool) Pairs {
// UnmarshalJSON comforms type to the umarshaler interface
func (p *Pairs) UnmarshalJSON(d []byte) error {
var pairs string
err := common.JSONDecode(d, &pairs)
err := json.Unmarshal(d, &pairs)
if err != nil {
return err
}
@@ -89,7 +89,7 @@ func (p *Pairs) UnmarshalJSON(d []byte) error {
// MarshalJSON conforms type to the marshaler interface
func (p Pairs) MarshalJSON() ([]byte, error) {
return common.JSONEncode(p.Join())
return json.Marshal(p.Join())
}
// Upper returns an upper formatted pair list

View File

@@ -1,9 +1,8 @@
package currency
import (
"encoding/json"
"testing"
"github.com/thrasher-corp/gocryptotrader/common"
)
func TestPairsUpper(t *testing.T) {
@@ -69,33 +68,33 @@ func TestPairsFormat(t *testing.T) {
func TestPairsUnmarshalJSON(t *testing.T) {
var unmarshalHere Pairs
configPairs := ""
encoded, err := common.JSONEncode(configPairs)
encoded, err := json.Marshal(configPairs)
if err != nil {
t.Fatal("Pairs UnmarshalJSON() error", err)
}
err = common.JSONDecode([]byte{1, 3, 3, 7}, &unmarshalHere)
err = json.Unmarshal([]byte{1, 3, 3, 7}, &unmarshalHere)
if err == nil {
t.Fatal("error cannot be nil")
}
err = common.JSONDecode(encoded, &unmarshalHere)
err = json.Unmarshal(encoded, &unmarshalHere)
if err != nil {
t.Fatal("Pairs UnmarshalJSON() error", err)
}
configPairs = "btc_usd,btc_aud,btc_ltc"
encoded, err = common.JSONEncode(configPairs)
encoded, err = json.Marshal(configPairs)
if err != nil {
t.Fatal("Pairs UnmarshalJSON() error", err)
}
err = common.JSONDecode(encoded, &unmarshalHere)
err = json.Unmarshal(encoded, &unmarshalHere)
if err != nil {
t.Fatal("Pairs UnmarshalJSON() error", err)
}
err = common.JSONDecode(encoded, &unmarshalHere)
err = json.Unmarshal(encoded, &unmarshalHere)
if err != nil {
t.Fatal("Pairs UnmarshalJSON() error", err)
}
@@ -113,7 +112,7 @@ func TestPairsMarshalJSON(t *testing.T) {
Pairs: NewPairsFromStrings([]string{"btc_usd", "btc_aud", "btc_ltc"}),
}
encoded, err := common.JSONEncode(quickstruct)
encoded, err := json.Marshal(quickstruct)
if err != nil {
t.Fatal("Pairs MarshalJSON() error", err)
}

View File

@@ -8,7 +8,6 @@ import (
"path/filepath"
"time"
"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/common/file"
"github.com/thrasher-corp/gocryptotrader/currency/coinmarketcap"
"github.com/thrasher-corp/gocryptotrader/currency/forexprovider"
@@ -264,7 +263,7 @@ func (s *Storage) SeedCurrencyAnalysisData() error {
}
var fromFile File
err = common.JSONDecode(b, &fromFile)
err = json.Unmarshal(b, &fromFile)
if err != nil {
return err
}
@@ -364,7 +363,7 @@ func (s *Storage) LoadFileCurrencyData(f *File) error {
return nil
}
// UpdateCurrencies updates currency roll and information using coin market cap
// UpdateCurrencies updates currency role and information using coin market cap
func (s *Storage) UpdateCurrencies() error {
m, err := s.currencyAnalysis.GetCryptocurrencyIDMap()
if err != nil {