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

Change type-definition of ValueSourceChain #1999

Open
dennis-tra opened this issue Oct 30, 2024 · 1 comment
Open

Change type-definition of ValueSourceChain #1999

dennis-tra opened this issue Oct 30, 2024 · 1 comment
Labels
area/v3 relates to / is being considered for v3 status/triage maintainers still need to look into this

Comments

@dennis-tra
Copy link

Just a fly-by comment. I'm currently looking into the changes of v3 vs v2 and saw the definition of:

type ValueSourceChain struct {
	Chain []ValueSource
}

Given the current usage of ValueSourceChain, I think this could be simplified to:

type ValueSourceChain []ValueSource
Complete Changeset:
type ValueChain []ValueSource

func NewValueChain(src ...ValueSource) ValueChain {
  return src
}

func (vsc ValueChain) Append(other ValueChain) {
  vsc = append(vsc, other...)
}

func (vsc ValueChain) EnvKeys() []string {
  vals := []string{}

  for _, src := range vsc {
    if v, ok := src.(*envVarValueSource); ok {
      vals = append(vals, v.Key)
    }
  }

  return vals
}

func (vsc ValueChain) String() string {
  s := []string{}

  for _, vs := range vsc {
    s = append(s, vs.String())
  }

  return strings.Join(s, ",")
}

func (vsc ValueChain) GoString() string {
  s := []string{}

  for _, vs := range vsc {
    s = append(s, vs.GoString())
  }

  return fmt.Sprintf("&ValueChain{Chain:{%[1]s}}", strings.Join(s, ","))
}

func (vsc ValueChain) Lookup() (string, bool) {
  s, _, ok := vsc.LookupWithSource()
  return s, ok
}

func (vsc ValueChain) LookupWithSource() (string, ValueSource, bool) {
  for _, src := range vsc {
    if value, found := src.Lookup(); found {
      return value, src, true
    }
  }

  return "", nil, false
}

This could make flag definitions a bit more ergonomic:

// before
&cli.StringFlag{
	Name: "log.level",
	Sources: cli.ValueSourceChain{
		Chain: []cli.ValueSource{cli.EnvVar("LOG_LEVEL")},
	},
	Usage:       "description",
	Destination: &cfg.LogLevel,
	Value:       cfg.LogLevel,
	Category:    "Logging",
}

// after
&cli.StringFlag{
	Name: "log.level",
	Sources: cli.ValueSourceChain{
		cli.EnvVar("LOG_LEVEL"),
	},
	Usage:       "description",
	Destination: &cfg.LogLevel,
	Value:       cfg.LogLevel,
	Category:    "Logging",
}

I wanted to create a quick issue because v3 isn't released yet and there's still an opportunity to do breaking changes.

@dennis-tra dennis-tra added area/v3 relates to / is being considered for v3 status/triage maintainers still need to look into this labels Oct 30, 2024
@dearchap
Copy link
Contributor

@dennis-tra We've gone through a couple of iterations of this. Can you look at the v3 changes for this particular piece of code and submit a PR if you'd like us to make the change ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/v3 relates to / is being considered for v3 status/triage maintainers still need to look into this
Projects
None yet
Development

No branches or pull requests

2 participants