go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/analysis/internal/changepoints/testvariantbranch/test_variant_branch_encode.go (about)

     1  // Copyright 2023 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package testvariantbranch
    16  
    17  import (
    18  	"google.golang.org/protobuf/proto"
    19  
    20  	"go.chromium.org/luci/common/errors"
    21  
    22  	cpb "go.chromium.org/luci/analysis/internal/changepoints/proto"
    23  	"go.chromium.org/luci/analysis/internal/span"
    24  	pb "go.chromium.org/luci/analysis/proto/v1"
    25  )
    26  
    27  // EncodeProtoMessage uses zstd to encode a proto message into []byte.
    28  func EncodeProtoMessage(m proto.Message) ([]byte, error) {
    29  	bytes, err := proto.Marshal(m)
    30  	if err != nil {
    31  		return nil, err
    32  	}
    33  	return span.Compress(bytes), nil
    34  }
    35  
    36  func EncodeSegment(seg *cpb.Segment) ([]byte, error) {
    37  	if seg == nil {
    38  		return []byte{}, nil
    39  	}
    40  	return EncodeProtoMessage(seg)
    41  }
    42  
    43  func EncodeSegments(segs *cpb.Segments) ([]byte, error) {
    44  	if segs == nil {
    45  		return []byte{}, nil
    46  	}
    47  	return EncodeProtoMessage(segs)
    48  }
    49  
    50  func EncodeStatistics(stats *cpb.Statistics) ([]byte, error) {
    51  	if stats == nil {
    52  		return []byte{}, nil
    53  	}
    54  	return EncodeProtoMessage(stats)
    55  }
    56  
    57  func EncodeSourceRef(sourceRef *pb.SourceRef) ([]byte, error) {
    58  	if sourceRef == nil {
    59  		panic("source ref should not be nil")
    60  	}
    61  	return EncodeProtoMessage(sourceRef)
    62  }
    63  
    64  // DecodeProtoMessage decodes a byte slice into a proto message.
    65  // It is the inverse of EncodeProtoMessage.
    66  func DecodeProtoMessage(bytes []byte, m proto.Message) error {
    67  	buf := make([]byte, len(bytes)*2)
    68  	decompressed, err := span.Decompress(bytes, buf)
    69  	if err != nil {
    70  		return errors.Annotate(err, "decompress").Err()
    71  	}
    72  	return proto.Unmarshal(decompressed, m)
    73  }
    74  
    75  // DecodeSegment decodes []byte in to Segment.
    76  func DecodeSegment(bytes []byte) (*cpb.Segment, error) {
    77  	if len(bytes) == 0 {
    78  		return nil, nil
    79  	}
    80  	seg := &cpb.Segment{}
    81  	err := DecodeProtoMessage(bytes, seg)
    82  	if err != nil {
    83  		return nil, err
    84  	}
    85  	return seg, nil
    86  }
    87  
    88  // DecodeSegments decodes []byte in to Segments.
    89  func DecodeSegments(bytes []byte) (*cpb.Segments, error) {
    90  	if len(bytes) == 0 {
    91  		return nil, nil
    92  	}
    93  	seg := &cpb.Segments{}
    94  	err := DecodeProtoMessage(bytes, seg)
    95  	if err != nil {
    96  		return nil, err
    97  	}
    98  	return seg, nil
    99  }
   100  
   101  // DecodeStatistics decodes []byte into a Statistics proto.
   102  func DecodeStatistics(bytes []byte) (*cpb.Statistics, error) {
   103  	if len(bytes) == 0 {
   104  		return nil, nil
   105  	}
   106  	stats := &cpb.Statistics{}
   107  	err := DecodeProtoMessage(bytes, stats)
   108  	if err != nil {
   109  		return nil, err
   110  	}
   111  	return stats, nil
   112  }
   113  
   114  // DecodeSourceRef decodes []byte in to SourceRef.
   115  func DecodeSourceRef(bytes []byte) (*pb.SourceRef, error) {
   116  	sourceRef := &pb.SourceRef{}
   117  	err := DecodeProtoMessage(bytes, sourceRef)
   118  	if err != nil {
   119  		return nil, err
   120  	}
   121  	return sourceRef, nil
   122  }