github.com/TeaOSLab/EdgeNode@v1.3.8/internal/metrics/task_kv_objects.go (about)

     1  // Copyright 2024 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
     2  
     3  package metrics
     4  
     5  import (
     6  	"encoding/json"
     7  	"errors"
     8  )
     9  
    10  type ItemEncoder[T interface{ *Stat }] struct {
    11  }
    12  
    13  func (this *ItemEncoder[T]) Encode(value T) ([]byte, error) {
    14  	return json.Marshal(value)
    15  }
    16  
    17  func (this *ItemEncoder[T]) EncodeField(value T, fieldName string) ([]byte, error) {
    18  	return nil, errors.New("invalid field name '" + fieldName + "'")
    19  }
    20  
    21  func (this *ItemEncoder[T]) Decode(valueBytes []byte) (value T, err error) {
    22  	err = json.Unmarshal(valueBytes, &value)
    23  	return
    24  }