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

Bug: Can not show time.Duration #11

Open
soluty opened this issue Apr 3, 2021 · 1 comment
Open

Bug: Can not show time.Duration #11

soluty opened this issue Apr 3, 2021 · 1 comment
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@soluty
Copy link

soluty commented Apr 3, 2021

time.Duration will cause ( missing ',' in argument list ) error
i wrote a hack and it can work well. change method gofumptFormatExpr

func gofumptFormatExpr(w io.Writer, fset *token.FileSet, expr ast.Expr, opt gofumpt.Options) error {
	// First use go/format to convert the expression to Go syntax.
	var tmp bytes.Buffer
	if err := format.Node(&tmp, fset, expr); err != nil {
		return err
	}
	rs := []rune(tmp.String())
	var firstNum int
	for i, v := range rs {
		if v == '(' {
			if rs[i+1] >= '0' && rs[i+1] <= '9' {
				firstNum = i + 1
				break
			}
		}
	}
	if firstNum > 0 {
		rs0 := rs[:firstNum]
		rs1 := rs[firstNum:]
		var hasChar bool
		for _, v := range rs1 {
			if v < '0' || v > '9' {
				hasChar = true
				break
			}
		}
		if hasChar {
			rs1 = append([]rune{'_'}, rs1...)
		}
		rs = append(rs0, rs1...)
	}
	tmpString := string(formatCompositeLiterals(rs))
@slimsag slimsag added bug Something isn't working help wanted Extra attention is needed labels Aug 8, 2021
@slimsag
Copy link
Member

slimsag commented Aug 8, 2021

Thanks for filing this, and sorry for the delayed follow-up. I think it would make sense to add support for time.Duration and time.Time natively. Happy to accept a PR for this if anyone has time and can add a few test cases alongside it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants