示例#1
0
文件: printer.go 项目: fatih/hcl
// Format formats src HCL and returns the result.
func Format(src []byte) ([]byte, error) {
	node, err := parser.Parse(src)
	if err != nil {
		return nil, err
	}

	var buf bytes.Buffer
	if err := DefaultConfig.Fprint(&buf, node); err != nil {
		return nil, err
	}

	return buf.Bytes(), nil
}
示例#2
0
文件: printer_test.go 项目: fatih/hcl
// format parses src, prints the corresponding AST, verifies the resulting
// src is syntactically correct, and returns the resulting src or an error
// if any.
func format(src []byte) ([]byte, error) {
	// parse src
	node, err := parser.Parse(src)
	if err != nil {
		return nil, fmt.Errorf("parse: %s\n%s", err, src)
	}

	var buf bytes.Buffer

	cfg := &Config{}
	if err := cfg.Fprint(&buf, node); err != nil {
		return nil, fmt.Errorf("print: %s", err)
	}

	// make sure formatted output is syntactically correct
	res := buf.Bytes()

	if _, err := parser.Parse(src); err != nil {
		return nil, fmt.Errorf("parse: %s\n%s", err, src)
	}

	return res, nil
}