2015-10-24 21:04:31 +00:00
|
|
|
package printer
|
2015-10-24 22:23:50 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/fatih/hcl/ast"
|
|
|
|
)
|
|
|
|
|
2015-10-25 13:08:09 +00:00
|
|
|
const (
|
|
|
|
blank = byte(' ')
|
|
|
|
newline = byte('\n')
|
|
|
|
tab = byte('\t')
|
|
|
|
)
|
|
|
|
|
2015-10-25 15:10:34 +00:00
|
|
|
// node
|
|
|
|
func (p *printer) output(n ast.Node) []byte {
|
2015-10-24 22:23:50 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
|
|
switch t := n.(type) {
|
|
|
|
case *ast.ObjectList:
|
2015-10-25 13:08:09 +00:00
|
|
|
for i, item := range t.Items {
|
2015-10-25 15:10:34 +00:00
|
|
|
buf.Write(p.objectItem(item))
|
2015-10-25 13:08:09 +00:00
|
|
|
if i != len(t.Items)-1 {
|
2015-10-25 13:12:08 +00:00
|
|
|
buf.Write([]byte{newline, newline})
|
2015-10-25 13:08:09 +00:00
|
|
|
}
|
2015-10-24 22:23:50 +00:00
|
|
|
}
|
|
|
|
case *ast.ObjectKey:
|
2015-10-25 13:08:09 +00:00
|
|
|
buf.WriteString(t.Token.Text)
|
2015-10-24 22:23:50 +00:00
|
|
|
case *ast.ObjectItem:
|
2015-10-25 15:10:34 +00:00
|
|
|
buf.Write(p.objectItem(t))
|
2015-10-24 22:23:50 +00:00
|
|
|
case *ast.LiteralType:
|
2015-10-25 13:08:09 +00:00
|
|
|
buf.WriteString(t.Token.Text)
|
2015-10-24 22:23:50 +00:00
|
|
|
case *ast.ListType:
|
2015-10-25 15:10:34 +00:00
|
|
|
buf.Write(p.list(t))
|
2015-10-24 22:23:50 +00:00
|
|
|
case *ast.ObjectType:
|
2015-10-25 15:10:34 +00:00
|
|
|
buf.Write(p.objectType(t))
|
2015-10-24 22:23:50 +00:00
|
|
|
default:
|
|
|
|
fmt.Printf(" unknown type: %T\n", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.Bytes()
|
|
|
|
}
|
|
|
|
|
2015-10-25 15:10:34 +00:00
|
|
|
func (p *printer) objectItem(o *ast.ObjectItem) []byte {
|
2015-10-24 22:23:50 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
|
|
for i, k := range o.Keys {
|
|
|
|
buf.WriteString(k.Token.Text)
|
2015-10-25 13:08:09 +00:00
|
|
|
buf.WriteByte(blank)
|
2015-10-24 22:23:50 +00:00
|
|
|
|
|
|
|
// reach end of key
|
|
|
|
if i == len(o.Keys)-1 {
|
|
|
|
buf.WriteString("=")
|
2015-10-25 13:08:09 +00:00
|
|
|
buf.WriteByte(blank)
|
2015-10-24 22:23:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-25 15:10:34 +00:00
|
|
|
buf.Write(p.output(o.Val))
|
2015-10-24 22:23:50 +00:00
|
|
|
return buf.Bytes()
|
|
|
|
}
|
|
|
|
|
2015-10-25 15:10:34 +00:00
|
|
|
func (p *printer) literal(l *ast.LiteralType) []byte {
|
2015-10-24 22:23:50 +00:00
|
|
|
return []byte(l.Token.Text)
|
|
|
|
}
|
|
|
|
|
2015-10-25 15:10:34 +00:00
|
|
|
func (p *printer) objectType(o *ast.ObjectType) []byte {
|
2015-10-25 13:08:09 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
buf.WriteString("{")
|
|
|
|
buf.WriteByte(newline)
|
|
|
|
|
|
|
|
for _, item := range o.List.Items {
|
2015-10-25 15:10:34 +00:00
|
|
|
buf.Write(p.indent(p.objectItem(item)))
|
2015-10-25 13:08:09 +00:00
|
|
|
buf.WriteByte(newline)
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.WriteString("}")
|
|
|
|
return buf.Bytes()
|
|
|
|
}
|
|
|
|
|
2015-10-25 15:10:34 +00:00
|
|
|
// printList prints a HCL list
|
|
|
|
func (p *printer) list(l *ast.ListType) []byte {
|
2015-10-24 22:23:50 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
buf.WriteString("[")
|
|
|
|
|
|
|
|
for i, item := range l.List {
|
|
|
|
if item.Pos().Line != l.Lbrack.Line {
|
|
|
|
// not same line
|
2015-10-25 13:08:09 +00:00
|
|
|
buf.WriteByte(newline)
|
2015-10-24 22:23:50 +00:00
|
|
|
}
|
|
|
|
|
2015-10-25 15:10:34 +00:00
|
|
|
buf.Write(p.indent(p.output(item)))
|
2015-10-24 22:23:50 +00:00
|
|
|
|
|
|
|
if i != len(l.List)-1 {
|
|
|
|
buf.WriteString(",")
|
2015-10-25 13:08:09 +00:00
|
|
|
buf.WriteByte(blank)
|
2015-10-24 22:23:50 +00:00
|
|
|
} else if item.Pos().Line != l.Lbrack.Line {
|
|
|
|
buf.WriteString(",")
|
2015-10-25 13:08:09 +00:00
|
|
|
buf.WriteByte(newline)
|
2015-10-24 22:23:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
buf.WriteString("]")
|
|
|
|
return buf.Bytes()
|
|
|
|
}
|
2015-10-25 13:08:09 +00:00
|
|
|
|
2015-10-25 15:10:34 +00:00
|
|
|
// indent indents the lines of the given buffer for each non-empty line
|
2015-10-25 15:02:40 +00:00
|
|
|
func (p *printer) indent(buf []byte) []byte {
|
2015-10-25 15:10:34 +00:00
|
|
|
var prefix []byte
|
|
|
|
if p.cfg.SpacesWidth != 0 {
|
|
|
|
for i := 0; i < p.cfg.SpacesWidth; i++ {
|
2015-10-25 15:02:40 +00:00
|
|
|
prefix = append(prefix, blank)
|
|
|
|
}
|
2015-10-25 15:10:34 +00:00
|
|
|
} else {
|
|
|
|
prefix = []byte{tab}
|
2015-10-25 15:02:40 +00:00
|
|
|
}
|
|
|
|
|
2015-10-25 14:51:18 +00:00
|
|
|
var res []byte
|
|
|
|
bol := true
|
|
|
|
for _, c := range buf {
|
|
|
|
if bol && c != '\n' {
|
2015-10-25 15:02:40 +00:00
|
|
|
res = append(res, prefix...)
|
2015-10-25 13:08:09 +00:00
|
|
|
}
|
2015-10-25 14:51:18 +00:00
|
|
|
res = append(res, c)
|
|
|
|
bol = c == '\n'
|
2015-10-25 13:08:09 +00:00
|
|
|
}
|
2015-10-25 14:51:18 +00:00
|
|
|
return res
|
2015-10-25 13:08:09 +00:00
|
|
|
}
|