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

Improve the code to support int and other types and cast them to string #498

Merged
merged 2 commits into from
Jan 17, 2020
Merged
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# CHANGELOG

## v13.3.1

### Bug Fixes

- Updated `autorest.AsStringSlice()` to convert slice elements to their string representation.

## v13.3.0

### New Features
Expand Down
10 changes: 5 additions & 5 deletions autorest/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,18 @@ func MapToValues(m map[string]interface{}) url.Values {
return v
}

// AsStringSlice method converts interface{} to []string. This expects a
//that the parameter passed to be a slice or array of a type that has the underlying
//type a string.
// AsStringSlice method converts interface{} to []string.
// s must be of type slice or array or an error is returned.
// Each element of s will be converted to its string representation.
func AsStringSlice(s interface{}) ([]string, error) {
v := reflect.ValueOf(s)
if v.Kind() != reflect.Slice && v.Kind() != reflect.Array {
return nil, NewError("autorest", "AsStringSlice", "the value's type is not an array.")
return nil, NewError("autorest", "AsStringSlice", "the value's type is not a slice or array.")
}
stringSlice := make([]string, 0, v.Len())

for i := 0; i < v.Len(); i++ {
stringSlice = append(stringSlice, v.Index(i).String())
stringSlice = append(stringSlice, fmt.Sprintf("%v", v.Index(i)))
}
return stringSlice, nil
}
Expand Down
9 changes: 9 additions & 0 deletions autorest/utility_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,15 @@ func TestStringWithEnumSlice(t *testing.T) {
}
}

func TestStringWithIntegerSlice(t *testing.T) {
type TestEnumType int32
s := []TestEnumType{1, 2}
if got, want := String(s, ","), "1,2"; got != want {
t.Logf("got: %q\nwant: %q", got, want)
t.Fail()
}
}

func ExampleAsStringSlice() {
type TestEnumType string

Expand Down
2 changes: 1 addition & 1 deletion autorest/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"runtime"
)

const number = "v13.3.0"
const number = "v13.3.1"

var (
userAgent = fmt.Sprintf("Go/%s (%s-%s) go-autorest/%s",
Expand Down