github.com/TeaOSLab/EdgeNode@v1.3.8/internal/utils/kvstore/value_encoder_bytes.go (about)

     1  // Copyright 2024 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
     2  
     3  package kvstore
     4  
     5  type BytesValueEncoder[T []byte] struct {
     6  }
     7  
     8  func NewBytesValueEncoder[T []byte]() *BytesValueEncoder[T] {
     9  	return &BytesValueEncoder[T]{}
    10  }
    11  
    12  func (this *BytesValueEncoder[T]) Encode(value T) ([]byte, error) {
    13  	if len(value) == 0 {
    14  		return nil, nil
    15  	}
    16  
    17  	var resultValue = make([]byte, len(value))
    18  	copy(resultValue, value)
    19  	return resultValue, nil
    20  }
    21  
    22  func (this *BytesValueEncoder[T]) EncodeField(value T, fieldName string) ([]byte, error) {
    23  	_ = fieldName
    24  	return this.Encode(value)
    25  }
    26  
    27  func (this *BytesValueEncoder[T]) Decode(valueData []byte) (value T, err error) {
    28  	if len(valueData) == 0 {
    29  		return
    30  	}
    31  
    32  	value = make([]byte, len(valueData))
    33  	copy(value, valueData)
    34  	return
    35  }