Compare commits
2 Commits
Author | SHA1 | Date |
---|---|---|
chenzhihui | f9825cb900 | 2 years ago |
chenzhihui | 315d3048bf | 2 years ago |
@ -1,12 +0,0 @@ |
||||
daysUntilStale: 30 |
||||
daysUntilClose: 3 |
||||
exemptLabels: |
||||
- pinned |
||||
- security |
||||
- bug |
||||
staleLabel: wontfix |
||||
markComment: > |
||||
This issue has been automatically marked as stale because it has not had |
||||
recent activity. It will be closed if no further activity occurs. Thank you |
||||
for your contributions. |
||||
closeComment: true |
@ -0,0 +1,13 @@ |
||||
package cache |
||||
|
||||
import ( |
||||
"context" |
||||
"time" |
||||
) |
||||
|
||||
// Cache is a generic key value cache interface.
|
||||
type Cache interface { |
||||
Get(ctx context.Context, key string) (interface{}, error) |
||||
Put(ctx context.Context, key string, value interface{}, ttl time.Duration) error |
||||
Delete(ctx context.Context, key string) error |
||||
} |
@ -1,29 +0,0 @@ |
||||
//go:build linux
|
||||
// +build linux
|
||||
|
||||
package project |
||||
|
||||
import ( |
||||
"testing" |
||||
) |
||||
|
||||
func Test_processProjectParams(t *testing.T) { |
||||
type args struct { |
||||
projectName string |
||||
fallbackPlaceDir string |
||||
} |
||||
tests := []struct { |
||||
name string |
||||
args args |
||||
want string |
||||
}{ |
||||
{"absLinux", args{projectName: "/home/kratos/awesome/go/demo", fallbackPlaceDir: ""}, "/home/kratos/awesome/go"}, |
||||
} |
||||
for _, tt := range tests { |
||||
t.Run(tt.name, func(t *testing.T) { |
||||
if _, got := processProjectParams(tt.args.projectName, tt.args.fallbackPlaceDir); got != tt.want { |
||||
t.Errorf("processProjectParams() = %v, want %v", got, tt.want) |
||||
} |
||||
}) |
||||
} |
||||
} |
@ -1,144 +0,0 @@ |
||||
package project |
||||
|
||||
import ( |
||||
"fmt" |
||||
"go/parser" |
||||
"go/token" |
||||
"os" |
||||
"path/filepath" |
||||
"testing" |
||||
|
||||
"github.com/go-kratos/kratos/cmd/kratos/v2/internal/base" |
||||
) |
||||
|
||||
// TestCmdNew tests the `kratos new` command.
|
||||
func TestCmdNew(t *testing.T) { |
||||
cwd := changeCurrentDir(t) |
||||
projectName := "helloworld" |
||||
|
||||
// create a new project
|
||||
CmdNew.SetArgs([]string{projectName}) |
||||
if err := CmdNew.Execute(); err != nil { |
||||
t.Fatalf("executing command: %v", err) |
||||
} |
||||
|
||||
// check that the expected files were created
|
||||
for _, file := range []string{ |
||||
"go.mod", |
||||
"go.sum", |
||||
"README.md", |
||||
"cmd/helloworld/main.go", |
||||
} { |
||||
if _, err := os.Stat(filepath.Join(cwd, projectName, file)); err != nil { |
||||
t.Errorf("expected file %s to exist", file) |
||||
} |
||||
} |
||||
|
||||
// check that the go.mod file contains the expected module name
|
||||
assertGoMod(t, filepath.Join(cwd, projectName, "go.mod"), projectName) |
||||
|
||||
assertImportsInclude(t, filepath.Join(cwd, projectName, "cmd", projectName, "wire.go"), fmt.Sprintf(`"%s/internal/biz"`, projectName)) |
||||
} |
||||
|
||||
// TestCmdNewNoMod tests the `kratos new` command with the --nomod flag.
|
||||
func TestCmdNewNoMod(t *testing.T) { |
||||
cwd := changeCurrentDir(t) |
||||
|
||||
// create a new project
|
||||
CmdNew.SetArgs([]string{"project"}) |
||||
if err := CmdNew.Execute(); err != nil { |
||||
t.Fatalf("executing command: %v", err) |
||||
} |
||||
|
||||
// add new app with --nomod flag
|
||||
CmdNew.SetArgs([]string{"--nomod", "project/app/user"}) |
||||
if err := CmdNew.Execute(); err != nil { |
||||
t.Fatalf("executing command: %v", err) |
||||
} |
||||
|
||||
// check that the expected files were created
|
||||
for _, file := range []string{ |
||||
"go.mod", |
||||
"go.sum", |
||||
"README.md", |
||||
"cmd/project/main.go", |
||||
"app/user/cmd/user/main.go", |
||||
} { |
||||
if _, err := os.Stat(filepath.Join(cwd, "project", file)); err != nil { |
||||
t.Errorf("expected file %s to exist", file) |
||||
} |
||||
} |
||||
|
||||
assertImportsInclude(t, filepath.Join(cwd, "project/app/user/cmd/user/wire.go"), `"project/app/user/internal/biz"`) |
||||
} |
||||
|
||||
// assertImportsInclude checks that the file at path contains the expected import.
|
||||
func assertImportsInclude(t *testing.T, path, expected string) { |
||||
t.Helper() |
||||
|
||||
got, err := imports(path) |
||||
if err != nil { |
||||
t.Fatalf("getting imports: %v", err) |
||||
} |
||||
|
||||
for _, imp := range got { |
||||
if imp == expected { |
||||
return |
||||
} |
||||
} |
||||
|
||||
t.Errorf("expected imports to include %s, got %v", expected, got) |
||||
} |
||||
|
||||
// imports returns the imports in the file at path.
|
||||
func imports(path string) ([]string, error) { |
||||
fset := token.NewFileSet() |
||||
f, err := parser.ParseFile(fset, path, nil, parser.ImportsOnly) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
imports := make([]string, 0, len(f.Imports)) |
||||
for _, s := range f.Imports { |
||||
imports = append(imports, s.Path.Value) |
||||
} |
||||
|
||||
return imports, nil |
||||
} |
||||
|
||||
// assertGoMod checks that the go.mod file contains the expected module name.
|
||||
func assertGoMod(t *testing.T, path, expected string) { |
||||
t.Helper() |
||||
|
||||
got, err := base.ModulePath(path) |
||||
if err != nil { |
||||
t.Fatalf("getting module path: %v", err) |
||||
} |
||||
|
||||
if got != expected { |
||||
t.Errorf("expected module name %s, got %s", expected, got) |
||||
} |
||||
} |
||||
|
||||
// change the working directory to the tempdir
|
||||
func changeCurrentDir(t *testing.T) string { |
||||
t.Helper() |
||||
|
||||
tmp := t.TempDir() |
||||
|
||||
oldCWD, err := os.Getwd() |
||||
if err != nil { |
||||
t.Fatalf("getting working directory: %v", err) |
||||
} |
||||
|
||||
if err := os.Chdir(tmp); err != nil { |
||||
t.Fatalf("changing working directory: %v", err) |
||||
} |
||||
t.Cleanup(func() { |
||||
if err := os.Chdir(oldCWD); err != nil { |
||||
t.Fatalf("restoring working directory: %v", err) |
||||
} |
||||
}) |
||||
|
||||
return tmp |
||||
} |
@ -1,30 +0,0 @@ |
||||
//go:build windows
|
||||
// +build windows
|
||||
|
||||
package project |
||||
|
||||
import ( |
||||
"testing" |
||||
) |
||||
|
||||
func Test_processProjectParams(t *testing.T) { |
||||
type args struct { |
||||
projectName string |
||||
fallbackPlaceDir string |
||||
} |
||||
tests := []struct { |
||||
name string |
||||
args args |
||||
want string |
||||
}{ |
||||
{"absWindows", args{projectName: "c:\\kratos\\awesome\\go\\demo", fallbackPlaceDir: ""}, "c:\\kratos\\awesome\\go"}, |
||||
//{"relativeWindows", args{projectName: "/home/kratos/awesome/go/demo", fallbackPlaceDir: ""}, "/home/kratos/awesome/go"},
|
||||
} |
||||
for _, tt := range tests { |
||||
t.Run(tt.name, func(t *testing.T) { |
||||
if _, got := processProjectParams(tt.args.projectName, tt.args.fallbackPlaceDir); got != tt.want { |
||||
t.Errorf("getProjectPlaceDir() = %v, want %v", got, tt.want) |
||||
} |
||||
}) |
||||
} |
||||
} |
@ -1,4 +1,4 @@ |
||||
package main |
||||
|
||||
// release is the current kratos tool version.
|
||||
const release = "v2.6.3" |
||||
const release = "v2.5.1" |
||||
|
@ -1,17 +0,0 @@ |
||||
{{ range .Errors }} |
||||
|
||||
{{ if .HasComment }}{{ .Comment }}{{ end -}} |
||||
func Is{{.CamelValue}}(err error) bool { |
||||
if err == nil { |
||||
return false |
||||
} |
||||
e := errors.FromError(err) |
||||
return e.Reason == {{ .Name }}_{{ .Value }}.String() && e.Code == {{ .HTTPCode }} |
||||
} |
||||
|
||||
{{ if .HasComment }}{{ .Comment }}{{ end -}} |
||||
func Error{{ .CamelValue }}(format string, args ...interface{}) *errors.Error { |
||||
return errors.New({{ .HTTPCode }}, {{ .Name }}_{{ .Value }}.String(), fmt.Sprintf(format, args...)) |
||||
} |
||||
|
||||
{{- end }} |
@ -1,4 +1,4 @@ |
||||
package main |
||||
|
||||
// release is the current protoc-gen-go-errors version.
|
||||
const release = "v2.6.3" |
||||
const release = "v2.5.1" |
||||
|
@ -1,93 +0,0 @@ |
||||
{{$svrType := .ServiceType}} |
||||
{{$svrName := .ServiceName}} |
||||
|
||||
{{- range .MethodSets}} |
||||
const Operation{{$svrType}}{{.OriginalName}} = "/{{$svrName}}/{{.OriginalName}}" |
||||
{{- end}} |
||||
|
||||
type {{.ServiceType}}HTTPServer interface { |
||||
{{- range .MethodSets}} |
||||
{{- if ne .Comment ""}} |
||||
{{.Comment}} |
||||
{{- end}} |
||||
{{.Name}}(context.Context, *{{.Request}}) (*{{.Reply}}, error) |
||||
{{- end}} |
||||
} |
||||
|
||||
func Register{{.ServiceType}}HTTPServer(s *http.Server, srv {{.ServiceType}}HTTPServer) { |
||||
r := s.Route("/") |
||||
{{- range .Methods}} |
||||
r.{{.Method}}("{{.Path}}", _{{$svrType}}_{{.Name}}{{.Num}}_HTTP_Handler(srv)) |
||||
{{- end}} |
||||
} |
||||
|
||||
{{range .Methods}} |
||||
func _{{$svrType}}_{{.Name}}{{.Num}}_HTTP_Handler(srv {{$svrType}}HTTPServer) func(ctx http.Context) error { |
||||
return func(ctx http.Context) error { |
||||
var in {{.Request}} |
||||
{{- if .HasBody}} |
||||
if err := ctx.Bind(&in{{.Body}}); err != nil { |
||||
return err |
||||
} |
||||
|
||||
{{- if not (eq .Body "")}} |
||||
if err := ctx.BindQuery(&in); err != nil { |
||||
return err |
||||
} |
||||
{{- end}} |
||||
{{- else}} |
||||
if err := ctx.BindQuery(&in{{.Body}}); err != nil { |
||||
return err |
||||
} |
||||
{{- end}} |
||||
{{- if .HasVars}} |
||||
if err := ctx.BindVars(&in); err != nil { |
||||
return err |
||||
} |
||||
{{- end}} |
||||
http.SetOperation(ctx,Operation{{$svrType}}{{.OriginalName}}) |
||||
h := ctx.Middleware(func(ctx context.Context, req interface{}) (interface{}, error) { |
||||
return srv.{{.Name}}(ctx, req.(*{{.Request}})) |
||||
}) |
||||
out, err := h(ctx, &in) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
reply := out.(*{{.Reply}}) |
||||
return ctx.Result(200, reply{{.ResponseBody}}) |
||||
} |
||||
} |
||||
{{end}} |
||||
|
||||
type {{.ServiceType}}HTTPClient interface { |
||||
{{- range .MethodSets}} |
||||
{{.Name}}(ctx context.Context, req *{{.Request}}, opts ...http.CallOption) (rsp *{{.Reply}}, err error) |
||||
{{- end}} |
||||
} |
||||
|
||||
type {{.ServiceType}}HTTPClientImpl struct{ |
||||
cc *http.Client |
||||
} |
||||
|
||||
func New{{.ServiceType}}HTTPClient (client *http.Client) {{.ServiceType}}HTTPClient { |
||||
return &{{.ServiceType}}HTTPClientImpl{client} |
||||
} |
||||
|
||||
{{range .MethodSets}} |
||||
func (c *{{$svrType}}HTTPClientImpl) {{.Name}}(ctx context.Context, in *{{.Request}}, opts ...http.CallOption) (*{{.Reply}}, error) { |
||||
var out {{.Reply}} |
||||
pattern := "{{.Path}}" |
||||
path := binding.EncodeURL(pattern, in, {{not .HasBody}}) |
||||
opts = append(opts, http.Operation(Operation{{$svrType}}{{.OriginalName}})) |
||||
opts = append(opts, http.PathTemplate(pattern)) |
||||
{{if .HasBody -}} |
||||
err := c.cc.Invoke(ctx, "{{.Method}}", path, in{{.Body}}, &out{{.ResponseBody}}, opts...) |
||||
{{else -}} |
||||
err := c.cc.Invoke(ctx, "{{.Method}}", path, nil, &out{{.ResponseBody}}, opts...) |
||||
{{end -}} |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
return &out, err |
||||
} |
||||
{{end}} |
@ -1,4 +1,4 @@ |
||||
package main |
||||
|
||||
// release is the current protoc-gen-go-http version.
|
||||
const release = "v2.6.3" |
||||
const release = "v2.5.1" |
||||
|
@ -1,90 +0,0 @@ |
||||
package apollo |
||||
|
||||
import ( |
||||
"testing" |
||||
) |
||||
|
||||
func Test_genKey(t *testing.T) { |
||||
type args struct { |
||||
ns string |
||||
sub string |
||||
} |
||||
tests := []struct { |
||||
name string |
||||
args args |
||||
want string |
||||
}{ |
||||
{ |
||||
name: "blank namespace", |
||||
args: args{ |
||||
ns: "", |
||||
sub: "x.y", |
||||
}, |
||||
want: "x.y", |
||||
}, |
||||
{ |
||||
name: "properties namespace", |
||||
args: args{ |
||||
ns: "application", |
||||
sub: "x.y", |
||||
}, |
||||
want: "application.x.y", |
||||
}, |
||||
{ |
||||
name: "namespace with format", |
||||
args: args{ |
||||
ns: "app.yaml", |
||||
sub: "x.y", |
||||
}, |
||||
want: "app.x.y", |
||||
}, |
||||
} |
||||
for _, tt := range tests { |
||||
t.Run(tt.name, func(t *testing.T) { |
||||
if got := genKey(tt.args.ns, tt.args.sub); got != tt.want { |
||||
t.Errorf("genKey() = %v, want %v", got, tt.want) |
||||
} |
||||
}) |
||||
} |
||||
} |
||||
|
||||
func Test_format(t *testing.T) { |
||||
tests := []struct { |
||||
name string |
||||
namespace string |
||||
want string |
||||
}{ |
||||
{ |
||||
name: "properties namespace", |
||||
namespace: "application", |
||||
want: "json", |
||||
}, |
||||
{ |
||||
name: "properties namespace #1", |
||||
namespace: "app.setting", |
||||
want: "json", |
||||
}, |
||||
{ |
||||
name: "namespace with format[yaml]", |
||||
namespace: "app.yaml", |
||||
want: "yaml", |
||||
}, |
||||
{ |
||||
name: "namespace with format[yml]", |
||||
namespace: "app.yml", |
||||
want: "yml", |
||||
}, |
||||
{ |
||||
name: "namespace with format[json]", |
||||
namespace: "app.json", |
||||
want: "json", |
||||
}, |
||||
} |
||||
for _, tt := range tests { |
||||
t.Run(tt.name, func(t *testing.T) { |
||||
if got := format(tt.namespace); got != tt.want { |
||||
t.Errorf("format() = %v, want %v", got, tt.want) |
||||
} |
||||
}) |
||||
} |
||||
} |
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue