Skip to content

Commit

Permalink
resolve error strings, log error
Browse files Browse the repository at this point in the history
  • Loading branch information
yukselcodingwithyou committed Nov 28, 2021
1 parent 15f9af0 commit 92f007f
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ type Value struct {
func (c Config) Value(key string) Value {
foundValue, err := getType(key, c)
if err != nil {
logError(err)
return Value{value: nil, err: err}
}
return Value{value: foundValue, err: nil}
}

func (v Value) Bool() (*bool, error) {
if err := v.error(reflect.Bool); err != nil {
logError(err)
return nil, err
} else {
value := v.value.(bool)
Expand All @@ -33,6 +33,7 @@ func (v Value) Bool() (*bool, error) {

func (v Value) Int() (*int, error) {
if err := v.error(reflect.Float64); err != nil {
logError(err)
return nil, err
} else {
value := int(v.value.(float64))
Expand All @@ -42,6 +43,7 @@ func (v Value) Int() (*int, error) {

func (v Value) Float() (*float64, error) {
if err := v.error(reflect.Float64); err != nil {
logError(err)
return nil, err
} else {
value := v.value.(float64)
Expand All @@ -51,6 +53,7 @@ func (v Value) Float() (*float64, error) {

func (v Value) String() (*string, error) {
if err := v.error(reflect.String); err != nil {
logError(err)
return nil, err
} else {
value := v.value.(string)
Expand All @@ -60,8 +63,8 @@ func (v Value) String() (*string, error) {

func (v Value) error(t reflect.Kind) error {
kind := reflect.ValueOf(v.value).Kind()
if kind != t {
return errors.New(fmt.Sprintf("type of value should be %s", kind))
if v.err == nil && kind != t {
return errors.New(fmt.Sprintf("type of value should be '%s', please use '%s()' function", kind, kind))
}
return v.err
}
Expand All @@ -80,5 +83,5 @@ func getType(key string, config Config) (interface{}, error) {
}

func errKeyNotFound(key string) error {
return errors.New(fmt.Sprintf("given key %s not exists in configuration", key))
return errors.New(fmt.Sprintf("given key '%s' not exists in configuration", key))
}

0 comments on commit 92f007f

Please sign in to comment.