Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StringMapE adapt to arrays #148

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ _testmain.go
*.test

*.bench

.idea
78 changes: 3 additions & 75 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,75 +1,3 @@
cast
====
[![GoDoc](https://godoc.org/github.com/spf13/cast?status.svg)](https://godoc.org/github.com/spf13/cast)
[![Build Status](https://github.com/spf13/cast/actions/workflows/go.yml/badge.svg)](https://github.com/spf13/cast/actions/workflows/go.yml)
[![Go Report Card](https://goreportcard.com/badge/github.com/spf13/cast)](https://goreportcard.com/report/github.com/spf13/cast)

Easy and safe casting from one type to another in Go

Don’t Panic! ... Cast

## What is Cast?

Cast is a library to convert between different go types in a consistent and easy way.

Cast provides simple functions to easily convert a number to a string, an
interface into a bool, etc. Cast does this intelligently when an obvious
conversion is possible. It doesn’t make any attempts to guess what you meant,
for example you can only convert a string to an int when it is a string
representation of an int such as “8”. Cast was developed for use in
[Hugo](http://hugo.spf13.com), a website engine which uses YAML, TOML or JSON
for meta data.

## Why use Cast?

When working with dynamic data in Go you often need to cast or convert the data
from one type into another. Cast goes beyond just using type assertion (though
it uses that when possible) to provide a very straightforward and convenient
library.

If you are working with interfaces to handle things like dynamic content
you’ll need an easy way to convert an interface into a given type. This
is the library for you.

If you are taking in data from YAML, TOML or JSON or other formats which lack
full types, then Cast is the library for you.

## Usage

Cast provides a handful of To_____ methods. These methods will always return
the desired type. **If input is provided that will not convert to that type, the
0 or nil value for that type will be returned**.

Cast also provides identical methods To_____E. These return the same result as
the To_____ methods, plus an additional error which tells you if it successfully
converted. Using these methods you can tell the difference between when the
input matched the zero value or when the conversion failed and the zero value
was returned.

The following examples are merely a sample of what is available. Please review
the code for a complete set.

### Example ‘ToString’:

cast.ToString("mayonegg") // "mayonegg"
cast.ToString(8) // "8"
cast.ToString(8.31) // "8.31"
cast.ToString([]byte("one time")) // "one time"
cast.ToString(nil) // ""

var foo interface{} = "one more time"
cast.ToString(foo) // "one more time"


### Example ‘ToInt’:

cast.ToInt(8) // 8
cast.ToInt(8.31) // 8
cast.ToInt("8") // 8
cast.ToInt(true) // 1
cast.ToInt(false) // 0

var eight interface{} = 8
cast.ToInt(eight) // 8
cast.ToInt(nil) // 0

修复了一些官方不解决的BUG(Fixed some bugs that are not officially resolved)
1. StringMapE 支持数组类型
2. ToInt系列的函数支持`08` `09`
2 changes: 1 addition & 1 deletion cast.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2014 Steve Francia <spf@spf13.com>.
// Copyright © 2014 Steve Francia <spf@kaylee595.com>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
Expand Down
2 changes: 1 addition & 1 deletion cast_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2014 Steve Francia <spf@spf13.com>.
// Copyright © 2014 Steve Francia <spf@kaylee595.com>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
Expand Down
27 changes: 16 additions & 11 deletions caste.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2014 Steve Francia <spf@spf13.com>.
// Copyright © 2014 Steve Francia <spf@kaylee595.com>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
Expand Down Expand Up @@ -260,7 +260,7 @@ func ToInt64E(i interface{}) (int64, error) {
case float32:
return int64(s), nil
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseInt(trimZeroDecimal(s), 10, 0)
if err == nil {
return v, nil
}
Expand Down Expand Up @@ -312,7 +312,7 @@ func ToInt32E(i interface{}) (int32, error) {
case float32:
return int32(s), nil
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseInt(trimZeroDecimal(s), 10, 0)
if err == nil {
return int32(v), nil
}
Expand Down Expand Up @@ -364,7 +364,7 @@ func ToInt16E(i interface{}) (int16, error) {
case float32:
return int16(s), nil
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseInt(trimZeroDecimal(s), 10, 0)
if err == nil {
return int16(v), nil
}
Expand Down Expand Up @@ -416,7 +416,7 @@ func ToInt8E(i interface{}) (int8, error) {
case float32:
return int8(s), nil
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseInt(trimZeroDecimal(s), 10, 0)
if err == nil {
return int8(v), nil
}
Expand Down Expand Up @@ -468,7 +468,7 @@ func ToIntE(i interface{}) (int, error) {
case float32:
return int(s), nil
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseInt(trimZeroDecimal(s), 10, 0)
if err == nil {
return int(v), nil
}
Expand Down Expand Up @@ -501,7 +501,7 @@ func ToUintE(i interface{}) (uint, error) {

switch s := i.(type) {
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseInt(trimZeroDecimal(s), 10, 0)
if err == nil {
if v < 0 {
return 0, errNegativeNotAllowed
Expand Down Expand Up @@ -577,7 +577,7 @@ func ToUint64E(i interface{}) (uint64, error) {

switch s := i.(type) {
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseInt(trimZeroDecimal(s), 10, 0)
if err == nil {
if v < 0 {
return 0, errNegativeNotAllowed
Expand Down Expand Up @@ -653,7 +653,7 @@ func ToUint32E(i interface{}) (uint32, error) {

switch s := i.(type) {
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseInt(trimZeroDecimal(s), 10, 0)
if err == nil {
if v < 0 {
return 0, errNegativeNotAllowed
Expand Down Expand Up @@ -729,7 +729,7 @@ func ToUint16E(i interface{}) (uint16, error) {

switch s := i.(type) {
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseInt(trimZeroDecimal(s), 10, 0)
if err == nil {
if v < 0 {
return 0, errNegativeNotAllowed
Expand Down Expand Up @@ -805,7 +805,7 @@ func ToUint8E(i interface{}) (uint8, error) {

switch s := i.(type) {
case string:
v, err := strconv.ParseInt(trimZeroDecimal(s), 0, 0)
v, err := strconv.ParseInt(trimZeroDecimal(s), 10, 0)
if err == nil {
if v < 0 {
return 0, errNegativeNotAllowed
Expand Down Expand Up @@ -1095,6 +1095,11 @@ func ToStringMapE(i interface{}) (map[string]interface{}, error) {
return m, nil
case map[string]interface{}:
return v, nil
case []interface{}:
for x := range v {
m[ToString(x)] = v[x]
}
return m, nil
case string:
err := jsonStringToObject(v, &m)
return m, err
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/spf13/cast
module github.com/kaylee595/cast

go 1.18

Expand Down