spf13 / spf13/cast

Support for ToSliceMapStringString in cast useful or better implement myself?

Open
#36 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

Hey,

thanks to cast. I found this library through viper.
I use viper to read a JSON configuration.
It works great so far. Viper has various support functions to get pre casted values. See Getting Values From Viper.

Next to standard types like string or bool is has support for GetStringMapStringSlice (which falls back to this library here).

Now my configuration file looks like this:

{
    "repositories": [
      {
        "name": "myvendor/package",
        "url": "git@othervcs:myvendor/package.git"
      }
    ],
    "dir": "/var/foo",
    "mirror": true,
    ...
}

The repositories would be a []map[string]string.
I need to do a few operations on this construct like "Is this repository is configured? If yes, is there a url?". To solve this question i do it like

func GetRepositoryURLOfPackage(n string) (*url.URL, error) {
	repositories := viper.Get("repositories")

	repositoriesSlice := repositories.([]interface{})
	if (len(repositoriesSlice) == 0) {
		return nil, errors.New("No repositories configured.")
	}

	for _, repoEntry := range repositoriesSlice {
		repoEntryMap := repoEntry.(map[string]interface{})
		if val, ok := repoEntryMap["name"]; !ok {
			if val.(string) == n {
				// TODO: Check if key "url" exists
				return repoEntryMap["url"].(string)
			}
		}
	}

	return nil, fmt.Errorf("No repository url found for package %s", n)
}

It works. A better solution would be to implement a ToStringMapStringSlice function.

Now the question: Do you see the need or does it make sense to implement a ToStringMapStringSlice into cast and enable this in viper as well?
Or should i implement it in my code and leave it there as a custom impl.?

The reason i ask:
There are multiple other usecases where those data structures are not implemented by cast like GetIntMapStringSlice, etc.
So it would be "never" complete.

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 reviewing cast's existing conversion functions and the linked Viper GetStringMapStringSlice implementation. Clarify whether support for the shown []map[string]string structure belongs in cast, including the naming and related map/slice variants. Done means the project has a decided scope and, if accepted, verified behavior for the example configuration.

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
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.