forked from lyft/protoc-gen-star
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost_process.go
46 lines (36 loc) · 1.08 KB
/
post_process.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
package pgs
import (
"go/format"
"strings"
)
// A PostProcessor modifies the output of an Artifact before final rendering.
// PostProcessors are only applied to Artifacts created by Modules.
type PostProcessor interface {
// Match returns true if the PostProcess should be applied to the Artifact.
// Process is called immediately after Match for the same Artifact.
Match(a Artifact) bool
// Process receives the rendered artifact and returns the processed bytes or
// an error if something goes wrong.
Process(in []byte) ([]byte, error)
}
type goFmt struct{}
// GoFmt returns a PostProcessor that runs gofmt on any files ending in ".go"
func GoFmt() PostProcessor { return goFmt{} }
func (p goFmt) Match(a Artifact) bool {
var n string
switch a := a.(type) {
case GeneratorFile:
n = a.Name
case GeneratorTemplateFile:
n = a.Name
case CustomFile:
n = a.Name
case CustomTemplateFile:
n = a.Name
default:
return false
}
return strings.HasSuffix(n, ".go")
}
func (p goFmt) Process(in []byte) ([]byte, error) { return format.Source(in) }
var _ PostProcessor = goFmt{}