Skip to content

Commit

Permalink
Refactor Value methods
Browse files Browse the repository at this point in the history
  • Loading branch information
pipe01 committed Jan 20, 2021
1 parent 3c8b92a commit 47d9103
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 20 deletions.
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
FROM golang:1.15.6-alpine3.12 AS go-builder
WORKDIR /src/
RUN apk --no-cache add upx git
RUN go get -u github.com/kataras/bindata/cmd/bindata
ENV GO111MODULE on

COPY go.mod .
Expand Down
111 changes: 92 additions & 19 deletions pkg/client/value.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
package client

import "encoding/json"
import (
"encoding/json"
"time"
)

type Value interface {
Error() error
Raw() string
String() string
Scan(v interface{}) error
String() (string, bool)
StringOr(def string) string
Int32() int32
Int32() (int32, bool)
Int32Or(def int32) int32
Bool() bool
Int64() (int64, bool)
Int64Or(def int64) int64
Bool() (bool, bool)
BoolOr(def bool) bool
Float64() (float64, bool)
Float64Or(def float64) float64
Duration() (time.Duration, bool)
DurationOr(def time.Duration) time.Duration
}

type jsonValue struct {
Expand All @@ -28,38 +38,101 @@ func (v *jsonValue) Raw() string {
return v.val
}

func (v *jsonValue) String() string {
return v.StringOr("")
func (v *jsonValue) Scan(val interface{}) error {
return json.Unmarshal([]byte(v.val), val)
}

func (v *jsonValue) StringOr(def string) string {
func (v *jsonValue) String() (string, bool) {
var ret string
if err := json.Unmarshal([]byte(v.val), &ret); err != nil {
return def
return "", false
}
return ret
return ret, true
}

func (v *jsonValue) Int32() int32 {
return v.Int32Or(0)
func (v *jsonValue) StringOr(def string) string {
if val, ok := v.String(); ok {
return val
}
return def
}

func (v *jsonValue) Int32Or(def int32) int32 {
func (v *jsonValue) Int32() (int32, bool) {
var ret int32
if err := json.Unmarshal([]byte(v.val), &ret); err != nil {
return def
return 0, false
}
return ret
return ret, true
}

func (v *jsonValue) Bool() bool {
return v.BoolOr(false)
func (v *jsonValue) Int32Or(def int32) int32 {
if val, ok := v.Int32(); ok {
return val
}
return def
}

func (v *jsonValue) BoolOr(def bool) bool {
func (v *jsonValue) Int64() (int64, bool) {
var ret int64
if err := json.Unmarshal([]byte(v.val), &ret); err != nil {
return 0, false
}
return ret, true
}

func (v *jsonValue) Int64Or(def int64) int64 {
if val, ok := v.Int64(); ok {
return val
}
return def
}

func (v *jsonValue) Bool() (bool, bool) {
var ret bool
if err := json.Unmarshal([]byte(v.val), &ret); err != nil {
return def
return false, false
}
return ret, true
}

func (v *jsonValue) BoolOr(def bool) bool {
if val, ok := v.Bool(); ok {
return val
}
return def
}

func (v *jsonValue) Float64() (float64, bool) {
var ret float64
if err := json.Unmarshal([]byte(v.val), &ret); err != nil {
return 0, false
}
return ret, true
}

func (v *jsonValue) Float64Or(def float64) float64 {
if val, ok := v.Float64(); ok {
return val
}
return def
}

func (v *jsonValue) Duration() (time.Duration, bool) {
if str, ok := v.String(); ok {
dur, err := time.ParseDuration(str)
if err != nil {
return 0, false
}

return dur, true
}

return 0, false
}

func (v *jsonValue) DurationOr(def time.Duration) time.Duration {
if val, ok := v.Duration(); ok {
return val
}
return ret
return def
}

0 comments on commit 47d9103

Please sign in to comment.