Skip to content

Commit 88519c3

Browse files
marcelocantossparkprime
authored andcommitted
Apply more golint recommendations (#201)
* Apply more golint recommendations * Update dumpstdlibast.go to include StdAst comment * Improve dump.go package comment.
1 parent ed5f280 commit 88519c3

File tree

10 files changed

+73
-39
lines changed

10 files changed

+73
-39
lines changed

Diff for: ast/ast.go

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17+
// Package ast provides AST nodes and ancillary structures and algorithms.
1718
package ast
1819

1920
import (

Diff for: ast/stdast.go

+9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
///////////////////////////////////////////////////////////
2+
// This file was auto-generated by cmd/dumpstdlibast.go. //
3+
// https://github.com/google/go-jsonnet#generated-stdlib //
4+
// //
5+
// --------------- DO NOT EDIT BY HAND! --------------- //
6+
///////////////////////////////////////////////////////////
7+
18
package ast
29

310
var p3Var = "$"
@@ -2768,6 +2775,8 @@ var p1 = &Source{
27682775
"\n",
27692776
},
27702777
}
2778+
2779+
// StdAst is the AST for the standard library.
27712780
var StdAst = &DesugaredObject{
27722781
NodeBase: NodeBase{
27732782
loc: LocationRange{

Diff for: ast/util.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,17 @@ package ast
1818

1919
import "sort"
2020

21-
func (i *IdentifierSet) Append(idents Identifiers) {
21+
// AddIdentifiers adds a slice of identifiers to an identifier set.
22+
func (i IdentifierSet) AddIdentifiers(idents Identifiers) {
2223
for _, ident := range idents {
2324
i.Add(ident)
2425
}
2526
}
2627

27-
// ToOrderedSlice returns the elements of the current set as a ordered slice
28-
func (set IdentifierSet) ToOrderedSlice() []Identifier {
28+
// ToOrderedSlice returns the elements of the current set as an ordered slice.
29+
func (i IdentifierSet) ToOrderedSlice() []Identifier {
2930
var s []Identifier
30-
for v := range set {
31+
for v := range i {
3132
s = append(s, v)
3233
}
3334
sort.Sort(identifierSorter(s))

Diff for: cmd/dumpstdlibast.go

+15-2
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,22 @@ func main() {
4343
dump.Config.HidePrivateFields = false
4444
dump.Config.StripPackageNames = true
4545
dump.Config.VariableName = "StdAst"
46+
dump.Config.VariableDescription = "StdAst is the AST for the standard library."
4647
ast := dump.Sdump(node)
4748

48-
file.WriteString("package ast\n\n")
49+
file.WriteString(header)
4950
file.WriteString(ast)
50-
file.Sync()
51+
file.Close()
5152
}
53+
54+
var header = `
55+
///////////////////////////////////////////////////////////
56+
// This file was auto-generated by cmd/dumpstdlibast.go. //
57+
// https://github.com/google/go-jsonnet#generated-stdlib //
58+
// //
59+
// --------------- DO NOT EDIT BY HAND! --------------- //
60+
///////////////////////////////////////////////////////////
61+
62+
package ast
63+
64+
`[1:]

Diff for: dump/dump.go

+20-9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17+
// Package dump can dump a Go data structure to Go source file, so that it can
18+
// be statically embedded into other code.
1719
package dump
1820

1921
import (
@@ -31,17 +33,19 @@ var packageNameStripperRegexp = regexp.MustCompile("\\b[a-zA-Z_]+[a-zA-Z_0-9]+\\
3133

3234
// Options represents configuration option
3335
type Options struct {
34-
StripPackageNames bool
35-
HidePrivateFields bool
36-
HomePackage string
37-
VariableName string
36+
StripPackageNames bool
37+
HidePrivateFields bool
38+
HomePackage string
39+
VariableName string
40+
VariableDescription string
3841
}
3942

4043
// Config is the default config used when calling Dump
4144
var Config = Options{
42-
StripPackageNames: false,
43-
HidePrivateFields: true,
44-
VariableName: "Obj",
45+
StripPackageNames: false,
46+
HidePrivateFields: true,
47+
VariableName: "Obj",
48+
VariableDescription: "",
4549
}
4650

4751
type dumpState struct {
@@ -157,8 +161,15 @@ func (s *dumpState) dumpMap(v reflect.Value) {
157161
}
158162

159163
func (s *dumpState) dump(value interface{}) {
160-
if value == nil {
164+
writeVar := func() {
165+
if s.config.VariableDescription != "" {
166+
fmt.Fprintf(s.w, "\n// %s\n", s.config.VariableDescription)
167+
}
161168
s.w.Write([]byte("var " + s.config.VariableName + " = "))
169+
}
170+
171+
if value == nil {
172+
writeVar()
162173
printNil(s.w)
163174
s.newline()
164175
return
@@ -170,7 +181,7 @@ func (s *dumpState) dump(value interface{}) {
170181

171182
s.dumpReusedPointerVal(v)
172183

173-
s.w.Write([]byte("var " + s.config.VariableName + " = "))
184+
writeVar()
174185
if v.Kind() == reflect.Ptr && v.IsNil() {
175186
printNil(s.w)
176187
} else {

Diff for: dump/pointermap.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ type pointerMap struct {
2424
primitivePointers []uintptr
2525
}
2626

27-
// GetPointers: Given a structure, it will recursively map all pointers mentioned in the tree,
28-
// breaking circular references and provide list of:
27+
// GetPointers recursively maps all pointers mentioned in a tree, breaking
28+
// circular references, and provides a list of:
2929
// * all pointers
3030
// * all pointers that were referenced at least twice
3131
// * all pointers that point to primitive type

Diff for: jsonnet/cmd.go

+18-21
Original file line numberDiff line numberDiff line change
@@ -120,21 +120,21 @@ func usage(o io.Writer) {
120120
func safeStrToInt(str string) (i int) {
121121
i, err := strconv.Atoi(str)
122122
if err != nil {
123-
fmt.Fprintf(os.Stderr, "ERROR: Invalid integer \"%s\"\n", str)
123+
fmt.Fprintf(os.Stderr, "Invalid integer \"%s\"\n", str)
124124
os.Exit(1)
125125
}
126126
return
127127
}
128128

129-
type Command int
129+
type command int
130130

131131
const (
132132
commandEval = iota
133133
commandFmt = iota
134134
)
135135

136136
type config struct {
137-
cmd Command
137+
cmd command
138138
inputFiles []string
139139
outputFile string
140140
filenameIsCode bool
@@ -167,20 +167,18 @@ func getVarVal(s string) (string, string, error) {
167167
if exists {
168168
return name, content, nil
169169
}
170-
return "", "", fmt.Errorf("ERROR: Environment variable %v was undefined.", name)
171-
} else {
172-
return name, parts[1], nil
170+
return "", "", fmt.Errorf("environment variable %v was undefined", name)
173171
}
172+
return name, parts[1], nil
174173
}
175174

176175
func getVarFile(s string, imp string) (string, string, error) {
177176
parts := strings.SplitN(s, "=", 2)
178177
name := parts[0]
179178
if len(parts) == 1 {
180-
return "", "", fmt.Errorf("ERROR: argument not in form <var>=<file> \"%s\".", s)
181-
} else {
182-
return name, fmt.Sprintf("%s @'%s'", imp, strings.Replace(parts[1], "'", "''", -1)), nil
179+
return "", "", fmt.Errorf(`argument not in form <var>=<file> "%s"`, s)
183180
}
181+
return name, fmt.Sprintf("%s @'%s'", imp, strings.Replace(parts[1], "'", "''", -1)), nil
184182
}
185183

186184
type processArgsStatus int
@@ -217,7 +215,7 @@ func processArgs(givenArgs []string, config *config, vm *jsonnet.VM) (processArg
217215
} else if arg == "-o" || arg == "--output-file" {
218216
outputFile := nextArg(&i, args)
219217
if len(outputFile) == 0 {
220-
return processArgsStatusFailure, fmt.Errorf("ERROR: -o argument was empty string")
218+
return processArgsStatusFailure, fmt.Errorf("-o argument was empty string")
221219
}
222220
config.outputFile = outputFile
223221
} else if arg == "--" {
@@ -231,13 +229,13 @@ func processArgs(givenArgs []string, config *config, vm *jsonnet.VM) (processArg
231229
if arg == "-s" || arg == "--max-stack" {
232230
l := safeStrToInt(nextArg(&i, args))
233231
if l < 1 {
234-
return processArgsStatusFailure, fmt.Errorf("ERROR: Invalid --max-stack value: %d", l)
232+
return processArgsStatusFailure, fmt.Errorf("invalid --max-stack value: %d", l)
235233
}
236234
vm.MaxStack = l
237235
} else if arg == "-J" || arg == "--jpath" {
238236
dir := nextArg(&i, args)
239237
if len(dir) == 0 {
240-
return processArgsStatusFailure, fmt.Errorf("ERROR: -J argument was empty string")
238+
return processArgsStatusFailure, fmt.Errorf("-J argument was empty string")
241239
}
242240
if dir[len(dir)-1] != '/' {
243241
dir += "/"
@@ -302,14 +300,14 @@ func processArgs(givenArgs []string, config *config, vm *jsonnet.VM) (processArg
302300
} else if arg == "-t" || arg == "--max-trace" {
303301
l := safeStrToInt(nextArg(&i, args))
304302
if l < 0 {
305-
return processArgsStatusFailure, fmt.Errorf("ERROR: Invalid --max-trace value: %d", l)
303+
return processArgsStatusFailure, fmt.Errorf("invalid --max-trace value: %d", l)
306304
}
307305
vm.ErrorFormatter.SetMaxStackTraceSize(l)
308306
} else if arg == "-m" || arg == "--multi" {
309307
config.evalMulti = true
310308
outputDir := nextArg(&i, args)
311309
if len(outputDir) == 0 {
312-
return processArgsStatusFailure, fmt.Errorf("ERROR: -m argument was empty string")
310+
return processArgsStatusFailure, fmt.Errorf("-m argument was empty string")
313311
}
314312
if outputDir[len(outputDir)-1] != '/' {
315313
outputDir += "/"
@@ -320,13 +318,12 @@ func processArgs(givenArgs []string, config *config, vm *jsonnet.VM) (processArg
320318
} else if arg == "-S" || arg == "--string" {
321319
vm.StringOutput = true
322320
} else if len(arg) > 1 && arg[0] == '-' {
323-
return processArgsStatusFailure, fmt.Errorf("ERROR: Unrecognized argument: %s", arg)
321+
return processArgsStatusFailure, fmt.Errorf("unrecognized argument: %s", arg)
324322
} else {
325323
remainingArgs = append(remainingArgs, arg)
326324
}
327-
328325
} else {
329-
return processArgsStatusFailure, fmt.Errorf("The Go implementation currently does not support jsonnet fmt.")
326+
return processArgsStatusFailure, fmt.Errorf("the Go implementation currently does not support jsonnet fmt")
330327
}
331328
}
332329

@@ -335,15 +332,15 @@ func processArgs(givenArgs []string, config *config, vm *jsonnet.VM) (processArg
335332
want = "code"
336333
}
337334
if len(remainingArgs) == 0 {
338-
return processArgsStatusFailureUsage, fmt.Errorf("ERROR: Must give %s", want)
335+
return processArgsStatusFailureUsage, fmt.Errorf("must give %s", want)
339336
}
340337

341338
// TODO(dcunnin): Formatter allows multiple files in test and in-place mode.
342339
multipleFilesAllowed := false
343340

344341
if !multipleFilesAllowed {
345342
if len(remainingArgs) > 1 {
346-
return processArgsStatusFailure, fmt.Errorf("ERROR: Only one %s is allowed", want)
343+
return processArgsStatusFailure, fmt.Errorf("only one %s is allowed", want)
347344
}
348345
}
349346

@@ -390,7 +387,7 @@ func writeMultiOutputFiles(output map[string]string, outputDir, outputFile strin
390387

391388
// Iterate through the map in order.
392389
keys := make([]string, 0, len(output))
393-
for k, _ := range output {
390+
for k := range output {
394391
keys = append(keys, k)
395392
}
396393
sort.Strings(keys)
@@ -511,7 +508,7 @@ func main() {
511508

512509
status, err := processArgs(os.Args[1:], &config, vm)
513510
if err != nil {
514-
fmt.Fprintln(os.Stderr, err.Error())
511+
fmt.Fprintln(os.Stderr, "ERROR: "+err.Error())
515512
}
516513
switch status {
517514
case processArgsStatusContinue:

Diff for: linter/linter.go

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package linter analyses Jsonnet code for code "smells".
12
package linter
23

34
import (

Diff for: parser/parser.go

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17+
// Package parser reads Jsonnet files and parses them into AST nodes.
1718
package parser
1819

1920
import (

Diff for: static_analyzer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func visitNext(a ast.Node, inObject bool, vars ast.IdentifierSet, state *analysi
3333
return
3434
}
3535
state.err = analyzeVisit(a, inObject, vars)
36-
state.freeVars.Append(a.FreeVariables())
36+
state.freeVars.AddIdentifiers(a.FreeVariables())
3737
}
3838

3939
func analyzeVisit(a ast.Node, inObject bool, vars ast.IdentifierSet) error {

0 commit comments

Comments
 (0)