Skip to content

Commit

Permalink
Merge 42ebf52 into c2c1884
Browse files Browse the repository at this point in the history
  • Loading branch information
gabstv committed Apr 16, 2020
2 parents c2c1884 + 42ebf52 commit 58018de
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,5 @@ type IP struct {

To skip the tag for the generated XXX_* fields, use
`-XXX_skip=yaml,xml` flag.

To enable verbose logging, use `-verbose`
9 changes: 4 additions & 5 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"go/parser"
"go/token"
"io/ioutil"
"log"
"os"
"regexp"
"strings"
Expand All @@ -26,7 +25,7 @@ type textArea struct {
}

func parseFile(inputPath string, xxxSkip []string) (areas []textArea, err error) {
log.Printf("parsing file %q for inject tag comments", inputPath)
logf("parsing file %q for inject tag comments", inputPath)
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, inputPath, nil, parser.ParseComments)
if err != nil {
Expand Down Expand Up @@ -103,7 +102,7 @@ func parseFile(inputPath string, xxxSkip []string) (areas []textArea, err error)
}
}
}
log.Printf("parsed file %q, number of fields to inject custom tags: %d", inputPath, len(areas))
logf("parsed file %q, number of fields to inject custom tags: %d", inputPath, len(areas))
return
}

Expand All @@ -125,15 +124,15 @@ func writeFile(inputPath string, areas []textArea) (err error) {
// inject custom tags from tail of file first to preserve order
for i := range areas {
area := areas[len(areas)-i-1]
log.Printf("inject custom tag %q to expression %q", area.InjectTag, string(contents[area.Start-1:area.End-1]))
logf("inject custom tag %q to expression %q", area.InjectTag, string(contents[area.Start-1:area.End-1]))
contents = injectTag(contents, area)
}
if err = ioutil.WriteFile(inputPath, contents, 0644); err != nil {
return
}

if len(areas) > 0 {
log.Printf("file %q is injected with custom tags", inputPath)
logf("file %q is injected with custom tags", inputPath)
}
return
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func main() {
var xxxTags string
flag.StringVar(&inputFile, "input", "", "path to input file")
flag.StringVar(&xxxTags, "XXX_skip", "", "skip tags to inject on XXX fields")
flag.BoolVar(&verbose, "verbose", false, "verbose logging")

flag.Parse()

Expand Down
17 changes: 17 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package main

import (
"bytes"
"io/ioutil"
"log"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -160,3 +162,18 @@ func TestContinueParsingWhenSkippingFields(t *testing.T) {
}
}
}

func TestVerbose(t *testing.T) {
b := new(bytes.Buffer)
log.SetOutput(b)
verbose = false
logf("test")
if len(b.Bytes()) > 0 {
t.Errorf("verbose should be off")
}
verbose = true
logf("test")
if len(b.Bytes()) == 0 {
t.Errorf("verbose should be on")
}
}
14 changes: 14 additions & 0 deletions verbose.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import (
"log"
)

var verbose = false

func logf(format string, v ...interface{}) {
if !verbose {
return
}
log.Printf(format, v...)
}

0 comments on commit 58018de

Please sign in to comment.