github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/util/serialization/metadata.go (about)

     1  package serialization
     2  
     3  import (
     4  	"bufio"
     5  	"encoding/json"
     6  	"io"
     7  
     8  	"github.com/pyroscope-io/pyroscope/pkg/util/varint"
     9  )
    10  
    11  func WriteMetadata(w io.Writer, metadata map[string]interface{}) error {
    12  	b, err := json.Marshal(metadata)
    13  	if err != nil {
    14  		return err
    15  	}
    16  	varint.Write(w, uint64(len(b)))
    17  	w.Write(b)
    18  	return nil
    19  }
    20  
    21  func ReadMetadata(br *bufio.Reader) (map[string]interface{}, error) {
    22  	l, err := varint.Read(br)
    23  	if err != nil {
    24  		return nil, err
    25  	}
    26  	jsonBuf := make([]byte, l)
    27  	_, err = io.ReadAtLeast(br, jsonBuf, int(l))
    28  	if err != nil {
    29  		return nil, err
    30  	}
    31  	var metadata map[string]interface{}
    32  	err = json.Unmarshal(jsonBuf, &metadata)
    33  	if err != nil {
    34  		return nil, err
    35  	}
    36  	return metadata, nil
    37  }