Exemplo n.º 1
0
func (p *PickleSerializer) Serialize(o interface{}) (output []byte, err error) {
	buf := new(bytes.Buffer)
	//BUG: this doesn't work for some reason. It always serialize into 0 bytes
	_, err = stalecucumber.NewPickler(buf).Pickle(o)
	output = buf.Bytes()
	return
}
Exemplo n.º 2
0
func (listener *CarbonlinkListener) packReply(reply *Reply) []byte {
	buf := new(bytes.Buffer)

	var datapoints []interface{}

	if reply.Points != nil {
		for _, item := range reply.Points.Data {
			datapoints = append(datapoints, stalecucumber.NewTuple(item.Timestamp, item.Value))
		}
	}

	r := make(map[string][]interface{})
	r["datapoints"] = datapoints

	_, err := stalecucumber.NewPickler(buf).Pickle(r)

	if err != nil { // unknown wtf error
		return nil
	}

	resultBuf := new(bytes.Buffer)
	if err := binary.Write(resultBuf, binary.BigEndian, int32(buf.Len())); err != nil {
		return nil
	}

	resultBuf.Write(buf.Bytes())

	return resultBuf.Bytes()
}
Exemplo n.º 3
0
func (req *CarbonlinkRequest) Build() []byte {
	requestBuf := new(bytes.Buffer)
	payloadBuf := new(bytes.Buffer)

	stalecucumber.NewPickler(payloadBuf).Pickle(req)

	binary.Write(requestBuf, binary.BigEndian, uint32(payloadBuf.Len()))
	binary.Write(requestBuf, binary.BigEndian, payloadBuf.Bytes())

	return requestBuf.Bytes()
}
Exemplo n.º 4
0
// Append a line to the output stream
func (output *DiscoOutputStream) Append(rec string) {
	s := len(rec)
	if s > output.MaxRecordSize {
		log.Println("Record too big to write to hunk")
		return
	}

	_, err := stalecucumber.NewPickler(output.PickleBuf).Pickle(rec)

	if err != nil {
		log.Println("Pickling error: ", err)
	}
	_, err = output.Hunk.Write(output.PickleBuf.Bytes())
	if err != nil {
		log.Println("Failed to append: ", err)
	}
	output.HunkSize += s
	defer output.PickleBuf.Reset()
	// Check if we need to flush the hunk
	if output.HunkSize > output.MinHunkSize {
		output.Flush()
	}
}
Exemplo n.º 5
0
func Marshal(data interface{}, outputFormat format) ([]byte, error) {
	var result []byte
	var err error

	switch outputFormat {
	case formatJSON:
		result, err = json.Marshal(&data)
		// FIXME: option to indent json
	case formatXML:
		result, err = xml.Marshal(&data)
	case formatYAML:
		result, err = yaml.Marshal(&data)
	case formatMsgpack:
		result, err = msgpack.Marshal(&data)
	case formatBson:
		result, err = bson.Marshal(&data)
	case formatPickle:
		buf := new(bytes.Buffer)
		pickler := stalecucumber.NewPickler(buf)
		_, err = pickler.Pickle(result)
		result = buf.Bytes()
	case formatPlist:
		// result, err = plist.Marshal(&data, plist.XMLFormat)
		output := new(bytes.Buffer)
		encoder := plist.NewEncoder(output)
		err = encoder.Encode(data)
		result = output.Bytes()
	case formatTOML:
		buf := new(bytes.Buffer)
		err = toml.NewEncoder(buf).Encode(data)
		result = buf.Bytes()
	default:
		err = fmt.Errorf("unsupported output format")
	}

	return result, err
}