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

feat: Cmd evaluation #2609

Merged
merged 14 commits into from
Nov 22, 2024
Merged

Conversation

mexes20
Copy link
Contributor

@mexes20 mexes20 commented Nov 2, 2024

Description

evaluation command line for local testing

Closes issue #2560

Resolve #2560

Checklist

  • I have tested this code
  • I have added unit test to cover this code
  • I have updated the documentation (README.md and /website/docs)
  • I have followed the contributing guide

Copy link

netlify bot commented Nov 2, 2024

Deploy Preview for go-feature-flag-doc-preview ready!

Name Link
🔨 Latest commit 9fd49d6
🔍 Latest deploy log https://app.netlify.com/sites/go-feature-flag-doc-preview/deploys/674084c3d2f53c000942ca7f
😎 Deploy Preview https://deploy-preview-2609--go-feature-flag-doc-preview.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@mexes20 mexes20 changed the title Cmd evaluation feat: Cmd evaluation Nov 3, 2024
Copy link
Owner

@thomaspoignant thomaspoignant left a comment

Choose a reason for hiding this comment

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

Hey @mexes20 thanks for your contribution, I am looking forward for this new cli.

I have made a first pass and found some things to fix before being able to have a working command line.
Can you try to fix it and ensure that the code compiles before re-requesting a review?

Thanks again for your contribution 🙏


"github.com/thomaspoignant/go-feature-flag/ffuser"
"github.com/thomaspoignant/go-feature-flag/retriever"
"github.com/thomaspoignant/go-feature-flag/variation"
Copy link
Owner

Choose a reason for hiding this comment

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

issue: This package does not exist in go-feature-flag I am not sure why you are importing it? I see that you are trying to use it in your code too. Did you miss committing something or is this an error?

"io/ioutil"

"github.com/thomaspoignant/go-feature-flag/ffuser"
"github.com/thomaspoignant/go-feature-flag/retriever"
Copy link
Owner

Choose a reason for hiding this comment

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

issue: This import is not used. We should remove it.

Suggested change
"github.com/thomaspoignant/go-feature-flag/retriever"

}

type FlagConfig struct {
Flags map[string]*variation.Flag `yaml:"flags"`
Copy link
Owner

Choose a reason for hiding this comment

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

suggestion: As mention in a previous comment, the package variation does not exists, what you are looking at here is flag.Flag

Suggested change
Flags map[string]*variation.Flag `yaml:"flags"`
Flags map[string]*flag.Flag `yaml:"flags"`

evaluateCmd.Flags().StringVarP(&flagName, "flag", "f", "", "Name of the flag to evaluate")
evaluateCmd.Flags().StringVarP(&evaluationContext, "evaluation-context", "e", "{}", "Evaluation context in JSON format")

evaluateCmd.MarkFlagRequired("config")
Copy link
Owner

Choose a reason for hiding this comment

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

suggestion: We should handle the error return by MarkFlagRequired to be sure that we are not letting the user pass without the required parameters.

cmd/cli/main.go Outdated
Comment on lines 12 to 13
fmt.Fprintf(os.Stderr, "Error executing command: %v\n", err)
os.Exit(1)
Copy link
Owner

Choose a reason for hiding this comment

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

suggestion: Since we want to exit in error and write an error I would suggest to use log.Fatalf instead.

Suggested change
fmt.Fprintf(os.Stderr, "Error executing command: %v\n", err)
os.Exit(1)
log.Fatalf("Error executing command: %v\n", err)

user := ffuser.NewUser(targetingKey)
for k, v := range context {
if k != "targetingKey" {
user.Custom[k] = v
Copy link
Owner

Choose a reason for hiding this comment

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

issue: Custom is not a public attribute so you can't add things in the context like this.

suggestion: Use the function AddCustomAttribute(k, v) instead

Suggested change
user.Custom[k] = v
evalCtx.AddCustomAttribute(k, v)

return &config, nil
}

func parseContext(contextJSON string) (*ffuser.User, error) {
Copy link
Owner

Choose a reason for hiding this comment

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

suggestion: ffuser.User is deprecated, please use ffcontext.EvaluationContext instead.

Comment on lines 16 to 25
type EvaluationResult struct {
TrackEvents bool `json:"trackEvents"`
VariationType string `json:"variationType"`
Failed bool `json:"failed"`
Version string `json:"version"`
Reason string `json:"reason"`
ErrorCode string `json:"errorCode"`
Value interface{} `json:"value"`
Cacheable bool `json:"cacheable"`
}
Copy link
Owner

Choose a reason for hiding this comment

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

question: Why do you need to create a new response struct here? You can probably use model.VariationResult instead that has the exact same signature (except that it is a generic type).

This struct is available here.

return nil
}

func evaluateSingleFlag(flag *variation.Flag, user *ffuser.User) (*EvaluationResult, error) {
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
func evaluateSingleFlag(flag *variation.Flag, user *ffuser.User) (*EvaluationResult, error) {
func evaluateSingleFlag(flag *variation.Flag, user *ffcontext.EvaluationContext) (*EvaluationResult, error) {

func evaluateAllFlags(config *FlagConfig, user *ffuser.User) error {
results := make(map[string]*EvaluationResult)

for flagName, flag := range config.Flags {
Copy link
Owner

Choose a reason for hiding this comment

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

issue: Here config.Flags will always be empty since you've passed nothing for the flag name here.

suggestion: You can directly use the function AllFlagsState that perform the evaluation for all the flags.

@mexes20
Copy link
Contributor Author

mexes20 commented Nov 5, 2024

Hey @mexes20 thanks for your contribution, I am looking forward for this new cli.

I have made a first pass and found some things to fix before being able to have a working command line. Can you try to fix it and ensure that the code compiles before re-requesting a review?

Thanks again for your contribution 🙏

Okay ser, I'm working on it now.

@mexes20
Copy link
Contributor Author

mexes20 commented Nov 15, 2024

@thomaspoignant I'm still working on this ser, just having a hard time with some parts but I'll fix it soon

Signed-off-by: Thomas Poignant <[email protected]>
Copy link

codecov bot commented Nov 21, 2024

Codecov Report

Attention: Patch coverage is 92.90780% with 10 lines in your changes missing coverage. Please review.

Project coverage is 84.73%. Comparing base (8c2e215) to head (9fd49d6).
Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
cmd/cli/main.go 69.23% 4 Missing ⚠️
cmd/cli/evaluate/evaluate.go 92.10% 2 Missing and 1 partial ⚠️
cmd/cli/evaluate/evaluate_cmd.go 91.66% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2609      +/-   ##
==========================================
+ Coverage   84.50%   84.73%   +0.22%     
==========================================
  Files         106      111       +5     
  Lines        5021     5162     +141     
==========================================
+ Hits         4243     4374     +131     
- Misses        616      624       +8     
- Partials      162      164       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.


🚨 Try these New Features:

Signed-off-by: Thomas Poignant <[email protected]>
Signed-off-by: Thomas Poignant <[email protected]>
Signed-off-by: Thomas Poignant <[email protected]>
Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
6.2% Duplication on New Code (required ≤ 3%)

See analysis details on SonarQube Cloud

@thomaspoignant thomaspoignant merged commit 1c9292d into thomaspoignant:main Nov 22, 2024
21 of 22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

(feature) evaluation command line for local testing
2 participants