diff --git a/Dockerfile b/Dockerfile index c6793c7..a711ade 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 . diff --git a/pkg/client/value.go b/pkg/client/value.go index e0137b1..9d16e72 100644 --- a/pkg/client/value.go +++ b/pkg/client/value.go @@ -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 { @@ -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 }