This repository was archived by the owner on Jun 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 606
Format generated source code before writing it to the destination #3
Closed
Closed
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,8 +19,10 @@ package main | |
// TODO: This does not support embedding package-local interfaces in a separate file. | ||
|
||
import ( | ||
"bytes" | ||
"flag" | ||
"fmt" | ||
"go/format" | ||
"go/token" | ||
"io" | ||
"log" | ||
|
@@ -86,9 +88,7 @@ func main() { | |
packageName = "mock_" + sanitize(pkg.Name) | ||
} | ||
|
||
g := generator{ | ||
w: dst, | ||
} | ||
g := generator{} | ||
if *source != "" { | ||
g.filename = *source | ||
} else { | ||
|
@@ -98,6 +98,9 @@ func main() { | |
if err := g.Generate(pkg, packageName); err != nil { | ||
log.Fatalf("Failed generating mock: %v", err) | ||
} | ||
if _, err := dst.Write(g.Format()); err != nil { | ||
log.Fatalf("Failed writing to destination: %v", err) | ||
} | ||
} | ||
|
||
func usage() { | ||
|
@@ -123,7 +126,7 @@ Example: | |
` | ||
|
||
type generator struct { | ||
w io.Writer | ||
buf bytes.Buffer | ||
indent string | ||
|
||
filename string // may be empty | ||
|
@@ -133,7 +136,7 @@ type generator struct { | |
} | ||
|
||
func (g *generator) p(format string, args ...interface{}) { | ||
fmt.Fprintf(g.w, g.indent+format+"\n", args...) | ||
fmt.Fprintf(&g.buf, g.indent+format+"\n", args...) | ||
} | ||
|
||
func (g *generator) in() { | ||
|
@@ -411,3 +414,15 @@ func (g *generator) GenerateMockRecorderMethod(mockType string, m *model.Method) | |
g.p("}") | ||
return nil | ||
} | ||
|
||
// Format gives a go-formatted copy of the buffer. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's call this Output. // Output returns the generator's output, formatted in the standard Go style. |
||
func (g *generator) Format() []byte { | ||
src, err := format.Source(g.buf.Bytes()) | ||
if err != nil { | ||
// This should never happen, but if it does, give the user a warning | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it should never happen, log.Fatalf is good enough. You can have it print out the buffer. |
||
// and return the contents of the unmodified buffer. | ||
log.Printf("Warning: generated source could not be go-formatted: %s", err) | ||
return g.buf.Bytes() | ||
} | ||
return src | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's make this