Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat: (convert) add ozzo-validator in converter
  • Loading branch information
meabed committed Sep 30, 2018
1 parent 03ff266 commit 029a974
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 7 deletions.
21 changes: 21 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Gopkg.toml
Expand Up @@ -31,6 +31,7 @@ required = [
"github.com/bitly/go-simplejson",
"github.com/go-redis/redis",
"github.com/op/go-logging",
"github.com/go-ozzo/ozzo-validation",
"github.com/stretchr/testify"
]

Expand Down Expand Up @@ -61,3 +62,7 @@ required = [
go-version = "1.11"
install = ["./cmd/server/..."]
ensure = "true"

[[constraint]]
name = "github.com/go-ozzo/ozzo-validation"
version = "3.5.0"
15 changes: 10 additions & 5 deletions cmd/server/convert.go
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/md5"
"encoding/json"
"fmt"
"github.com/go-ozzo/ozzo-validation"
ex "github.com/me-io/go-swap/pkg/exchanger"
"github.com/me-io/go-swap/pkg/swap"
"io/ioutil"
Expand All @@ -16,12 +17,16 @@ import (
// Validate ... Validation function for convertReqObj
func (c *convertReqObj) Validate() error {

if ex.CurrencyList[c.To] == "" || ex.CurrencyList[c.From] == "" {
return fmt.Errorf("currency %s or %s is not supoorted", c.From, c.To)
}
return validation.ValidateStruct(c,
validation.Field(&c.Amount, validation.Required),
validation.Field(&c.From, validation.Required, validation.In(ex.CurrencyListArr...)),
validation.Field(&c.To, validation.Required, validation.In(ex.CurrencyListArr...)),
validation.Field(&c.Exchanger, validation.Required),
)

// todo implement
return nil
//if ex.CurrencyList[c.To] == "" || ex.CurrencyList[c.From] == "" {
// return fmt.Errorf("currency %s or %s is not supported", c.From, c.To)
//}
}

// Hash ... return md5 string hash of the convertReqObj with 1 Unit Amount to cache the rate only for 1 Unit Amount
Expand Down
4 changes: 2 additions & 2 deletions cmd/server/main.go
Expand Up @@ -21,9 +21,9 @@ var (
cacheDriver string
redisUrl string
// Storage ... Server Cache Storage
Storage cache.Storage
Storage cache.Storage
// Logger ... Logger Driver
Logger = logging.MustGetLogger("go-swap-server")
Logger = logging.MustGetLogger("go-swap-server")

format = logging.MustStringFormatter(
`%{color}%{time:2006-01-02T15:04:05.999999} %{shortfunc} ▶ %{level:.8s} %{id:03x}%{color:reset} %{message}`,
Expand Down
3 changes: 3 additions & 0 deletions pkg/exchanger/currency_list.go
Expand Up @@ -289,3 +289,6 @@ var CurrencyList = map[string]string{
"ZWR": "Zimbabwean Dollar (2008)",
"ZWL": "Zimbabwean Dollar (2009)",
}

// CurrencyListRev ... CurrencyList to array of int => interfaces to be used in validation
var CurrencyListArr = MapKeyArrInterface(CurrencyList)
19 changes: 19 additions & 0 deletions pkg/exchanger/helpers.go
@@ -0,0 +1,19 @@
package exchanger

// ReverseMap ... Reverse a map in <value, key>
func ReverseMap(m map[string]string) map[string]string {
n := make(map[string]string)
for k, v := range m {
n[v] = k
}
return n
}

// MapKeyArrInterface ... Reverse a map in <value, key>
func MapKeyArrInterface(m map[string]string) []interface{} {
var n []interface{}
for k := range m {
n = append(n, k)
}
return n
}

0 comments on commit 029a974

Please sign in to comment.