Skip to content

Commit

Permalink
Add Must* version for the core methods and shortcut methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
cmstar committed Mar 13, 2022
1 parent 4fa07a8 commit e02d96a
Show file tree
Hide file tree
Showing 3 changed files with 519 additions and 0 deletions.
17 changes: 17 additions & 0 deletions conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,23 @@ func (c *Conv) Convert(src interface{}, dstPtr interface{}) error {
return nil
}

// MustConvertType is like ConvertType() but panics instead of returns an error.
func (c *Conv) MustConvertType(src interface{}, dstTyp reflect.Type) interface{} {
res, err := c.ConvertType(src, dstTyp)
if err != nil {
panic(err)
}
return res
}

// MustConvert is like Convert() but panics instead of returns an error.
func (c *Conv) MustConvert(src interface{}, dstPtr interface{}) {
err := c.Convert(src, dstPtr)
if err != nil {
panic(err)
}
}

// getUnderlyingValue extracts the underlying value if v is a pointer; otherwise returns v.
// If the pointer points to nil, returns nil.
func (c *Conv) getUnderlyingValue(v interface{}) interface{} {
Expand Down
154 changes: 154 additions & 0 deletions shortcut.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,157 @@ func MapToStruct(m map[string]interface{}, dstTyp reflect.Type) (interface{}, er
func StructToMap(v interface{}) (map[string]interface{}, error) {
return defaultConv.StructToMap(v)
}

// MustConvertType is equivalent to new(Conv).MustConvertType() .
func MustConvertType(src interface{}, dstTyp reflect.Type) interface{} {
return defaultConv.MustConvertType(src, dstTyp)
}

// MustConvert is equivalent to new(Conv).MustConvert() .
func MustConvert(src interface{}, dstPtr interface{}) {
defaultConv.MustConvert(src, dstPtr)
}

// MustBool is like Bool() but panics instead of returns an error.
func MustBool(v interface{}) bool {
res, err := defaultConv.SimpleToBool(v)
if err != nil {
panic(err)
}
return res
}

// MustString is like String() but panics instead of returns an error.
func MustString(v interface{}) string {
res, err := defaultConv.SimpleToString(v)
if err != nil {
panic(err)
}
return res
}

// MustInt is like Int() but panics instead of returns an error.
func MustInt(v interface{}) int {
res, err := defaultConv.simpleToPrimitive(v, reflect.Int)
if err != nil {
panic(err)
}
return res.(int)
}

// MustInt64 is like Int64() but panics instead of returns an error.
func MustInt64(v interface{}) int64 {
res, err := defaultConv.simpleToPrimitive(v, reflect.Int64)
if err != nil {
panic(err)
}
return res.(int64)
}

// MustInt32 is like Int32() but panics instead of returns an error.
func MustInt32(v interface{}) int32 {
res, err := defaultConv.simpleToPrimitive(v, reflect.Int32)
if err != nil {
panic(err)
}
return res.(int32)
}

// MustInt16 is like Int16() but panics instead of returns an error.
func MustInt16(v interface{}) int16 {
res, err := defaultConv.simpleToPrimitive(v, reflect.Int16)
if err != nil {
panic(err)
}
return res.(int16)
}

// MustInt8 is like Int8() but panics instead of returns an error.
func MustInt8(v interface{}) int8 {
res, err := defaultConv.simpleToPrimitive(v, reflect.Int8)
if err != nil {
panic(err)
}
return res.(int8)
}

// MustUint is like Uint() but panics instead of returns an error.
func MustUint(v interface{}) uint {
res, err := defaultConv.simpleToPrimitive(v, reflect.Uint)
if err != nil {
panic(err)
}
return res.(uint)
}

// MustUint64 is like Uint64() but panics instead of returns an error.
func MustUint64(v interface{}) uint64 {
res, err := defaultConv.simpleToPrimitive(v, reflect.Uint64)
if err != nil {
panic(err)
}
return res.(uint64)
}

// MustUint32 is like Uint32() but panics instead of returns an error.
func MustUint32(v interface{}) uint32 {
res, err := defaultConv.simpleToPrimitive(v, reflect.Uint32)
if err != nil {
panic(err)
}
return res.(uint32)
}

// MustUint16 is like Uint16() but panics instead of returns an error.
func MustUint16(v interface{}) uint16 {
res, err := defaultConv.simpleToPrimitive(v, reflect.Uint16)
if err != nil {
panic(err)
}
return res.(uint16)
}

// MustUint8 is like Uint8() but panics instead of returns an error.
func MustUint8(v interface{}) uint8 {
res, err := defaultConv.simpleToPrimitive(v, reflect.Uint8)
if err != nil {
panic(err)
}
return res.(uint8)
}

// MustFloat64 is like Float64() but panics instead of returns an error.
func MustFloat64(v interface{}) float64 {
res, err := defaultConv.simpleToPrimitive(v, reflect.Float64)
if err != nil {
panic(err)
}
return res.(float64)
}

// MustFloat32 is like Float32() but panics instead of returns an error.
func MustFloat32(v interface{}) float32 {
res, err := defaultConv.simpleToPrimitive(v, reflect.Float32)
if err != nil {
panic(err)
}
return res.(float32)
}

// MustMapToStruct is like MapToStruct() but panics instead of returns an error.
func MustMapToStruct(m map[string]interface{}, dstTyp reflect.Type) interface{} {
res, err := defaultConv.MapToStruct(m, dstTyp)
if err != nil {
panic(err)
}
return res
}

// MustStructToMap is like StructToMap() but panics instead of returns an error.
func MustStructToMap(v interface{}) map[string]interface{} {
res, err := defaultConv.StructToMap(v)
if err != nil {
panic(err)
}
return res
}
Loading

0 comments on commit e02d96a

Please sign in to comment.