github.com/facebookincubator/go-belt@v0.0.0-20230703220935-39cd348f1a38/pkg/field/field.go (about) 1 // Copyright 2022 Meta Platforms, Inc. and affiliates. 2 // 3 // Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 4 // 5 // 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 6 // 7 // 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 8 // 9 // 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 10 // 11 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 12 13 package field 14 15 // Key is an identifier of the Field. 16 type Key = string 17 18 // Value is a value stored within a Field. 19 type Value = any 20 21 // Field represents a single structured value. 22 type Field struct { 23 // Key is the identifier of the field. They are supposed to be unique, 24 // but this is not a mandatory requirement. Each tool implementation 25 // may treat duplicated keys differently. 26 Key Key 27 28 // Value is the value. 29 Value 30 31 // Properties defines Hook-specific options (how to interpret the specific field). 32 Properties Properties 33 } 34 35 var _ AbstractFields = (*Field)(nil) 36 37 // ForEachField implements AbstractFields. 38 func (f *Field) ForEachField(callback func(f *Field) bool) bool { 39 return callback(f) 40 } 41 42 // Len implements AbstractFields. 43 func (f *Field) Len() int { 44 return 1 45 } 46 47 // Fields is a slice of Field-s 48 type Fields []Field 49 50 var _ AbstractFields = (Fields)(nil) 51 52 // Copy returns a copy of the slice. 53 func (s Fields) Copy() Fields { 54 return s.WithPreallocate(0) 55 } 56 57 // WithPreallocate returns a copy of the slice with available additional capacity of size preallocateLen. 58 func (s Fields) WithPreallocate(preallocateLen uint) Fields { 59 cpy := make(Fields, len(s), len(s)+int(preallocateLen)) 60 copy(cpy, s) 61 return cpy 62 } 63 64 // Less implements sort.Interface 65 func (s Fields) Less(i, j int) bool { 66 return s[i].Key < s[j].Key 67 } 68 69 // ForEachField implements AbstractFields 70 func (s Fields) ForEachField(callback func(f *Field) bool) bool { 71 for idx := range s { 72 if !callback(&s[idx]) { 73 return false 74 } 75 } 76 return true 77 } 78 79 // Len implements sort.Interface 80 func (s Fields) Len() int { 81 return len(s) 82 }