Skip to content

Commit

Permalink
copy errors proto (#1030)
Browse files Browse the repository at this point in the history
  • Loading branch information
tonybase authored Jun 11, 2021
1 parent 736385c commit 828a7c5
Show file tree
Hide file tree
Showing 4 changed files with 265 additions and 13 deletions.
19 changes: 10 additions & 9 deletions cmd/protoc-gen-go-errors/errors.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package main

import (
pb "github.com/go-kratos/kratos/v2/errors"
"strings"

"github.com/go-kratos/kratos/cmd/protoc-gen-go-errors/v2/errors"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/proto"
"strings"
)

const (
Expand Down Expand Up @@ -44,7 +45,7 @@ func generateFileContent(gen *protogen.Plugin, file *protogen.File, g *protogen.
}

func genErrorsReason(gen *protogen.Plugin, file *protogen.File, g *protogen.GeneratedFile, enum *protogen.Enum) {
defaultCode := proto.GetExtension(enum.Desc.Options(), pb.E_DefaultCode)
defaultCode := proto.GetExtension(enum.Desc.Options(), errors.E_DefaultCode)
code := 0
if ok := defaultCode.(int32); ok != 0 && ok <= 600 && ok >= 200 {
code = int(ok)
Expand All @@ -53,7 +54,7 @@ func genErrorsReason(gen *protogen.Plugin, file *protogen.File, g *protogen.Gene
}
var ew errorWrapper
for _, v := range enum.Values {
eCode := proto.GetExtension(v.Desc.Options(), pb.E_Code)
eCode := proto.GetExtension(v.Desc.Options(), errors.E_Code)
enumCode := int(eCode.(int32))
if enumCode == 0 {
enumCode = code
Expand All @@ -62,22 +63,22 @@ func genErrorsReason(gen *protogen.Plugin, file *protogen.File, g *protogen.Gene
return
}
err := &errorInfo{
Name: string(enum.Desc.Name()),
Value: string(v.Desc.Name()),
Name: string(enum.Desc.Name()),
Value: string(v.Desc.Name()),
CamelValue: Case2Camel(string(v.Desc.Name())),
HttpCode: enumCode,
HttpCode: enumCode,
}
ew.Errors = append(ew.Errors, err)
}
g.P(ew.execute())
}

func Case2Camel(name string) string {
if !strings.Contains(name,"_") {
if !strings.Contains(name, "_") {
return name
}
name = strings.ToLower(name)
name = strings.Replace(name, "_", " ", -1)
name = strings.Title(name)
return strings.Replace(name, " ", "", -1)
}
}
229 changes: 229 additions & 0 deletions cmd/protoc-gen-go-errors/errors/errors.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions cmd/protoc-gen-go-errors/errors/errors.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
syntax = "proto3";

package errors;

option go_package = "github.com/go-kratos/kratos/v2/errors;errors";
option java_multiple_files = true;
option java_package = "com.github.kratos.errors";
option objc_class_prefix = "KratosErrors";

import "google/protobuf/descriptor.proto";

message Error {
int32 code = 1;
string reason = 2;
string message = 3;
map<string, string> metadata = 4;
};

extend google.protobuf.EnumOptions {
int32 default_code = 1108;
}

extend google.protobuf.EnumValueOptions {
int32 code = 1109;
}
5 changes: 1 addition & 4 deletions cmd/protoc-gen-go-errors/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@ module github.com/go-kratos/kratos/cmd/protoc-gen-go-errors/v2

go 1.15

require (
github.com/go-kratos/kratos/v2 v2.0.0-20210608085308-7585257f9ea1
google.golang.org/protobuf v1.26.0
)
require google.golang.org/protobuf v1.26.0

0 comments on commit 828a7c5

Please sign in to comment.