Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Engine) Variety of engine updates #390

Merged
merged 14 commits into from
Dec 2, 2019
3 changes: 1 addition & 2 deletions cmd/exchange_wrapper_issues/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"sync"
"text/template"

"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/common/file"
"github.com/thrasher-corp/gocryptotrader/config"
"github.com/thrasher-corp/gocryptotrader/currency"
Expand Down Expand Up @@ -737,7 +736,7 @@ func loadConfig() (Config, error) {
return config, err
}

err = common.JSONDecode(keys, &config)
err = json.Unmarshal(keys, &config)
return config, err
}

Expand Down
5 changes: 3 additions & 2 deletions cmd/websocket_client/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"encoding/json"
"errors"
"fmt"
"log"
Expand Down Expand Up @@ -113,13 +114,13 @@ func main() {
}
log.Printf("Fetched config.")

dataJSON, err := common.JSONEncode(&wsResp.Data)
dataJSON, err := json.Marshal(&wsResp.Data)
if err != nil {
log.Fatal(err)
}

var resultCfg config.Config
err = common.JSONDecode(dataJSON, &resultCfg)
err = json.Unmarshal(dataJSON, &resultCfg)
if err != nil {
log.Fatal(err)
}
Expand Down
22 changes: 1 addition & 21 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@ import (
"os"
"os/user"
"path/filepath"
"reflect"
"regexp"
"strconv"
"strings"
"time"

"github.com/gofrs/uuid"
log "github.com/thrasher-corp/gocryptotrader/logger"
)

Expand All @@ -43,11 +41,6 @@ const (
WeiPerEther = 1000000000000000000
)

// GetV4UUID returns a RFC 4122 UUID based on random numbers
func GetV4UUID() (uuid.UUID, error) {
return uuid.NewV4()
}

