github.com/aacfactory/fns@v1.2.86-0.20240310083819-80d667fc0a17/services/documents/error.go (about) 1 /* 2 * Copyright 2023 Wang Min Xiang 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * 16 */ 17 18 package documents 19 20 import ( 21 "bufio" 22 "bytes" 23 "io" 24 "sort" 25 "strings" 26 ) 27 28 type ErrorDescription struct { 29 Name string `json:"name" avro:"name"` 30 Value string `json:"value" avro:"value"` 31 } 32 33 type ErrorDescriptions []ErrorDescription 34 35 func (pp ErrorDescriptions) Len() int { 36 return len(pp) 37 } 38 39 func (pp ErrorDescriptions) Less(i, j int) bool { 40 return strings.Compare(pp[i].Name, pp[j].Name) < 0 41 } 42 43 func (pp ErrorDescriptions) Swap(i, j int) { 44 pp[i], pp[j] = pp[j], pp[i] 45 } 46 47 func (pp ErrorDescriptions) Add(name string, value string) ErrorDescriptions { 48 n := append(pp, ErrorDescription{ 49 Name: name, 50 Value: value, 51 }) 52 sort.Sort(n) 53 return n 54 } 55 56 func (pp ErrorDescriptions) Get(name string) (v string, has bool) { 57 for _, description := range pp { 58 if description.Name == name { 59 v = description.Value 60 has = true 61 return 62 } 63 } 64 return 65 } 66 67 func NewError(name string) Error { 68 return Error{ 69 Name: name, 70 Descriptions: make(ErrorDescriptions, 0, 1), 71 } 72 } 73 74 type Error struct { 75 Name string `json:"name" avro:"name"` 76 Descriptions ErrorDescriptions `json:"descriptions" avro:"descriptions"` 77 } 78 79 func (err Error) AddNamedDescription(name string, value string) Error { 80 err.Descriptions = err.Descriptions.Add(name, value) 81 return err 82 } 83 84 func NewErrors(src string) Errors { 85 errs := make(Errors, 0, 1) 86 if len(src) == 0 { 87 return errs 88 } 89 reader := bufio.NewReader(bytes.NewReader([]byte(src))) 90 pos := -1 91 for { 92 line, _, readErr := reader.ReadLine() 93 if readErr == io.EOF { 94 break 95 } 96 line = bytes.TrimSpace(line) 97 if len(line) == 0 { 98 continue 99 } 100 idx := bytes.IndexByte(line, ':') 101 if idx < 0 { 102 errs = errs.Add(NewError(string(line))) 103 pos++ 104 continue 105 } 106 name := bytes.TrimSpace(line[0:idx]) 107 var value []byte 108 if len(line) > idx+1 { 109 value = bytes.TrimSpace(line[idx+1:]) 110 } 111 errs[pos] = errs[pos].AddNamedDescription(string(name), string(value)) 112 } 113 return errs 114 } 115 116 // Errors 117 // use @errors 118 // @errors >>> 119 // 120 // name1 121 // zh: chinese 122 // en: english 123 // name2 124 // zh: chinese 125 // en: english 126 // 127 // <<< 128 type Errors []Error 129 130 func (pp Errors) Len() int { 131 return len(pp) 132 } 133 134 func (pp Errors) Less(i, j int) bool { 135 return strings.Compare(pp[i].Name, pp[j].Name) < 0 136 } 137 138 func (pp Errors) Swap(i, j int) { 139 pp[i], pp[j] = pp[j], pp[i] 140 } 141 142 func (pp Errors) Add(e Error) Errors { 143 n := append(pp, e) 144 sort.Sort(n) 145 return n 146 }