From 3aada93f62feadb08b89d931432e37e64c53a36f Mon Sep 17 00:00:00 2001 From: Lonng Date: Tue, 20 Oct 2020 17:29:10 +0800 Subject: [PATCH] Make the generated errors.toml more stable if duplicated code exist Signed-off-by: Lonng --- components/errdoc/errdoc-gen/main.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/components/errdoc/errdoc-gen/main.go b/components/errdoc/errdoc-gen/main.go index ceaf8582dd..42be8daf55 100644 --- a/components/errdoc/errdoc-gen/main.go +++ b/components/errdoc/errdoc-gen/main.go @@ -124,8 +124,7 @@ func main() { Description string Workaround string } - var sorted []spec - var dedup = map[string]struct{}{} + var dedup = map[string]spec{} for _, e := range allErrors { terr, ok := e.(*errors.Error) if !ok { @@ -133,12 +132,13 @@ func main() { } else { val := reflect.ValueOf(terr).Elem() codeText := val.FieldByName("codeText") - if _, found := dedup[codeText.String()]; found { + message := val.FieldByName("message") + if previous, found := dedup[codeText.String()]; found { println("Duplicated error code:", codeText.String()) - continue + if message.String() < previous.Message { + continue + } } - dedup[codeText.String()] = struct{}{} - message := val.FieldByName("message") description := val.FieldByName("description") workaround := val.FieldByName("workaround ") s := spec{ @@ -151,10 +151,14 @@ func main() { if workaround.IsValid() { s.Workaround = workaround.String() } - sorted = append(sorted, s) + dedup[codeText.String()] = s } } + var sorted []spec + for _, item := range dedup { + sorted = append(sorted, item) + } sort.Slice(sorted, func(i, j int) bool { // TiDB exits duplicated code if sorted[i].Code == sorted[j].Code {