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

Modify optional flag to use generics #14645

Closed
wants to merge 2 commits into from

Conversation

Its-Maniaco
Copy link
Contributor

Description

Fixes #11154

Checklist

  • "Backport to:" labels have been added if this change should be back-ported
  • Tests were added or are not required
  • Did the new or modified tests pass consistently locally and on the CI
  • Documentation was added or is not required

@Its-Maniaco Its-Maniaco requested a review from deepthi as a code owner November 30, 2023 11:59
Copy link
Contributor

vitess-bot bot commented Nov 30, 2023

Review Checklist

Hello reviewers! 👋 Please follow this checklist when reviewing this Pull Request.

General

  • Ensure that the Pull Request has a descriptive title.
  • Ensure there is a link to an issue (except for internal cleanup and flaky test fixes), new features should have an RFC that documents use cases and test cases.

Tests

  • Bug fixes should have at least one unit or end-to-end test, enhancement and new features should have a sufficient number of tests.

Documentation

  • Apply the release notes (needs details) label if users need to know about this change.
  • New features should be documented.
  • There should be some code comments as to why things are implemented the way they are.
  • There should be a comment at the top of each new or modified test to explain what the test does.

New flags

  • Is this flag really necessary?
  • Flag names must be clear and intuitive, use dashes (-), and have a clear help text.

If a workflow is added or modified:

  • Each item in Jobs should be named in order to mark it as required.
  • If the workflow needs to be marked as required, the maintainer team must be notified.

Backward compatibility

  • Protobuf changes should be wire-compatible.
  • Changes to _vt tables and RPCs need to be backward compatible.
  • RPC changes should be compatible with vitess-operator
  • If a flag is removed, then it should also be removed from vitess-operator and arewefastyet, if used there.
  • vtctl command output order should be stable and awk-able.

@vitess-bot vitess-bot bot added NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work NeedsIssue A linked issue is missing for this Pull Request NeedsWebsiteDocsUpdate What it says labels Nov 30, 2023
@github-actions github-actions bot added this to the v19.0.0 milestone Nov 30, 2023
@frouioui frouioui requested a review from ajm188 November 30, 2023 14:51
@frouioui frouioui added Type: Enhancement Logical improvement (somewhere between a bug and feature) Component: CLI and removed NeedsWebsiteDocsUpdate What it says NeedsIssue A linked issue is missing for this Pull Request labels Nov 30, 2023
@frouioui
Copy link
Member

Hello @Its-Maniaco thank you for this first contribution! The DCO check is failing, can you please fix it using:

git rebase HEAD~1 --signoff
git push --force-with-lease origin optionalflag

From https://github.com/vitessio/vitess/pull/14645/checks?check_run_id=19177278801

type OptionalType struct {
val any
set bool
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should just be

Suggested change
type OptionalType struct {
val any
set bool
}
type Optional[T any] struct {
val T
set bool
}

and then rewriting the rest to match this type definition

Copy link
Contributor Author

Choose a reason for hiding this comment

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

func (t OptionalType[any]) Set(arg string) error {
	tType := t.String()
	switch tType {
	case "float64":
		var temp interface{}
		temp, err := strconv.ParseFloat(arg, 64)
		if err != nil {
			return numError(err)
		}
		t.val = temp.(any)
	case "string":
		var temp interface{}
		temp = arg
		t.val = temp.(any)
	}
	t.set = true
	return nil
}

I was having trouble directly setting t.val because of type mismatch. So I came up with above workaround.
Please let me know if this is the correct approach.

Copy link
Contributor

Choose a reason for hiding this comment

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

it's on the right track. i think you should be able to do just this, for example:

switch tType {
case "float64":
    temp, err := strconv.ParseFloat(...)
    if err != nil { ... }
    t.val = temp.(any)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried that earlier but was given InvalidAssert.
invalid operation: temp (variable of type float64) is not an interface
Same error when I did:
t.val = arg.(any)

Copy link
Contributor

Choose a reason for hiding this comment

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

ah then i guess you still need the declaration (but you should be able to declare as any rather than the older interface{})

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry but I could not figure out what is wrong. Below is a screenshot.
image

Copy link
Contributor

Choose a reason for hiding this comment

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

i don't think from the code you've shown that you have the right definition for Optional. does it match what i suggested here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I already rewrote it based on your suggestion.
image

Copy link
Contributor

Choose a reason for hiding this comment

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

this approach seems to work: https://go.dev/play/p/RrQxZeRXD2a

@Its-Maniaco Its-Maniaco force-pushed the optionalflag branch 2 times, most recently from e612bfb to a97a2f0 Compare December 21, 2023 09:17
Signed-off-by: Divya Pamecha <[email protected]>
Signed-off-by: Divya Pamecha <[email protected]>
go/flagutil/optional.go Outdated Show resolved Hide resolved
Co-authored-by: Andrew Mason <[email protected]>
Signed-off-by: Maniaco <[email protected]>
Copy link
Contributor

This PR is being marked as stale because it has been open for 30 days with no activity. To rectify, you may do any of the following:

  • Push additional commits to the associated branch.
  • Remove the stale label.
  • Add a comment indicating why it is not stale.

If no action is taken within 7 days, this PR will be closed.

@github-actions github-actions bot added the Stale Marks PRs as stale after a period of inactivity, which are then closed after a grace period. label Jan 26, 2024
@Its-Maniaco Its-Maniaco requested a review from ajm188 January 26, 2024 06:53
@github-actions github-actions bot removed the Stale Marks PRs as stale after a period of inactivity, which are then closed after a grace period. label Jan 27, 2024
@frouioui frouioui modified the milestones: v19.0.0, v20.0.0 Feb 6, 2024
Copy link
Contributor

github-actions bot commented Mar 8, 2024

This PR is being marked as stale because it has been open for 30 days with no activity. To rectify, you may do any of the following:

  • Push additional commits to the associated branch.
  • Remove the stale label.
  • Add a comment indicating why it is not stale.

If no action is taken within 7 days, this PR will be closed.

@github-actions github-actions bot added the Stale Marks PRs as stale after a period of inactivity, which are then closed after a grace period. label Mar 8, 2024
Copy link
Contributor

This PR was closed because it has been stale for 7 days with no activity.

@github-actions github-actions bot closed this Mar 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: CLI NeedsDescriptionUpdate The description is not clear or comprehensive enough, and needs work Stale Marks PRs as stale after a period of inactivity, which are then closed after a grace period. Type: Enhancement Logical improvement (somewhere between a bug and feature)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rewrite flagutil.OptionalFlag to use generics
3 participants