spf13 / spf13/cast

Feature: ToSlice

Open
#355 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Go
Stars
4k
Forks
337
Avg merge
15m
Merged PRs (30d)
1

Description

The current scope of application for the ToSlice method is too narrow, failing to cover many scenarios—for example:

type Users []User

ToSlice([]any{1,2,3,}) => []any{1, 2, 3}
ToSlice([]int{1,2,3,}) => []any{} // should []any{1,2,3}
ToSlice([]User{u1, u2}) => []any{}  // should []any{u1, u2}

A possible new implementation:

  func ToSliceE(v any) ([]any, error) {
        if v == nil {
                return nil, fmt.Errorf("nil")
        }
        rv := reflect.ValueOf(v)
        if rv.Kind() != reflect.Slice && rv.Kind() != reflect.Array {
                return nil, fmt.Errorf("not slice/array: %T", v)
        }
        out := make([]any, rv.Len())
        for i := 0; i < rv.Len(); i++ {
                out[i] = rv.Index(i).Interface()
        }
        return out, nil
  }

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the existing ToSlice implementation and its related tests, then compare their current behavior for []any, []int, and named []User slices or arrays. Done means supported slice and array values are converted into []any with all elements preserved, with tests covering the examples in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
67/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.