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

x/tools/cmd/stringer: support for hex values #38491

Closed
maltegrosse opened this issue Apr 16, 2020 · 2 comments
Closed

x/tools/cmd/stringer: support for hex values #38491

maltegrosse opened this issue Apr 16, 2020 · 2 comments
Labels
FrozenDueToAge Tools This label describes issues relating to any tools in the x/tools repository. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Milestone

Comments

@maltegrosse
Copy link

Stringer works perfect on on multiple types, but using a hex value does not return the const name by giving integer as input.

What version of Go are you using (go version)?

$ go version 1.13

Does this issue reproduce with the latest release?

Feature Request, yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
...
set GOHOSTARCH=amd64
set GOHOSTOS=windows
...

What did you do?

type SomeState uint32

//go:generate stringer -type=SomeState
const (
	StateOne   SomeState = 0x01 // dec 1
	StateTwo   SomeState = 0x02 // dec 2
	StateThree SomeState = 0x03 // dec 3
)

returns generated:

// Code generated by "stringer -type=SomeState"; DO NOT EDIT.

package go_mytest

import "strconv"

func _() {
	// An "invalid array index" compiler error signifies that the constant values have changed.
	// Re-run the stringer command to generate them again.
	var x [1]struct{}
	_ = x[StateOne-1]
	_ = x[StateTwo-2]
	_ = x[StateThree-3]
}

const _SomeState_name = "StateOneStateTwoStateThree"

var _SomeState_index = [...]uint8{0, 8, 16, 26}

func (i SomeState) String() string {
	i -= 1
	if i >= SomeState(len(_SomeState_index)-1) {
		return "SomeState(" + strconv.FormatInt(int64(i+1), 10) + ")"
	}
	return _SomeState_name[_SomeState_index[i]:_SomeState_index[i+1]]
}

What did you expect to see?

fmt.Println(SomeState(1))

should return

"StateOne"

What did you see instead?

SomeState(1)
@gopherbot gopherbot added this to the Unreleased milestone Apr 16, 2020
@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Apr 16, 2020
@robpike
Copy link
Contributor

robpike commented Apr 16, 2020

It works fine for me. Are you including somestate_stringer.go in your build? You can test this by trying

fmt.Println(SomeState(1).String())

I think this is what's wrong, and if you try this variant it won't compile because the String method is not compiled into the binary.

@robpike robpike added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Apr 16, 2020
@maltegrosse
Copy link
Author

maltegrosse commented Apr 17, 2020

Thank you for your quick reply. I double checked using .String() without any success.

I currently use it for my dbus modem manager wrapper (work in progress)

https://github.com/maltegrosse/go-modemmanager

It works fine for all enums (and these are alooot :) except of:

https://github.com/maltegrosse/go-modemmanager/blob/master/enums.go#L424
which includes hex numbers. (the others including hex numbers havent tested yet)

I am not sure if stringer messed something up or my limited coding skills are the reason for that strange behavior. :)

perhaps a quick go get and trying
fmt.Println(modemmanager.MMSmsDeliveryState(256)) fmt.Println(modemmanager.MMSmsDeliveryState(256).String())
would help to clarify this.

edit: really strange, it works for other numbers except of 256 ( 0x100 )

edit: sorry, my fault, limited coding skills overseen to add the type...

@golang golang locked and limited conversation to collaborators Apr 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge Tools This label describes issues relating to any tools in the x/tools repository. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

3 participants