this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

cmd/cue: get go: support time.Duration

Fix `get go` behavior with `time.Duration` to output valid CUE.
Convert the underlying numeric to string, and add the unit suffix so
it's compliant with CUE's `time.Duration`.

Add `Duration` to the `time` built-in check so we render as
`time.Duration` rather than the invalid `time.#Duration`.

Run the valid nanosecond `Duration` str through `time.ParseDuration` and
use its `String()` output so the CUE is human-readable.

Fixes #2969.

Signed-off-by: Greg Dallavalle <greg.dallavalle@gmail.com>
Change-Id: I4b69e30a2a679643ea80854fe2b01f5794d94b71
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1189718
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: CUEcueckoo <cueckoo@gmail.com>

authored by

Greg Dallavalle and committed by
Daniel Martí
1b9b0799 4db44817

+51 -2
+17 -2
cmd/cue/cmd/get_go.go
··· 29 29 "regexp" 30 30 "strconv" 31 31 "strings" 32 + "time" 32 33 "unicode" 33 34 34 35 "github.com/spf13/cobra" ··· 788 789 } 789 790 } 790 791 792 + typ := e.pkg.TypesInfo.TypeOf(name) 791 793 c := e.pkg.TypesInfo.Defs[v.Names[i]].(*types.Const) 792 794 sv := c.Val().ExactString() 795 + switch t := typ.(type) { 796 + case *types.Named: 797 + if t.Obj().Pkg().Path() == "time" && t.Obj().Name() == "Duration" { 798 + // time.Duration is an int64 representing nanoseconds, 799 + // but CUE only accepts strings with units as accepted by time.ParseDuration. 800 + durationWithUnit := sv + "ns" 801 + // Roundtrip the constant through time.Duration.String for human readability. 802 + duration, err := time.ParseDuration(durationWithUnit) 803 + if err != nil { 804 + panic(fmt.Errorf("time.ParseDuration(%s) failed: %v", durationWithUnit, err)) 805 + } 806 + sv = `"` + duration.String() + `"` 807 + } 808 + } 793 809 cv, err := parser.ParseExpr("", sv) 794 810 if err != nil { 795 811 panic(fmt.Errorf("failed to parse %v: %v", sv, err)) ··· 805 821 } 806 822 } 807 823 808 - typ := e.pkg.TypesInfo.TypeOf(name) 809 824 switch typ { 810 825 case typeByte, typeString, typeError: 811 826 default: ··· 1046 1061 } 1047 1062 // Check for builtin packages. 1048 1063 switch { 1049 - case obj.Pkg().Path() == "time" && obj.Name() == "Time": 1064 + case obj.Pkg().Path() == "time" && (obj.Name() == "Time" || obj.Name() == "Duration"): 1050 1065 ref := e.ident(e.pkgNames[obj.Pkg().Path()].name, false) 1051 1066 var name *cueast.Ident 1052 1067 if ref.Name != "time" {
+34
cmd/cue/cmd/testdata/script/issue2969.txtar
··· 1 + # Test for go get issue with time.Duration returning invalid numeric. 2 + exec cue get go --local ./... 3 + cmp ./pkg1/file1_go_gen.cue ./pkg1/file1_go_gen.cue.golden 4 + # Verify dependencies did not change 5 + cmp go.mod go.mod.golden 6 + 7 + -- go.mod -- 8 + module mod.test 9 + 10 + go 1.21 11 + -- go.mod.golden -- 12 + module mod.test 13 + 14 + go 1.21 15 + -- cue.mod/module.cue -- 16 + module: "mod.test" 17 + -- pkg1/file1.go -- 18 + package pkg1 19 + 20 + import "time" 21 + 22 + const ( 23 + Time1Second = time.Duration(time.Second) 24 + ) 25 + -- pkg1/file1_go_gen.cue.golden -- 26 + // Code generated by cue get go. DO NOT EDIT. 27 + 28 + //cue:generate cue get go mod.test/pkg1 29 + 30 + package pkg1 31 + 32 + import "time" 33 + 34 + #Time1Second: time.Duration & "1s"