-
Notifications
You must be signed in to change notification settings - Fork 25
/
common.go
164 lines (147 loc) · 4.54 KB
/
common.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright 2015-present, Cyrill @ Schumacher.fm and the CoreStore contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package codegen
import (
"bytes"
"strings"
"github.com/corestoreio/errors"
"github.com/corestoreio/pkg/util/conv"
"github.com/corestoreio/pkg/util/strs"
)
// FormatError gets returned when generating the source code in case gofmt or
// any equal other program for a language can't run the formatting.
type FormatError struct {
error
Code string
}
func (*FormatError) ErrorKind() errors.Kind {
return errors.NotAcceptable
}
type common struct {
SecondLineComments []string
packageName string
*bytes.Buffer
packageNames map[string]string // Imported package names in the current file.
indent string
}
// AddImport adds a new import path. importPath required and packageName optional.
func (g *common) AddImport(importPath, packageName string) {
g.packageNames[importPath] = packageName
}
// AddImports adds multiple import paths at once. They all must have unique base
// names.
func (g *common) AddImports(importPaths ...string) {
for _, ip := range importPaths {
g.packageNames[ip] = ""
}
}
// Writes a multiline comment and formats it to a max width of 80 chars. It adds
// automatically the comment prefix `//`. It converts all types to string, if it
// can't it panics. If the first argument is a boolean and true, the subsequent
// data gets printed, otherwise not.
func (g *common) C(comments ...interface{}) {
if b, ok := comments[0].(bool); ok && !b { // do not print as condition is false.
return
} else if ok {
comments = comments[1:] // remove the first bool value to not print it
}
cs := make([]string, 0, len(comments))
for _, cIF := range comments {
s, err := conv.ToStringE(cIF)
if err != nil {
panic(err)
}
cs = append(cs, s)
}
comment(g.Buffer, cs...)
}
func comment(g *bytes.Buffer, comments ...string) {
cLines := strings.Split(strs.WordWrap(strings.Join(comments, " "), 78), "\n")
for _, c := range cLines {
g.WriteString("// ")
g.WriteString(c)
g.WriteByte('\n')
}
}
var emptyIString interface{} = ""
// SkipWS converts all arguments to type string, panics if it does not support a
// type, and merges all arguments to one single string without white space
// concatenation. This function can be used as argument to Pln or P or C.
func SkipWS(str ...interface{}) []byte {
var buf bytes.Buffer
for _, v := range str {
s, err := conv.ToStringE(v)
if err != nil {
panic(err)
}
buf.WriteString(s)
}
return buf.Bytes()
}
// Pln prints the arguments to the generated output. It tries to convert all
// kind of types to a string. It adds a line break at the end IF there are strs
// to print. If the first argument is a boolean and true, the subsequent data
// gets printed, otherwise not.
func (g *common) Pln(str ...interface{}) {
if ls := len(str); ls == 0 || (ls == 1 && str[0] == emptyIString) {
return
}
if b, ok := str[0].(bool); ok && !b { // do not print as condition is false.
return
} else if ok {
str = str[1:] // remove the first bool value to not print it
}
_, _ = g.WriteString(g.indent)
for _, v := range str {
s, err := conv.ToStringE(v)
if err != nil {
panic(err)
}
_, _ = g.WriteString(s)
g.WriteByte(' ')
}
_ = g.WriteByte('\n')
}
// P same as Pln but without the line break at the end.
func (g *common) P(str ...interface{}) {
if ls := len(str); ls == 0 || (ls == 1 && str[0] == emptyIString) {
return
}
if b, ok := str[0].(bool); ok && b { // do not print as condition is false.
return
} else if ok {
str = str[1:] // remove the first bool value to not print it
}
_, _ = g.WriteString(g.indent)
for _, v := range str {
s, err := conv.ToStringE(v)
if err != nil {
panic(err)
}
_, _ = g.WriteString(s)
g.WriteByte(' ')
}
}
// In Indents the output one tab stop.
func (g *common) In() { g.indent += "\t" }
// Out unindents the output one tab stop.
func (g *common) Out() {
if len(g.indent) > 0 {
g.indent = g.indent[1:]
}
}
// EncloseBT encloses the string s in backticks
func EncloseBT(s string) string {
return "`" + s + "`"
}