diff --git a/database/accessor/accessor-json-bytes.go b/database/accessor/accessor-json-bytes.go index 3115bf6..f8a6923 100644 --- a/database/accessor/accessor-json-bytes.go +++ b/database/accessor/accessor-json-bytes.go @@ -1,8 +1,6 @@ package accessor import ( - "fmt" - "github.com/tidwall/gjson" "github.com/tidwall/sjson" ) @@ -23,23 +21,9 @@ func NewJSONBytesAccessor(json *[]byte) *JSONBytesAccessor { func (ja *JSONBytesAccessor) Set(key string, value interface{}) error { result := gjson.GetBytes(*ja.json, key) if result.Exists() { - switch value.(type) { - case string: - if result.Type != gjson.String { - return fmt.Errorf("tried to set field %s (%s) to a %T value", key, result.Type.String(), value) - } - case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64: - if result.Type != gjson.Number { - return fmt.Errorf("tried to set field %s (%s) to a %T value", key, result.Type.String(), value) - } - case bool: - if result.Type != gjson.True && result.Type != gjson.False { - return fmt.Errorf("tried to set field %s (%s) to a %T value", key, result.Type.String(), value) - } - case []string: - if !result.IsArray() { - return fmt.Errorf("tried to set field %s (%s) to a %T value", key, result.Type.String(), value) - } + err := checkJSONValueType(result, key, value) + if err != nil { + return err } } diff --git a/database/accessor/accessor-json-string.go b/database/accessor/accessor-json-string.go index c5c215d..b6e8025 100644 --- a/database/accessor/accessor-json-string.go +++ b/database/accessor/accessor-json-string.go @@ -23,23 +23,9 @@ func NewJSONAccessor(json *string) *JSONAccessor { func (ja *JSONAccessor) Set(key string, value interface{}) error { result := gjson.Get(*ja.json, key) if result.Exists() { - switch value.(type) { - case string: - if result.Type != gjson.String { - return fmt.Errorf("tried to set field %s (%s) to a %T value", key, result.Type.String(), value) - } - case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64: - if result.Type != gjson.Number { - return fmt.Errorf("tried to set field %s (%s) to a %T value", key, result.Type.String(), value) - } - case bool: - if result.Type != gjson.True && result.Type != gjson.False { - return fmt.Errorf("tried to set field %s (%s) to a %T value", key, result.Type.String(), value) - } - case []string: - if !result.IsArray() { - return fmt.Errorf("tried to set field %s (%s) to a %T value", key, result.Type.String(), value) - } + err := checkJSONValueType(result, key, value) + if err != nil { + return err } } @@ -51,6 +37,28 @@ func (ja *JSONAccessor) Set(key string, value interface{}) error { return nil } +func checkJSONValueType(jsonValue gjson.Result, key string, value interface{}) error { + switch value.(type) { + case string: + if jsonValue.Type != gjson.String { + return fmt.Errorf("tried to set field %s (%s) to a %T value", key, jsonValue.Type.String(), value) + } + case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64: + if jsonValue.Type != gjson.Number { + return fmt.Errorf("tried to set field %s (%s) to a %T value", key, jsonValue.Type.String(), value) + } + case bool: + if jsonValue.Type != gjson.True && jsonValue.Type != gjson.False { + return fmt.Errorf("tried to set field %s (%s) to a %T value", key, jsonValue.Type.String(), value) + } + case []string: + if !jsonValue.IsArray() { + return fmt.Errorf("tried to set field %s (%s) to a %T value", key, jsonValue.Type.String(), value) + } + } + return nil +} + // Get returns the value found by the given json key and whether it could be successfully extracted. func (ja *JSONAccessor) Get(key string) (value interface{}, ok bool) { result := gjson.Get(*ja.json, key) diff --git a/database/accessor/accessor-struct.go b/database/accessor/accessor-struct.go index 6f245aa..c13072b 100644 --- a/database/accessor/accessor-struct.go +++ b/database/accessor/accessor-struct.go @@ -160,10 +160,7 @@ func (sa *StructAccessor) GetBool(key string) (value bool, ok bool) { // Exists returns the whether the given key exists. func (sa *StructAccessor) Exists(key string) bool { field := sa.object.FieldByName(key) - if field.IsValid() { - return true - } - return false + return field.IsValid() } // Type returns the accessor type as a string. diff --git a/database/accessor/accessor_test.go b/database/accessor/accessor_test.go index 9190818..3d6d5bb 100644 --- a/database/accessor/accessor_test.go +++ b/database/accessor/accessor_test.go @@ -1,3 +1,4 @@ +//nolint:maligned,unparam package accessor import (