示例#1
0
文件: input.go 项目: postfix/fwk
func (input *InputStreamer) Connect(ports []fwk.Port) error {
	var err error

	input.ports = make(map[string]fwk.Port, len(ports))

	// FIXME(sbinet): handle multi-reader
	// FIXME(sbinet): handle local/remote files, protocols
	input.r, err = os.Open(input.Names[0])
	if err != nil {
		return err
	}

	input.rio, err = rio.NewReader(input.r)
	if err != nil {
		return err
	}

	recnames := make([]rio.Selector, 0, len(input.ports))
	for _, port := range ports {
		input.ports[port.Name] = port
		rec := input.rio.Record(port.Name)
		err = rec.Connect(port.Name, reflect.New(port.Type))
		if err != nil {
			return err
		}
		recnames = append(recnames, rio.Selector{Name: port.Name, Unpack: true})
	}

	input.scan = rio.NewScanner(input.rio)
	input.scan.Select(recnames)
	return err
}
示例#2
0
文件: stream.go 项目: postfix/fwk
func (stream *istream) read(name string, ptr interface{}) error {
	var err error

	seekr, ok := stream.f.(io.Seeker)
	if !ok {
		return fwk.Errorf("hbooksvc: input stream [%s] is not seek-able", stream.name)
	}

	pos, err := seekr.Seek(0, 1)
	if err != nil {
		return err
	}
	defer seekr.Seek(pos, 0)

	_, err = seekr.Seek(0, 0)
	if err != nil {
		return err
	}

	r := seekr.(io.Reader)
	rr, err := rio.NewReader(r)
	if err != nil {
		return err
	}
	defer rr.Close()

	scan := rio.NewScanner(rr)
	scan.Select([]rio.Selector{{Name: name, Unpack: true}})
	if !scan.Scan() {
		return scan.Err()
	}
	rec := scan.Record()
	if rec == nil {
		return fwk.Errorf("hbooksvc: could not find record [%s] in stream [%s]", name, stream.name)
	}
	blk := rec.Block(name)
	if blk == nil {
		return fwk.Errorf(
			"hbooksvc: could not get block [%s] from record [%s] in stream [%s]",
			name, name, stream.name,
		)
	}
	err = blk.Read(ptr)
	if err != nil {
		return fwk.Errorf(
			"hbooksvc: could not read data from block [%s] from record [%s] in stream [%s]: %v",
			name, name, stream.name, err,
		)
	}
	return err
}