-
Notifications
You must be signed in to change notification settings - Fork 385
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
244 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package gno | ||
|
||
import ( | ||
"fmt" | ||
"go/ast" | ||
"go/token" | ||
"strings" | ||
|
||
"go.uber.org/multierr" | ||
"golang.org/x/tools/go/ast/astutil" | ||
) | ||
|
||
const ( | ||
gnoRealmPkgsPrefixBefore = "gno.land/r/" | ||
gnoRealmPkgsPrefixAfter = "github.com/gnolang/gno/examples/gno.land/r/" | ||
gnoPackagePrefixBefore = "gno.land/p/" | ||
gnoPackagePrefixAfter = "github.com/gnolang/gno/examples/gno.land/p/" | ||
gnoStdPkgBefore = "std" | ||
gnoStdPkgAfter = "github.com/gnolang/gno/stdlibs/stdshim" | ||
) | ||
|
||
var stdlibWhitelist = []string{ | ||
"regexp", | ||
"std", | ||
"strconv", | ||
"strings", | ||
} | ||
|
||
func Gno2Go(fset *token.FileSet, f *ast.File) (ast.Node, error) { | ||
var errs error | ||
|
||
imports := astutil.Imports(fset, f) | ||
|
||
// import whitelist | ||
for _, paragraph := range imports { | ||
for _, importSpec := range paragraph { | ||
importPath := strings.TrimPrefix(strings.TrimSuffix(importSpec.Path.Value, `"`), `"`) | ||
|
||
if strings.HasPrefix(importPath, gnoRealmPkgsPrefixBefore) { | ||
continue | ||
} | ||
|
||
if strings.HasPrefix(importPath, gnoPackagePrefixBefore) { | ||
continue | ||
} | ||
|
||
valid := false | ||
for _, whitelisted := range stdlibWhitelist { | ||
if importPath == whitelisted { | ||
valid = true | ||
continue | ||
} | ||
} | ||
if valid { | ||
continue | ||
} | ||
|
||
errs = multierr.Append(errs, fmt.Errorf("import %q is not in the whitelist", importPath)) | ||
} | ||
} | ||
|
||
// rewrite imports | ||
for _, paragraph := range imports { | ||
for _, importSpec := range paragraph { | ||
importPath := strings.TrimPrefix(strings.TrimSuffix(importSpec.Path.Value, `"`), `"`) | ||
|
||
// std package | ||
if importPath == gnoStdPkgBefore { | ||
if !astutil.RewriteImport(fset, f, gnoStdPkgBefore, gnoStdPkgAfter) { | ||
errs = multierr.Append(errs, fmt.Errorf("failed to replace the %q package with %q", gnoStdPkgBefore, gnoStdPkgAfter)) | ||
} | ||
} | ||
|
||
// p/pkg packages | ||
if strings.HasPrefix(importPath, gnoPackagePrefixBefore) { | ||
target := gnoPackagePrefixAfter + strings.TrimPrefix(importPath, gnoPackagePrefixBefore) | ||
|
||
if !astutil.RewriteImport(fset, f, importPath, target) { | ||
errs = multierr.Append(errs, fmt.Errorf("failed to replace the %q package with %q", importPath, target)) | ||
} | ||
|
||
} | ||
|
||
// r/realm packages | ||
if strings.HasPrefix(importPath, gnoRealmPkgsPrefixBefore) { | ||
target := gnoRealmPkgsPrefixAfter + strings.TrimPrefix(importPath, gnoRealmPkgsPrefixBefore) | ||
|
||
if !astutil.RewriteImport(fset, f, importPath, target) { | ||
errs = multierr.Append(errs, fmt.Errorf("failed to replace the %q package with %q", importPath, target)) | ||
} | ||
|
||
} | ||
} | ||
} | ||
|
||
// custom handler | ||
node := astutil.Apply(f, | ||
// pre | ||
func(c *astutil.Cursor) bool { | ||
// do things here | ||
return true | ||
}, | ||
// post | ||
func(c *astutil.Cursor) bool { | ||
// and here | ||
return true | ||
}, | ||
) | ||
|
||
return node, errs | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package gno | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"go/format" | ||
"go/parser" | ||
"go/token" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGno2Go(t *testing.T) { | ||
var cases = []struct { | ||
name string | ||
source string | ||
expectedOutput string | ||
expectedPreprocessorError error | ||
}{ | ||
{ | ||
name: "hello", | ||
source: "package foo\nfunc hello() string { return \"world\"}", | ||
expectedOutput: "package foo\nfunc hello() string { return \"world\"}", | ||
}, { | ||
name: "use-std", | ||
source: "package foo\nimport \"std\"\nfunc hello() string { _ = std.Foo\nreturn \"world\"}", | ||
expectedOutput: "package foo\nimport \"github.com/gnolang/gno/stdlibs/stdshim\"\nfunc hello() string { _ = std.Foo\nreturn \"world\"}", | ||
}, { | ||
name: "use-realm", | ||
source: "package foo\nimport \"gno.land/r/users\"\nfunc foo() { _ = users.Register}", | ||
expectedOutput: "package foo\nimport \"github.com/gnolang/gno/examples/gno.land/r/users\"\nfunc foo() { _ = users.Register}", | ||
}, { | ||
name: "use-avl", | ||
source: "package foo\nimport \"gno.land/p/avl\"\nfunc foo() { _ = avl.Tree}", | ||
expectedOutput: "package foo\nimport \"github.com/gnolang/gno/examples/gno.land/p/avl\"\nfunc foo() { _ = avl.Tree}", | ||
}, { | ||
name: "use-named-std", | ||
source: "package foo\nimport bar \"std\"\nfunc hello() string { _ = bar.Foo\nreturn \"world\"}", | ||
expectedOutput: "package foo\nimport bar \"github.com/gnolang/gno/stdlibs/stdshim\"\nfunc hello() string { _ = bar.Foo\nreturn \"world\"}", | ||
}, { | ||
name: "blacklisted-package", | ||
source: "package foo\nimport \"reflect\"\nfunc foo() { _ = reflect.ValueOf}", | ||
expectedPreprocessorError: errors.New(`import "reflect" is not in the whitelist`), | ||
}, { | ||
name: "whitelisted-package", | ||
source: "package foo\nimport \"regexp\"\nfunc foo() { _ = regexp.MatchString}", | ||
expectedOutput: "package foo\nimport \"regexp\"\nfunc foo() { _ = regexp.MatchString}", | ||
}, | ||
// multiple files | ||
// syntax error | ||
// unknown realm? | ||
// blacklist | ||
// etc | ||
} | ||
for _, c := range cases { | ||
c := c // scopelint | ||
t.Run(c.name, func(t *testing.T) { | ||
// parse gno | ||
fset := token.NewFileSet() | ||
f, err := parser.ParseFile(fset, "foo.go", c.source, parser.ParseComments) | ||
assert.NoError(t, err) | ||
|
||
// call preprocessor | ||
transformed, err := Gno2Go(fset, f) | ||
if c.expectedPreprocessorError == nil { | ||
assert.NoError(t, err) | ||
} else { | ||
assert.Equal(t, err, c.expectedPreprocessorError) | ||
} | ||
|
||
// generate go | ||
var buf bytes.Buffer | ||
err = format.Node(&buf, fset, transformed) | ||
assert.NoError(t, err) | ||
got := buf.Bytes() | ||
|
||
// check output | ||
if c.expectedOutput != "" { | ||
expect, err := format.Source([]byte(c.expectedOutput)) | ||
if !bytes.Equal(expect, got) { | ||
t.Logf("got:\n%s", got) | ||
t.Logf("expect:\n%s", expect) | ||
t.Fatal("mismatch") | ||
} | ||
assert.NoError(t, err) | ||
} | ||
}) | ||
} | ||
} |
This file contains 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
This file contains 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