github.com/aacfactory/fns@v1.2.86-0.20240310083819-80d667fc0a17/services/documents/validation.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 "sort" 22 "strings" 23 ) 24 25 type I18n struct { 26 Name string `json:"name,omitempty" avro:"name"` 27 Value string `json:"value,omitempty" avro:"value"` 28 } 29 30 type I18ns []I18n 31 32 func (pp I18ns) Len() int { 33 return len(pp) 34 } 35 36 func (pp I18ns) Less(i, j int) bool { 37 return strings.Compare(pp[i].Name, pp[j].Name) < 0 38 } 39 40 func (pp I18ns) Swap(i, j int) { 41 pp[i], pp[j] = pp[j], pp[i] 42 } 43 44 func (pp I18ns) Add(name string, value string) I18ns { 45 n := append(pp, I18n{ 46 Name: name, 47 Value: value, 48 }) 49 sort.Sort(n) 50 return n 51 } 52 53 func (pp I18ns) Get(name string) (p string, has bool) { 54 for _, i18n := range pp { 55 if i18n.Name == name { 56 p = i18n.Value 57 has = true 58 return 59 } 60 } 61 return 62 } 63 64 func NewValidation(name string) Validation { 65 return Validation{ 66 Enable: true, 67 Name: name, 68 I18ns: make(I18ns, 0, 1), 69 } 70 } 71 72 type Validation struct { 73 Enable bool `json:"enable,omitempty" avro:"enable"` 74 Name string `json:"name,omitempty" avro:"name"` 75 I18ns I18ns `json:"i18ns,omitempty" avro:"i18Ns"` 76 } 77 78 func (validation Validation) AddI18n(name string, value string) Validation { 79 validation.I18ns = validation.I18ns.Add(name, value) 80 return validation 81 }