Skip to content

Commit

Permalink
[*] refactor: Reduce complexity a little bit
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew-M-C committed Sep 28, 2024
1 parent ff80771 commit 40c54c6
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 104 deletions.
65 changes: 11 additions & 54 deletions conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,62 +134,19 @@ func escapeStringToBuff(s string, buf buffer.Buffer, opt *Opt) {
}

func anyToInt(v any) (u int, err error) {
switch v := v.(type) {
case int:
u = v
case uint:
u = int(v)
case int64:
u = int(v)
case uint64:
u = int(v)
case int32:
u = int(v)
case uint32:
u = int(v)
case int16:
u = int(v)
case uint16:
u = int(v)
case int8:
u = int(v)
case uint8:
u = int(v)
if v == nil {
return 0, fmt.Errorf("%w: parameter is nil", ErrParameterError)
}
value := reflect.ValueOf(v)
switch value.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return int(value.Int()), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return int(value.Uint()), nil
default:
err = fmt.Errorf("%s is not a number", reflect.TypeOf(v).String())
return 0, fmt.Errorf("%v is not a number", reflect.TypeOf(v))
}

return
}

// func anyToInt64(v any) (i int64, err error) {
// switch v.(type) {
// case int:
// i = int64(v.(int))
// case uint:
// i = int64(v.(uint))
// case int64:
// i = int64(v.(int64))
// case uint64:
// i = int64(v.(uint64))
// case int32:
// i = int64(v.(int32))
// case uint32:
// i = int64(v.(uint32))
// case int16:
// i = int64(v.(int16))
// case uint16:
// i = int64(v.(uint16))
// case int8:
// i = int64(v.(int8))
// case uint8:
// i = int64(v.(uint8))
// default:
// err = fmt.Errorf("%s is not a number", reflect.TypeOf(v).String())
// }

// return
// }
}

func anyToString(v any) (s string, err error) {
if v == nil {
Expand Down
55 changes: 31 additions & 24 deletions get.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,33 +96,40 @@ func getFromObjectChildren(v *V, caseless bool, key string) (child *V, exist boo
}

func getInCurrentValue(v *V, caseless bool, param any) (*V, error) {
if v.valueType == Array {
// integer expected
pos, err := anyToInt(param)
if err != nil {
return &V{}, err
}
child, ok := childAtIndex(v, pos)
if !ok {
return &V{}, ErrOutOfRange
}
return child, nil
switch v.valueType {
case Array:
return getInCurrentArray(v, param)
case Object:
return getInCurrentObject(v, caseless, param)
default:
return &V{}, fmt.Errorf("%v type does not supports Get()", v.valueType)
}
}

} else if v.valueType == Object {
// string expected
key, err := anyToString(param)
if err != nil {
return &V{}, err
}
child, exist := getFromObjectChildren(v, caseless, key)
if !exist {
return &V{}, ErrNotFound
}
return child, nil
func getInCurrentArray(v *V, param any) (*V, error) {
// integer expected
pos, err := anyToInt(param)
if err != nil {
return &V{}, err
}
child, ok := childAtIndex(v, pos)
if !ok {
return &V{}, ErrOutOfRange
}
return child, nil
}

} else {
return &V{}, fmt.Errorf("%v type does not supports Get()", v.valueType)
func getInCurrentObject(v *V, caseless bool, param any) (*V, error) {
// string expected
key, err := anyToString(param)
if err != nil {
return &V{}, err
}
child, exist := getFromObjectChildren(v, caseless, key)
if !exist {
return &V{}, ErrNotFound
}
return child, nil
}

// GetBytes is similar with v, err := Get(...); v.Bytes(). But if error occurs or Base64 decode error, returns error.
Expand Down
57 changes: 31 additions & 26 deletions insert_append_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -587,36 +587,41 @@ func (v *V) delete(caseless bool, firstParam any, otherParams ...any) error {
}

func deleteInCurrentValue(v *V, caseless bool, param any) error {
if v.valueType == Object {
// string expected
key, err := anyToString(param)
if err != nil {
return err
}

if exist := delFromObjectChildren(v, caseless, key); !exist {
return ErrNotFound
}
return nil
switch v.valueType {
case Object:
return deleteInCurrentObject(v, caseless, param)
case Array:
return deleteInCurrentArray(v, param)
default:
// else, this is an object value
return fmt.Errorf("%v type does not supports Delete()", v.valueType)
}
}

if v.valueType == Array {
// integer expected
pos, err := anyToInt(param)
if err != nil {
return err
}

pos = posAtIndexForRead(v, pos)
if pos < 0 {
return ErrOutOfRange
}
deleteInArr(v, pos)
return nil
func deleteInCurrentObject(v *V, caseless bool, param any) error {
// string expected
key, err := anyToString(param)
if err != nil {
return err
}
if exist := delFromObjectChildren(v, caseless, key); !exist {
return ErrNotFound
}
return nil
}

// else, this is an object value
return fmt.Errorf("%v type does not supports Delete()", v.valueType)
func deleteInCurrentArray(v *V, param any) error {
// integer expected
pos, err := anyToInt(param)
if err != nil {
return err
}
pos = posAtIndexForRead(v, pos)
if pos < 0 {
return ErrOutOfRange
}
deleteInArr(v, pos)
return nil
}

func deleteInArr(v *V, pos int) {
Expand Down

0 comments on commit 40c54c6

Please sign in to comment.