diff --git a/tools/goctl/api/gogen/genroutes.go b/tools/goctl/api/gogen/genroutes.go index 6d9295f7..9770a57e 100644 --- a/tools/goctl/api/gogen/genroutes.go +++ b/tools/goctl/api/gogen/genroutes.go @@ -139,12 +139,7 @@ rest.WithPrefix("%s"),`, g.prefix) return err } - // why we check this, maybe some users set value 1, it's 1ns, not 1s. - if duration < timeoutThreshold { - return fmt.Errorf("timeout should not less than 1ms, now %v", duration) - } - - timeout = fmt.Sprintf("\n rest.WithTimeout(%d * time.Millisecond),", duration.Milliseconds()) + timeout = fmt.Sprintf("\n rest.WithTimeout(%s),", formatDuration(duration)) hasTimeout = true } @@ -211,6 +206,16 @@ rest.WithPrefix("%s"),`, g.prefix) }) } +func formatDuration(duration time.Duration) string { + if duration < time.Microsecond { + return fmt.Sprintf("%d * time.Nanosecond", duration.Nanoseconds()) + } + if duration < time.Millisecond { + return fmt.Sprintf("%d * time.Microsecond", duration.Microseconds()) + } + return fmt.Sprintf("%d * time.Millisecond", duration.Milliseconds()) +} + func genRouteImports(parentPkg string, api *spec.ApiSpec) string { importSet := collection.NewSet() importSet.AddStr(fmt.Sprintf("\"%s\"", pathx.JoinPackages(parentPkg, contextDir))) diff --git a/tools/goctl/api/gogen/genroutes_test.go b/tools/goctl/api/gogen/genroutes_test.go new file mode 100644 index 00000000..9aac4182 --- /dev/null +++ b/tools/goctl/api/gogen/genroutes_test.go @@ -0,0 +1,27 @@ +package gogen + +import ( + "testing" + "time" +) + +func Test_formatDuration(t *testing.T) { + tests := []struct { + duration time.Duration + expected string + }{ + {0, "0 * time.Nanosecond"}, + {time.Nanosecond, "1 * time.Nanosecond"}, + {100 * time.Nanosecond, "100 * time.Nanosecond"}, + {500 * time.Microsecond, "500 * time.Microsecond"}, + {2 * time.Millisecond, "2 * time.Millisecond"}, + {time.Second, "1000 * time.Millisecond"}, + } + + for _, test := range tests { + result := formatDuration(test.duration) + if result != test.expected { + t.Errorf("formatDuration(%v) = %v; want %v", test.duration, result, test.expected) + } + } +} diff --git a/tools/goctl/internal/version/version.go b/tools/goctl/internal/version/version.go index ce2225ec..bdbbc628 100644 --- a/tools/goctl/internal/version/version.go +++ b/tools/goctl/internal/version/version.go @@ -6,7 +6,7 @@ import ( ) // BuildVersion is the version of goctl. -const BuildVersion = "1.7.3" +const BuildVersion = "1.7.4" var tag = map[string]int{"pre-alpha": 0, "alpha": 1, "pre-bata": 2, "beta": 3, "released": 4, "": 5} diff --git a/tools/goctl/pkg/parser/api/scanner/scanner.go b/tools/goctl/pkg/parser/api/scanner/scanner.go index 7aac0056..e1989104 100644 --- a/tools/goctl/pkg/parser/api/scanner/scanner.go +++ b/tools/goctl/pkg/parser/api/scanner/scanner.go @@ -29,8 +29,6 @@ const ( // string mode end ) -var missingInput = errors.New("missing input") - type mode int // Scanner is a lexical scanner. @@ -629,7 +627,7 @@ func NewScanner(filename string, src interface{}) (*Scanner, error) { } if len(data) == 0 { - return nil, missingInput + return nil, fmt.Errorf("filename: %s,missing input", filename) } var runeList []rune diff --git a/tools/goctl/pkg/parser/api/scanner/scanner_test.go b/tools/goctl/pkg/parser/api/scanner/scanner_test.go index 5e2f1956..42d20ff4 100644 --- a/tools/goctl/pkg/parser/api/scanner/scanner_test.go +++ b/tools/goctl/pkg/parser/api/scanner/scanner_test.go @@ -62,13 +62,13 @@ func TestNewScanner(t *testing.T) { { filename: "foo", src: "", - expected: missingInput, + expected: "missing input", }, } for _, v := range testData { s, err := NewScanner(v.filename, v.src) if err != nil { - assert.Equal(t, v.expected.(error).Error(), err.Error()) + assert.Contains(t, err.Error(), v.expected) } else { assert.Equal(t, v.expected, s.filename) }