mirror of
https://github.com/safing/portbase
synced 2025-09-01 10:09:50 +00:00
Clean up accessor package, dedupe code
This commit is contained in:
parent
7ea7b5ed40
commit
3236ddf87f
4 changed files with 30 additions and 40 deletions
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
//nolint:maligned,unparam
|
||||
package accessor
|
||||
|
||||
import (
|
||||
|
|
Loading…
Add table
Reference in a new issue