|
|
|
@ -1,6 +1,7 @@ |
|
|
|
|
package main |
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
"fmt" |
|
|
|
|
"strings" |
|
|
|
|
|
|
|
|
|
"github.com/go-kratos/kratos/cmd/protoc-gen-go-errors/v2/errors" |
|
|
|
@ -15,13 +16,7 @@ const ( |
|
|
|
|
|
|
|
|
|
// generateFile generates a _http.pb.go file containing kratos errors definitions.
|
|
|
|
|
func generateFile(gen *protogen.Plugin, file *protogen.File) *protogen.GeneratedFile { |
|
|
|
|
var hasCode bool |
|
|
|
|
for _, enum := range file.Enums { |
|
|
|
|
if code := defaultErrorCode(enum); code > 0 { |
|
|
|
|
hasCode = true |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if len(file.Enums) == 0 || !hasCode { |
|
|
|
|
if len(file.Enums) == 0 { |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
filename := file.GeneratedFilenamePrefix + "_errors.pb.go" |
|
|
|
@ -45,42 +40,59 @@ func generateFileContent(gen *protogen.Plugin, file *protogen.File, g *protogen. |
|
|
|
|
g.P("// is compatible with the kratos package it is being compiled against.") |
|
|
|
|
g.P("const _ = ", errorsPackage.Ident("SupportPackageIsVersion1")) |
|
|
|
|
g.P() |
|
|
|
|
index := 0 |
|
|
|
|
for _, enum := range file.Enums { |
|
|
|
|
genErrorsReason(gen, file, g, enum) |
|
|
|
|
skip := genErrorsReason(gen, file, g, enum) |
|
|
|
|
if skip == false { |
|
|
|
|
index++ |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
// If all enums do not contain 'errors.code', the current file is skipped
|
|
|
|
|
if index == 0 { |
|
|
|
|
g.Skip() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func defaultErrorCode(enum *protogen.Enum) int { |
|
|
|
|
func genErrorsReason(gen *protogen.Plugin, file *protogen.File, g *protogen.GeneratedFile, enum *protogen.Enum) bool { |
|
|
|
|
defaultCode := proto.GetExtension(enum.Desc.Options(), errors.E_DefaultCode) |
|
|
|
|
if code, ok := defaultCode.(int32); ok && code > 0 { |
|
|
|
|
return int(code) |
|
|
|
|
code := 0 |
|
|
|
|
if v, ok := defaultCode.(int32); ok { |
|
|
|
|
code = int(v) |
|
|
|
|
} |
|
|
|
|
if code > 600 || code < 0 { |
|
|
|
|
panic(fmt.Sprintf("Enum '%s' range must be greater than 0 and less than or equal to 600", string(enum.Desc.Name()))) |
|
|
|
|
} |
|
|
|
|
return 0 |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func genErrorsReason(gen *protogen.Plugin, file *protogen.File, g *protogen.GeneratedFile, enum *protogen.Enum) { |
|
|
|
|
var ew errorWrapper |
|
|
|
|
defCode := defaultErrorCode(enum) |
|
|
|
|
for _, v := range enum.Values { |
|
|
|
|
code := int(proto.GetExtension(v.Desc.Options(), errors.E_Code).(int32)) |
|
|
|
|
if code == 0 { |
|
|
|
|
code = defCode |
|
|
|
|
enumCode := code |
|
|
|
|
eCode := proto.GetExtension(v.Desc.Options(), errors.E_Code) |
|
|
|
|
if v, ok := eCode.(int32); ok { |
|
|
|
|
enumCode = int(v) |
|
|
|
|
} |
|
|
|
|
if code > 600 || code < 200 { |
|
|
|
|
panic("httpstatus code must be greater than or equal to 200 and less than 600") |
|
|
|
|
// If the current enumeration does not contain 'errors.code'
|
|
|
|
|
//or the code value exceeds the range, the current enum will be skipped
|
|
|
|
|
if enumCode > 600 || enumCode < 0 { |
|
|
|
|
panic(fmt.Sprintf("Enum '%s' range must be greater than 0 and less than or equal to 600", string(v.Desc.Name()))) |
|
|
|
|
} |
|
|
|
|
if enumCode == 0 { |
|
|
|
|
continue |
|
|
|
|
} |
|
|
|
|
err := &errorInfo{ |
|
|
|
|
Name: string(enum.Desc.Name()), |
|
|
|
|
Value: string(v.Desc.Name()), |
|
|
|
|
CamelValue: case2Camel(string(v.Desc.Name())), |
|
|
|
|
HttpCode: code, |
|
|
|
|
CamelValue: Case2Camel(string(v.Desc.Name())), |
|
|
|
|
HttpCode: enumCode, |
|
|
|
|
} |
|
|
|
|
ew.Errors = append(ew.Errors, err) |
|
|
|
|
} |
|
|
|
|
if len(ew.Errors) == 0 { |
|
|
|
|
return true |
|
|
|
|
} |
|
|
|
|
g.P(ew.execute()) |
|
|
|
|
return false |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func case2Camel(name string) string { |
|
|
|
|
func Case2Camel(name string) string { |
|
|
|
|
if !strings.Contains(name, "_") { |
|
|
|
|
return name |
|
|
|
|
} |
|
|
|
|