github.com/alibaba/ilogtail/pkg@v0.0.0-20250526110833-c53b480d046c/models/common.go (about)

     1  // Copyright 2022 iLogtail Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package models
    16  
    17  import (
    18  	"sort"
    19  )
    20  
    21  type EventType int
    22  
    23  const (
    24  	_ EventType = iota
    25  	EventTypeMetric
    26  	EventTypeSpan
    27  	EventTypeLogging
    28  	EventTypeByteArray
    29  )
    30  
    31  type ValueType int
    32  
    33  const (
    34  	_ ValueType = iota
    35  	ValueTypeString
    36  	ValueTypeBoolean
    37  	ValueTypeArray
    38  	ValueTypeMap
    39  
    40  	ContentKey = "content"
    41  	BodyKey    = ContentKey
    42  )
    43  
    44  var (
    45  	NilStringValues    = &keyValuesNil[string]{}
    46  	NilTypedValues     = &keyValuesNil[*TypedValue]{}
    47  	NilFloatValues     = &keyValuesNil[float64]{}
    48  	NilInterfaceValues = &keyValuesNil[interface{}]{}
    49  )
    50  
    51  type TypedValue struct {
    52  	Type  ValueType
    53  	Value interface{}
    54  }
    55  
    56  type KeyValue[TValue string | float64 | *TypedValue | any] struct {
    57  	Key   string
    58  	Value TValue
    59  }
    60  
    61  type KeyValueSlice[TValue string | float64 | *TypedValue | any] []KeyValue[TValue]
    62  
    63  func (x KeyValueSlice[TValue]) Len() int           { return len(x) }
    64  func (x KeyValueSlice[TValue]) Less(i, j int) bool { return x[i].Key < x[j].Key }
    65  func (x KeyValueSlice[TValue]) Swap(i, j int)      { x[i], x[j] = x[j], x[i] }
    66  
    67  type KeyValues[TValue string | float64 | *TypedValue | any] interface {
    68  	Add(key string, value TValue)
    69  
    70  	AddAll(items map[string]TValue)
    71  
    72  	Get(key string) TValue
    73  
    74  	Contains(key string) bool
    75  
    76  	Delete(key string)
    77  
    78  	Merge(other KeyValues[TValue])
    79  
    80  	Iterator() map[string]TValue
    81  
    82  	SortTo(buf []KeyValue[TValue]) []KeyValue[TValue]
    83  
    84  	Len() int
    85  
    86  	IsNil() bool
    87  }
    88  
    89  type Tags KeyValues[string]
    90  
    91  type Metadata KeyValues[string]
    92  
    93  type keyValuesImpl[TValue string | float64 | *TypedValue | any] struct {
    94  	keyValues map[string]TValue
    95  }
    96  
    97  func (kv *keyValuesImpl[TValue]) values() (map[string]TValue, bool) {
    98  	if kv == nil || kv.keyValues == nil {
    99  		return nil, false
   100  	}
   101  	return kv.keyValues, true
   102  }
   103  
   104  func (kv *keyValuesImpl[TValue]) Add(key string, value TValue) {
   105  	if values, ok := kv.values(); ok {
   106  		values[key] = value
   107  	}
   108  }
   109  
   110  func (kv *keyValuesImpl[TValue]) AddAll(items map[string]TValue) {
   111  	if values, ok := kv.values(); ok {
   112  		for key, value := range items {
   113  			values[key] = value
   114  		}
   115  	}
   116  }
   117  
   118  func (kv *keyValuesImpl[TValue]) Get(key string) TValue {
   119  	if values, ok := kv.values(); ok {
   120  		return values[key]
   121  	}
   122  	var null TValue
   123  	return null
   124  }
   125  
   126  func (kv *keyValuesImpl[TValue]) Contains(key string) bool {
   127  	if values, ok := kv.values(); ok {
   128  		_, valueOk := values[key]
   129  		return valueOk
   130  	}
   131  	return false
   132  }
   133  
   134  func (kv *keyValuesImpl[TValue]) Delete(key string) {
   135  	if _, ok := kv.values(); ok {
   136  		delete(kv.keyValues, key)
   137  	}
   138  }
   139  
   140  func (kv *keyValuesImpl[TValue]) Merge(other KeyValues[TValue]) {
   141  	if values, ok := kv.values(); ok {
   142  		for k, v := range other.Iterator() {
   143  			values[k] = v
   144  		}
   145  	}
   146  }
   147  
   148  func (kv *keyValuesImpl[TValue]) Iterator() map[string]TValue {
   149  	if values, ok := kv.values(); ok {
   150  		return values
   151  	}
   152  	return nil
   153  }
   154  
   155  func (kv *keyValuesImpl[TValue]) Len() int {
   156  	if values, ok := kv.values(); ok {
   157  		return len(values)
   158  	}
   159  	return 0
   160  }
   161  
   162  func (kv *keyValuesImpl[TValue]) IsNil() bool {
   163  	return false
   164  }
   165  
   166  func (kv *keyValuesImpl[TValue]) SortTo(buf []KeyValue[TValue]) []KeyValue[TValue] {
   167  	values, ok := kv.values()
   168  	if !ok {
   169  		buf = buf[:0]
   170  		return buf
   171  	}
   172  	if buf == nil {
   173  		buf = make([]KeyValue[TValue], 0, len(values))
   174  	} else {
   175  		buf = buf[:0]
   176  	}
   177  
   178  	for k, v := range values {
   179  		buf = append(buf, KeyValue[TValue]{Key: k, Value: v})
   180  	}
   181  	sort.Sort(KeyValueSlice[TValue](buf))
   182  	return buf
   183  }
   184  
   185  type keyValuesNil[TValue string | float64 | *TypedValue | any] struct {
   186  }
   187  
   188  func (kv *keyValuesNil[TValue]) Add(key string, value TValue) {
   189  }
   190  
   191  func (kv *keyValuesNil[TValue]) AddAll(items map[string]TValue) {
   192  }
   193  
   194  func (kv *keyValuesNil[TValue]) Get(key string) TValue {
   195  	var null TValue
   196  	return null
   197  }
   198  
   199  func (kv *keyValuesNil[TValue]) Contains(key string) bool {
   200  	return false
   201  }
   202  
   203  func (kv *keyValuesNil[TValue]) Delete(key string) {
   204  }
   205  
   206  func (kv *keyValuesNil[TValue]) Merge(other KeyValues[TValue]) {
   207  }
   208  
   209  func (kv *keyValuesNil[TValue]) Iterator() map[string]TValue {
   210  	return make(map[string]TValue)
   211  }
   212  
   213  func (kv *keyValuesNil[TValue]) Len() int {
   214  	return 0
   215  }
   216  
   217  func (kv *keyValuesNil[TValue]) IsNil() bool {
   218  	return true
   219  }
   220  
   221  func (kv *keyValuesNil[TValue]) SortTo(buf []KeyValue[TValue]) []KeyValue[TValue] {
   222  	return nil
   223  }
   224  
   225  func NewKeyValues[TValue string | float64 | *TypedValue | any]() KeyValues[TValue] {
   226  	return &keyValuesImpl[TValue]{
   227  		keyValues: make(map[string]TValue),
   228  	}
   229  }