func initialiseHTTPClient() {
// If the HTTPClient isn't set, start a new client with a default timeout of 15 seconds
if HTTPClient == nil {
Expand Down Expand Up @@ -227,7 +220,7 @@ func SendHTTPGetRequest(urlPath string, jsonDecode, isVerbose bool, result inter
defer res.Body.Close()

if jsonDecode {
err := JSONDecode(contents, result)
err := json.Unmarshal(contents, result)
if err != nil {
return err
}
Expand All @@ -236,19 +229,6 @@ func SendHTTPGetRequest(urlPath string, jsonDecode, isVerbose bool, result inter
return nil
}

// JSONEncode encodes structure data into JSON
func JSONEncode(v interface{}) ([]byte, error) {
return json.Marshal(v)
}

// JSONDecode decodes JSON data into a structure
func JSONDecode(data []byte, to interface{}) error {
if !strings.Contains(reflect.ValueOf(to).Type().String(), "*") {
return errors.New("json decode error - memory address not supplied")
}
return json.Unmarshal(data, to)
}

// EncodeURLValues concatenates url values onto a url string and returns a
// string
func EncodeURLValues(urlPath string, values url.Values) string {
Expand Down
56 changes: 0 additions & 56 deletions common/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,62 +263,6 @@ func TestSendHTTPGetRequest(t *testing.T) {
}
}

func TestJSONEncode(t *testing.T) {
t.Parallel()
type test struct {
Status int `json:"status"`
Data []struct {
Address string `json:"address"`
Balance float64 `json:"balance"`
Nonce interface{} `json:"nonce"`
Code string `json:"code"`
Name interface{} `json:"name"`
Storage interface{} `json:"storage"`
FirstSeen interface{} `json:"firstSeen"`
} `json:"data"`
}
expectOutputString := `{"status":0,"data":null}`
v := test{}

bitey, err := JSONEncode(v)
if err != nil {
t.Errorf("common JSONEncode error: %s", err)
}
if string(bitey) != expectOutputString {
t.Error("common JSONEncode error")
}
_, err = JSONEncode("WigWham")
if err != nil {
t.Errorf("common JSONEncode error: %s", err)
}
}

func TestJSONDecode(t *testing.T) {
t.Parallel()
var data []byte
result := "Not a memory address"
err := JSONDecode(data, result)
if err == nil {
t.Error("Common JSONDecode, unmarshalled when address not supplied")
}

type test struct {
Status int `json:"status"`
Data []struct {
Address string `json:"address"`
Balance float64 `json:"balance"`
} `json:"data"`
}

var v test
data = []byte(`{"status":1,"data":null}`)
err = JSONDecode(data, &v)
if err != nil || v.Status != 1 {
t.Errorf("Common JSONDecode. Data: %v \nError: %s",
v, err)
}
}

func TestEncodeURLValues(t *testing.T) {
t.Parallel()
urlstring := "https://www.test.com"
Expand Down
2 changes: 1 addition & 1 deletion communications/communications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func TestNewComm(t *testing.T) {
var cfg config.CommunicationsConfig
_, err := NewComm(&cfg)
if err == nil {
t.Error("NewComm should failed on no enabled communication mediums")
t.Error("NewComm should have failed on no enabled communication mediums")
}

cfg.TelegramConfig.Enabled = true
Expand Down
10 changes: 5 additions & 5 deletions communications/slack/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (s *Slack) PushEvent(event base.Event) error {
return s.WebsocketSend("message",
fmt.Sprintf("event: %s %s", event.Type, event.Message))
}
return nil
return errors.New("slack not connected")
}

// BuildURL returns an appended token string with the SlackURL
Expand Down Expand Up @@ -212,7 +212,7 @@ func (s *Slack) WebsocketReader() {
}

var data WebsocketResponse
err = common.JSONDecode(resp, &data)
err = json.Unmarshal(resp, &data)
if err != nil {
log.Errorln(log.CommunicationMgr, err)
continue
Expand Down Expand Up @@ -253,7 +253,7 @@ func (s *Slack) WebsocketReader() {

func (s *Slack) handlePresenceChange(resp []byte) error {
var pres PresenceChange
err := common.JSONDecode(resp, &pres)
err := json.Unmarshal(resp, &pres)
if err != nil {
return err
}
Expand All @@ -270,7 +270,7 @@ func (s *Slack) handleMessageResponse(resp []byte, data WebsocketResponse) error
return errors.New("reply to is != 0")
}
var msg Message
err := common.JSONDecode(resp, &msg)
err := json.Unmarshal(resp, &msg)
if err != nil {
return err
}
Expand Down Expand Up @@ -318,7 +318,7 @@ func (s *Slack) handleReconnectResponse(resp []byte) error {
URL string `json:"url"`
}
var recURL reconnectResponse
err := common.JSONDecode(resp, &recURL)
err := json.Unmarshal(resp, &recURL)
if err != nil {
return err
}
Expand Down
10 changes: 5 additions & 5 deletions communications/slack/slack_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package slack

import (
"encoding/json"
"testing"

"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/communications/base"
"github.com/thrasher-corp/gocryptotrader/config"
)
Expand Down Expand Up @@ -198,7 +198,7 @@ func TestHandlePresenceChange(t *testing.T) {
t.Error("slack handlePresenceChange(), unmarshalled malformed json")
}

data, _ := common.JSONEncode(pres)
data, _ := json.Marshal(pres)
err = s.handlePresenceChange(data)
if err != nil {
t.Errorf("slack handlePresenceChange() Error: %s", err)
Expand All @@ -225,15 +225,15 @@ func TestHandleMessageResponse(t *testing.T) {
var msg Message
msg.User = "1337"
msg.Text = "Hello World!"
resp, _ := common.JSONEncode(msg)
resp, _ := json.Marshal(msg)

err = s.handleMessageResponse(resp, data)
if err != nil {
t.Error("slack HandleMessage(), Sent message through nil websocket")
}

msg.Text = "!notacommand"
resp, _ = common.JSONEncode(msg)
resp, _ = json.Marshal(msg)

err = s.handleMessageResponse(resp, data)
if err == nil {
Expand Down Expand Up @@ -270,7 +270,7 @@ func TestHandleReconnectResponse(t *testing.T) {
}
testURL.URL = "https://www.thrasher.io"

data, _ := common.JSONEncode(testURL)
data, _ := json.Marshal(testURL)

err = s.handleReconnectResponse(data)
if err != nil || s.ReconnectURL != "https://www.thrasher.io" {
Expand Down
12 changes: 8 additions & 4 deletions communications/telegram/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package telegram

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
Expand Down Expand Up @@ -219,7 +220,7 @@ func (t *Telegram) SendMessage(text string, chatID int64) error {
text,
}

json, err := common.JSONEncode(&messageToSend)
json, err := json.Marshal(&messageToSend)
if err != nil {
return err
}
Expand All @@ -241,14 +242,17 @@ func (t *Telegram) SendMessage(text string, chatID int64) error {
}

// SendHTTPRequest sends an authenticated HTTP request
func (t *Telegram) SendHTTPRequest(path string, json []byte, result interface{}) error {
func (t *Telegram) SendHTTPRequest(path string, data []byte, result interface{}) error {
headers := make(map[string]string)
headers["content-type"] = "application/json"

resp, err := common.SendHTTPRequest(http.MethodPost, path, headers, bytes.NewBuffer(json))
resp, err := common.SendHTTPRequest(http.MethodPost,
path,
headers,
bytes.NewBuffer(data))
if err != nil {
return err
}

return common.JSONDecode([]byte(resp), result)
return json.Unmarshal([]byte(resp), result)
}
3 changes: 2 additions & 1 deletion config/config_encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/json"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -168,7 +169,7 @@ func DecryptConfigFile(configData, key []byte) ([]byte, error) {

// ConfirmConfigJSON confirms JSON in file
func ConfirmConfigJSON(file []byte, result interface{}) error {
return common.JSONDecode(file, &result)
return json.Unmarshal(file, &result)
}

// ConfirmSalt checks whether the encrypted data contains a salt
Expand Down
19 changes: 10 additions & 9 deletions currency/code.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package currency

import (
"encoding/json"
"errors"
"fmt"
"strings"
Expand All @@ -11,7 +12,7 @@ import (
func (r Role) String() string {
switch r {
case Unset:
return UnsetRollString
return UnsetRoleString
case Fiat:
return FiatCurrencyString
case Cryptocurrency:
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand Down
Loading