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

cli gen: improve flag set defaults from message literals #1738

Merged
merged 1 commit into from
Sep 26, 2023

Conversation

kralicky
Copy link
Contributor

@kralicky kralicky commented Sep 26, 2023

This improves how field values from nested message literals are rendered as strings for flag set defaults.


For example, given:

message MetricsCapabilitySpec {
  .config.v1beta1.RulesSpec rules = 1 [(cli.flag_set).default = {
    [type.googleapis.com/config.v1beta1.RulesSpec]: {
      discovery: {
        prometheusRules: {
          searchNamespaces: [""]; // search all namespaces
        }
      }
    }
  }];
}

The default field above will translate into this call to flagutil.SetDefValue:

func (in *MetricsCapabilitySpec) FlagSet(prefix ...string) *pflag.FlagSet {
	// ...

	fs.AddFlagSet(in.Rules.FlagSet(append(prefix, "rules")...))
	flagutil.SetDefValue(fs, strings.Join(append(prefix, "rules", "discovery.prometheus-rules.search-namespaces"), "."), `[""]`)

	// ...
}

Partial fix for #1735

Copy link
Contributor

@alexandreLamarre alexandreLamarre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but given the complexity of generated code and the multitude of edge cases it could present, there should be unit tests for this package

@kralicky
Copy link
Contributor Author

As much as I agree, unit tests for this code would be very time consuming and it would probably be more efficient to instead write CLI tests that use the generated code, which we need to do anyway

@kralicky kralicky merged commit 50d8272 into main Sep 26, 2023
@kralicky kralicky deleted the cli-gen-nested-defaults branch September 26, 2023 21:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants