Skip to content

Commit

Permalink
clearer method names
Browse files Browse the repository at this point in the history
  • Loading branch information
dlepex committed Mar 22, 2018
1 parent a89aae1 commit 090de93
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 26 deletions.
16 changes: 11 additions & 5 deletions pan/bounded.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

// Bounded simplifies the implemenetation of "internal" panic i.e. panic that doesn't cross package boundaries
// The idea is that the public API of your package never leaks internal panics, see RecoverTo() & Panic() methods.
// The idea is that the public API of your package never leaks internal panics, see RecoverTo() & Check() methods.
type Bounded struct {
pkg string
}
Expand All @@ -30,23 +30,29 @@ func (b *Bounded) RecoverTo(errPtr *error) {
return
}
if w, ok := r.(*errWrap); ok && w.b == b {
// our own panic: catch.
// our own "internal"/package-local panic: catch.
*errPtr = w.e
} else {
// alien panic: rethrow.
panic(r)
}
}

// Panic must never be called by public API of your pkg, without `defer b.RecoverTo(&err)`
func (b *Bounded) Panic(e error) {
// Check must never be called by public API of your pkg, without `defer b.RecoverTo(&err)`
func (b *Bounded) Check(e error) {
if e != nil {
panic(&errWrap{e, b})
}
}

func (b *Bounded) Checkf(cond bool, format string, a ...interface{}) {
if !cond {
panic(&errWrap{fmt.Errorf(format, a...), b})
}
}

func (b *Bounded) Panicf(format string, a ...interface{}) {
b.Panic(fmt.Errorf(format, a...))
panic(&errWrap{fmt.Errorf(format, a...), b})
}

func callerPkg() string {
Expand Down
8 changes: 4 additions & 4 deletions print.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"go/ast"
"go/token"
"log"
"os"

pri "github.com/dlepex/typeinst/printer"
Expand All @@ -29,19 +28,20 @@ func newAstPrinter(w *bufio.Writer, rf pri.RenameFunc) *astPrinter {
func (p *astPrinter) println(node interface{}) {
err := p.Fprint(p.w, p.fset, node)
if err != nil {
log.Fatalf("Print AST error (%v) for node: %v", err, node)
bpan.Panicf("Print AST error (%v) for node: %v", err, node)
}
_, err = p.w.WriteString("\n\n")
if err != nil {
log.Fatalf("Writer error: %v", err)
bpan.Panicf("Writer error: %v", err)
}
}

func (im *Impl) Print() error {
func (im *Impl) Print() (err error) {
f, err := os.Create(im.outputFile)
if err != nil {
return err
}
defer bpan.RecoverTo(&err)
defer f.Close()
wr := bufio.NewWriter(f)
defer wr.Flush()
Expand Down
24 changes: 12 additions & 12 deletions typeinst.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,34 @@ const fileSuffix = "_ti"
func main() {
gofile := os.Getenv("GOFILE")
fmt.Printf("$GOPATH = %v\n$GOFILE = %v\n", os.Getenv("GOPATH"), gofile)
fatalIfErr(Run(gofile))
}

func Run(gofile string) (err error) {
defer bpan.RecoverTo(&err)
implFile := implFilename(gofile, fileSuffix)

dsl, err := ParseDSL(gofile, "")
fatalIfErr(err)
bpan.Check(err)
impl := NewImpl(implFile, dsl.PkgName)
err = Dsl2Impl(dsl, impl)
fatalIfErr(err)
dsl2Impl(dsl, impl)
return
}

func Dsl2Impl(dsl *DSL, impl *Impl) (err error) {
defer bpan.RecoverTo(&err)
check := bpan.Panic
func dsl2Impl(dsl *DSL, impl *Impl) {
for _, it := range dsl.Items {
for _, g := range it.GenericTypes {
p, err := impl.Package(g.PkgName, dsl.Imports)
check(err)
bpan.Check(err)
log.Printf("dsl: type %s = %s with args: %v", it.InstName, g.Type, it.TypeArgs)
check(p.Inst(g.Type, it.InstName, it.TypeArgs))
bpan.Check(p.Inst(g.Type, it.InstName, it.TypeArgs))
}
}
for path, pdesc := range impl.pkg {
log.Printf("walk: %s", path)
check(pdesc.ResolveGeneric())
bpan.Check(pdesc.ResolveGeneric())
}
log.Printf("printing...")
check(impl.Print())
return
bpan.Check(impl.Print())
}

func implFilename(p, suf string) string {
Expand Down
11 changes: 6 additions & 5 deletions typeinst_test.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
package main

import (
"os"
"os/exec"
"path"
"testing"

"github.com/stretchr/testify/assert"
)

func TestUsage(t *testing.T) {
run("testdata/usage/case1.go")
e := run("testdata/usage/case1.go")
assert.NoError(t, e)
cmd := exec.Command("go", "build", "github.com/dlepex/typeinst/testdata/usage")
b, e := cmd.CombinedOutput()
if e != nil {
t.Errorf(string(b))
}
}

func run(f string) {
func run(f string) error {
p := packagePath(path.Join("github.com/dlepex/typeinst", f))
os.Setenv("GOFILE", p)
main()
return Run(p)
}

0 comments on commit 090de93

Please sign in to comment.