Ejemplo n.º 1
0
func (ft protoIDLFileType) VerifyFile(f *generator.File, pathname string) error {
	log.Printf("Verifying IDL file %q", pathname)
	friendlyName := filepath.Join(f.PackageName, f.Name)
	b := &bytes.Buffer{}
	et := generator.NewErrorTracker(b)
	ft.assemble(et, f)
	if et.Error() != nil {
		return et.Error()
	}
	formatted := b.Bytes()
	existing, err := ioutil.ReadFile(pathname)
	if err != nil {
		return fmt.Errorf("unable to read file %q for comparison: %v", friendlyName, err)
	}
	if bytes.Compare(formatted, existing) == 0 {
		return nil
	}
	// Be nice and find the first place where they differ
	i := 0
	for i < len(formatted) && i < len(existing) && formatted[i] == existing[i] {
		i++
	}
	eDiff, fDiff := existing[i:], formatted[i:]
	if len(eDiff) > 100 {
		eDiff = eDiff[:100]
	}
	if len(fDiff) > 100 {
		fDiff = fDiff[:100]
	}
	return fmt.Errorf("output for %q differs; first existing/expected diff: \n  %q\n  %q", friendlyName, string(eDiff), string(fDiff))
}
Ejemplo n.º 2
0
func (ft protoIDLFileType) AssembleFile(f *generator.File, pathname string) error {
	log.Printf("Assembling IDL file %q", pathname)
	destFile, err := os.Create(pathname)
	if err != nil {
		return err
	}
	defer destFile.Close()

	b := &bytes.Buffer{}
	et := generator.NewErrorTracker(b)
	ft.assemble(et, f)
	if et.Error() != nil {
		return et.Error()
	}

	// TODO: is there an IDL formatter?
	_, err = destFile.Write(b.Bytes())
	return err
}