forked from google/starlark-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
starlarkstruct: add 'module' type, and use it for assert module (goog…
…le#91) ...instead of struct, which is more suited to dynamic values. This type will be used for Stargo (see wip-stargo branch). For example, this load statement load("go", http="net/http", json="encoding/json") would load two modules.
- Loading branch information
Showing
6 changed files
with
76 additions
and
24 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
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,8 @@ | ||
# Tests of Module. | ||
|
||
load("assert.star", "assert") | ||
|
||
assert.eq(type(assert), "module") | ||
assert.eq(str(assert), '<module "assert">') | ||
assert.eq(dir(assert), ["contains", "eq", "fail", "fails", "lt", "ne", "true"]) | ||
assert.fails(lambda : {assert: None}, "unhashable: module") |
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,43 @@ | ||
package starlarkstruct | ||
|
||
import ( | ||
"fmt" | ||
|
||
"go.starlark.net/starlark" | ||
) | ||
|
||
// A Module is a named collection of values, | ||
// typically a suite of functions imported by a load statement. | ||
// | ||
// It differs from Struct primarily in that its string representation | ||
// does not enumerate its fields. | ||
type Module struct { | ||
Name string | ||
Members starlark.StringDict | ||
} | ||
|
||
var _ starlark.HasAttrs = (*Module)(nil) | ||
|
||
func (m *Module) Attr(name string) (starlark.Value, error) { return m.Members[name], nil } | ||
func (m *Module) AttrNames() []string { return m.Members.Keys() } | ||
func (m *Module) Freeze() { m.Members.Freeze() } | ||
func (m *Module) Hash() (uint32, error) { return 0, fmt.Errorf("unhashable: %s", m.Type()) } | ||
func (m *Module) String() string { return fmt.Sprintf("<module %q>", m.Name) } | ||
func (m *Module) Truth() starlark.Bool { return true } | ||
func (m *Module) Type() string { return "module" } | ||
|
||
// MakeModule may be used as the implementation of a Starlark built-in | ||
// function, module(name, **kwargs). It returns a new module with the | ||
// specified name and members. | ||
func MakeModule(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) { | ||
var name string | ||
if err := starlark.UnpackPositionalArgs(b.Name(), args, nil, 1, &name); err != nil { | ||
return nil, err | ||
} | ||
members := make(starlark.StringDict, len(kwargs)) | ||
for _, kwarg := range kwargs { | ||
k := string(kwarg[0].(starlark.String)) | ||
members[k] = kwarg[1] | ||
} | ||
return &Module{name, members}, nil | ||
} |
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
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