github.com/TeaOSLab/EdgeNode@v1.3.8/internal/utils/agents/db_kv_objects.go (about) 1 // Copyright 2024 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn . 2 3 package agents 4 5 import ( 6 "encoding/binary" 7 "encoding/json" 8 "errors" 9 ) 10 11 type AgentIPEncoder[T interface{ *AgentIP }] struct { 12 } 13 14 func (this *AgentIPEncoder[T]) Encode(value T) ([]byte, error) { 15 return json.Marshal(value) 16 } 17 18 func (this *AgentIPEncoder[T]) EncodeField(value T, fieldName string) ([]byte, error) { 19 return nil, errors.New("invalid field name '" + fieldName + "'") 20 } 21 22 func (this *AgentIPEncoder[T]) Decode(valueBytes []byte) (value T, err error) { 23 err = json.Unmarshal(valueBytes, &value) 24 return 25 } 26 27 // EncodeKey generate key for ip item 28 func (this *AgentIPEncoder[T]) EncodeKey(item *AgentIP) string { 29 var b = make([]byte, 8) 30 if item.Id < 0 { 31 item.Id = 0 32 } 33 34 binary.BigEndian.PutUint64(b, uint64(item.Id)) 35 return string(b) 36 }