Skip to content

Commit

Permalink
make indirect private
Browse files Browse the repository at this point in the history
  • Loading branch information
shockerli committed Mar 18, 2021
1 parent fa03dae commit c11ddf8
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 14 deletions.
42 changes: 41 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

[![PkgGoDev](https://pkg.go.dev/badge/github.com/shockerli/cvt)](https://pkg.go.dev/github.com/shockerli/cvt) [![Go Report Card](https://goreportcard.com/badge/github.com/shockerli/cvt)](https://goreportcard.com/report/github.com/shockerli/cvt) [![Build Status](https://travis-ci.com/shockerli/cvt.svg?branch=master)](https://travis-ci.com/shockerli/cvt)

> Simple, safe conversion of any type, including custom types
> Simple, safe conversion of any type, including custom types.
>
> Inspired by [cast](https://github.com/spf13/cast)
## Install

Expand Down Expand Up @@ -60,6 +62,44 @@ cvt.Float("hello", 12.34) // 12.34

> For more examples, see tests [cvt_test.go](cvt_test.go) and [cvte_test.go](cvte_test.go)
## API

- Bool
- BoolE
- ColumnsE
- FieldE
- Float32
- Float32E
- Float64
- Float64E
- Int
- Int16
- Int16E
- Int32
- Int32E
- Int64
- Int64E
- Int8
- Int8E
- IntE
- Slice
- SliceE
- String
- StringE
- Time
- TimeE
- Uint
- Uint16
- Uint16E
- Uint32
- Uint32E
- Uint64
- Uint64E
- Uint8
- Uint8E
- UintE


## License

This project is licensed under the terms of the [MIT](LICENSE) license.
41 changes: 40 additions & 1 deletion README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,45 @@ cvt.Float("hello", 12.34) // 12.34

> 所有示例,可通过单元测试了解:[cvt_test.go](cvt_test.go)[cvte_test.go](cvte_test.go)

## API

- Bool
- BoolE
- ColumnsE
- FieldE
- Float32
- Float32E
- Float64
- Float64E
- Int
- Int16
- Int16E
- Int32
- Int32E
- Int64
- Int64E
- Int8
- Int8E
- IntE
- Slice
- SliceE
- String
- StringE
- Time
- TimeE
- Uint
- Uint16
- Uint16E
- Uint32
- Uint32E
- Uint64
- Uint64E
- Uint8
- Uint8E
- UintE


## 开源协议

基于 [MIT](LICENSE) 开发源代码
本项目基于 [MIT](LICENSE) 协议开放源代码。
24 changes: 12 additions & 12 deletions cvte.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var formatExtend = "%v, %w"

// BoolE convert an interface to a bool type
func BoolE(val interface{}) (bool, error) {
v, rk, rv := Indirect(val)
v, rk, rv := indirect(val)

switch vv := v.(type) {
case bool:
Expand Down Expand Up @@ -130,7 +130,7 @@ func UintE(val interface{}) (uint, error) {
}

func convUint64(val interface{}) (uint64, error) {
v, _, rv := Indirect(val)
v, _, rv := indirect(val)

switch vv := v.(type) {
case nil:
Expand Down Expand Up @@ -228,7 +228,7 @@ func IntE(val interface{}) (int, error) {
}

func convInt64(val interface{}) (int64, error) {
v, _, rv := Indirect(val)
v, _, rv := indirect(val)

switch vv := v.(type) {
case nil:
Expand Down Expand Up @@ -265,7 +265,7 @@ func convInt64(val interface{}) (int64, error) {

// Float64E convert an interface to a float64 type
func Float64E(val interface{}) (float64, error) {
v, _, rv := Indirect(val)
v, _, rv := indirect(val)

switch vv := v.(type) {
case nil:
Expand Down Expand Up @@ -318,7 +318,7 @@ func Float32E(val interface{}) (float32, error) {

// StringE convert an interface to a string type
func StringE(val interface{}) (string, error) {
v, _, rv := Indirect(val)
v, _, rv := indirect(val)

// interface implements
switch vv := val.(type) {
Expand Down Expand Up @@ -360,7 +360,7 @@ func StringE(val interface{}) (string, error) {

// TimeE convert an interface to a time.Time type
func TimeE(val interface{}) (t time.Time, err error) {
v, _, _ := Indirect(val)
v, _, _ := indirect(val)

// source type
switch vv := v.(type) {
Expand Down Expand Up @@ -431,7 +431,7 @@ func SliceE(val interface{}) (sl []interface{}, err error) {
return nil, errUnsupportedTypeNil
}

_, rt, rv := Indirect(val)
_, rt, rv := indirect(val)

switch rt.Kind() {
case reflect.String:
Expand Down Expand Up @@ -485,7 +485,7 @@ func FieldE(val interface{}, field interface{}) (interface{}, error) {
}

sf := String(field) // match with the String of field, so field can be any type
_, rt, rv := Indirect(val)
_, rt, rv := indirect(val)

switch rt.Kind() {
case reflect.Map: // key of map
Expand All @@ -510,7 +510,7 @@ func ColumnsE(val interface{}, field interface{}) (sl []interface{}, err error)
return nil, errUnsupportedTypeNil
}

_, rt, rv := Indirect(val)
_, rt, rv := indirect(val)

switch rt.Kind() {
case reflect.Slice, reflect.Array:
Expand All @@ -537,8 +537,8 @@ func ColumnsE(val interface{}, field interface{}) (sl []interface{}, err error)
return nil, fmt.Errorf("unsupported type: %s", rt.Kind())
}

// Indirect returns the value with base type
func Indirect(a interface{}) (val interface{}, rt reflect.Type, rv reflect.Value) {
// returns the value with base type
func indirect(a interface{}) (val interface{}, rt reflect.Type, rv reflect.Value) {
if a == nil {
return
}
Expand All @@ -551,7 +551,7 @@ func Indirect(a interface{}) (val interface{}, rt reflect.Type, rv reflect.Value
for rv.Kind() == reflect.Ptr && !rv.IsNil() {
rv = rv.Elem()
}
return Indirect(rv.Interface())
return indirect(rv.Interface())
case reflect.Bool:
val = rv.Bool()
case reflect.Int:
Expand Down

0 comments on commit c11ddf8

Please sign in to comment.