-
Notifications
You must be signed in to change notification settings - Fork 19
/
gen_classfile.go
52 lines (42 loc) · 1.14 KB
/
gen_classfile.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
package java
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"github.com/pulumi/pulumi-java/pkg/codegen/java/names"
)
type classFileContext struct {
writer io.Writer
imports *names.Imports
pkg names.FQN
className names.Ident
}
func (ctx *classFileContext) classFQN() names.FQN {
return ctx.pkg.Dot(ctx.className)
}
func (ctx *classFileContext) ref(name names.FQN) string {
return ctx.imports.Ref(name)
}
func genClassFile(
pkg names.FQN,
className names.Ident,
generator func(ctx *classFileContext) error,
) (string, error) {
imports := names.NewImports(pkg, className)
var buf bytes.Buffer
ctx := &classFileContext{&buf, imports, pkg, className}
err := generator(ctx)
code := fmt.Sprintf("%s\n\n%s\n\n%s",
imports.PackageCode(),
imports.ImportCode(),
buf.String())
return code, err
}
// Constructs a context with no-op writer, and throw-away import tracking.
func newPseudoClassFileContext() *classFileContext {
pkg := names.Ident("com").FQN().Dot(names.Ident("example"))
className := names.Ident("Pseudo")
imports := names.NewImports(pkg, className)
return &classFileContext{ioutil.Discard, imports, pkg, className}
}