github.com/TeaOSLab/EdgeNode@v1.3.8/internal/utils/kvstore/value_encoder.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  import "encoding/json"
     6  
     7  type ValueEncoder[T any] interface {
     8  	Encode(value T) ([]byte, error)
     9  	EncodeField(value T, fieldName string) ([]byte, error)
    10  	Decode(valueBytes []byte) (value T, err error)
    11  }
    12  
    13  type BaseObjectEncoder[T any] struct {
    14  }
    15  
    16  func (this *BaseObjectEncoder[T]) Encode(value T) ([]byte, error) {
    17  	return json.Marshal(value)
    18  }
    19  
    20  func (this *BaseObjectEncoder[T]) Decode(valueData []byte) (value T, err error) {
    21  	err = json.Unmarshal(valueData, &value)
    22  	return
    23  }