github.com/alibaba/ilogtail/pkg@v0.0.0-20250526110833-c53b480d046c/protocol/sls_logs.pb.helper.go (about)

     1  package protocol
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/gogo/protobuf/proto"
     7  )
     8  
     9  func CloneLog(log *Log) *Log {
    10  	cloneLog := &Log{
    11  		Time:     log.Time,
    12  		TimeNs:   log.TimeNs,
    13  		Contents: make([]*Log_Content, len(log.Contents), cap(log.Contents)),
    14  	}
    15  	for i, content := range log.Contents {
    16  		cloneLog.Contents[i] = &Log_Content{
    17  			Key:   content.Key,
    18  			Value: content.Value,
    19  		}
    20  	}
    21  	return cloneLog
    22  }
    23  
    24  func SetLogTime(log *Log, second uint32) {
    25  	log.Time = second
    26  }
    27  
    28  func SetLogTimeWithNano(log *Log, second uint32, nanosecond uint32) {
    29  	log.Time = second
    30  	log.TimeNs = &nanosecond
    31  }
    32  
    33  type Codec struct{}
    34  
    35  func (Codec) Marshal(v interface{}) ([]byte, error) {
    36  	vv, ok := v.(proto.Message)
    37  	if !ok {
    38  		return nil, fmt.Errorf("failed to marshal, message is %T, want proto.Message", v)
    39  	}
    40  	return proto.Marshal(vv)
    41  }
    42  
    43  func (Codec) Unmarshal(data []byte, v interface{}) error {
    44  	vv, ok := v.(proto.Message)
    45  	if !ok {
    46  		return fmt.Errorf("failed to unmarshal, message is %T, want proto.Message", v)
    47  	}
    48  	return proto.Unmarshal(data, vv)
    49  }
    50  
    51  func (Codec) Name() string {
    52  	return "proto"
    53  }