Exemplo n.º 1
0
func Msgpack(file, pkg string) error {
	fset, err := parse.File(file, true)
	if err != nil {
		return err
	}

	buf := bytes.NewBuffer([]byte{})

	wt := iox.NewWriteTransformer(buf, func() iox.WriteTransformerFunc {
		done := false
		return func(s *iox.WriteTransformerState) {
			if res := iox.StrictMatchFinder([]byte("package "+pkg), 0, s.Buffer); res != nil {
				if done {
					s.Pass(-1)
					return
				}
				if res.Partial {
				} else {
					s.Drop(res.End + 1)
					done = true
				}
			} else {
				s.Drop(-1)
			}
		}
	}())

	_ = wt

	prntr := gen.NewPrinter(gen.Encode|gen.Decode|gen.Marshal|gen.Unmarshal|gen.Size, buf, nil)
	err = fset.PrintTo(prntr)
	if err != nil {
		return err
	}

	data := buf.Bytes()
	data, err = format.Source(data)
	if err != nil {
		return err
	}

	f, err := os.OpenFile(file, os.O_APPEND|os.O_WRONLY, 0600)
	if err != nil {
		return err
	}
	defer f.Close()
	_, err = io.Copy(f, bytes.NewReader(data))
	if err != nil {
		return err
	}

	return nil
}
Exemplo n.º 2
0
Arquivo: main.go Projeto: kgrz/msgp
// Run writes all methods using the associated file or path, e.g.
//
//	err := msgp.Run("path/to/myfile.go", gen.Size|gen.Marshal|gen.Unmarshal|gen.Test, false)
//
func Run(gofile string, mode gen.Method, unexported bool) error {
	if mode&^gen.Test == 0 {
		return nil
	}
	fmt.Println(chalk.Magenta.Color("======== MessagePack Code Generator ======="))
	fmt.Printf(chalk.Magenta.Color(">>> Input: \"%s\"...\n"), gofile)
	fs, err := parse.File(gofile, unexported)
	if err != nil {
		return err
	}

	if len(fs.Identities) == 0 {
		fmt.Println(chalk.Magenta.Color("No types requiring code generation were found!"))
		return nil
	}

	return printer.PrintFile(newFilename(gofile, fs.Package), fs, mode)
}