Skip to content

proposal: x/tools/cmd/stringer: add -scanner #71041

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

Closed
TonyRippy opened this issue Dec 27, 2024 · 3 comments
Closed

proposal: x/tools/cmd/stringer: add -scanner #71041

TonyRippy opened this issue Dec 27, 2024 · 3 comments
Labels
Milestone

Comments

@TonyRippy
Copy link

Proposal Details

In addition to being able go from a type to a string, it would be useful to parse a string back into a type value.

I propose adding a new -scanner option that would tell the stringer tool to also generate a fmt.Scanner implementation that accepts the "%s" verb. It makes sense to include this as part of x/tools/cmd/stringer because the implmentation can make use of the generated _Type_name and _Type_index array.

An example of use:

import "fmt"

type MyType uint64
const (
  One MyType = iota
  Two
  Three
)
//go:generate stringer -type=MyType -scanner

func Example(v MyType) {
  stringerOutput := fmt.Sprintf("<%s>", v)
  var v2 MyType
  _, err := fmt.Sscanf(stringerOutput, "<%s>", &v2)
  if err != nil {
    panic(err)
  }
  if v2 != v {
    panic("the values should match")
  }
} 

I have an implementation that I have used in a private project, and I would like to contribute it back to the Go project. This proposal would provide a solution for the earlier request #60228.

@gopherbot gopherbot added this to the Proposal milestone Dec 27, 2024
@gabyhelp
Copy link

Related Issues

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

@TonyRippy
Copy link
Author

Wow, that "Related Issues" search is handy... I swear I looked through the existing issues, but somehow didn't see that one.

@seankhliao
Copy link
Member

Duplicate of #23535

@seankhliao seankhliao marked this as a duplicate of #23535 Dec 27, 2024
@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Dec 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants