github.com/TeaOSLab/EdgeNode@v1.3.8/internal/utils/kvstore/value_encoder_bool.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 BoolValueEncoder[T bool] struct {
     6  }
     7  
     8  func NewBoolValueEncoder[T bool]() *BoolValueEncoder[T] {
     9  	return &BoolValueEncoder[T]{}
    10  }
    11  
    12  func (this *BoolValueEncoder[T]) Encode(value T) ([]byte, error) {
    13  	if value {
    14  		return []byte{1}, nil
    15  	}
    16  	return []byte{0}, nil
    17  }
    18  
    19  func (this *BoolValueEncoder[T]) EncodeField(value T, fieldName string) ([]byte, error) {
    20  	_ = fieldName
    21  	return this.Encode(value)
    22  }
    23  
    24  func (this *BoolValueEncoder[T]) Decode(valueData []byte) (value T, err error) {
    25  	if len(valueData) == 1 {
    26  		value = valueData[0] == 1
    27  	}
    28  	return
    29  }