xref: /aosp_15_r20/external/starlark-go/starlarkstruct/module.go (revision 4947cdc739c985f6d86941e22894f5cefe7c9e9a)
1package starlarkstruct
2
3import (
4	"fmt"
5
6	"go.starlark.net/starlark"
7)
8
9// A Module is a named collection of values,
10// typically a suite of functions imported by a load statement.
11//
12// It differs from Struct primarily in that its string representation
13// does not enumerate its fields.
14type Module struct {
15	Name    string
16	Members starlark.StringDict
17}
18
19var _ starlark.HasAttrs = (*Module)(nil)
20
21func (m *Module) Attr(name string) (starlark.Value, error) { return m.Members[name], nil }
22func (m *Module) AttrNames() []string                      { return m.Members.Keys() }
23func (m *Module) Freeze()                                  { m.Members.Freeze() }
24func (m *Module) Hash() (uint32, error)                    { return 0, fmt.Errorf("unhashable: %s", m.Type()) }
25func (m *Module) String() string                           { return fmt.Sprintf("<module %q>", m.Name) }
26func (m *Module) Truth() starlark.Bool                     { return true }
27func (m *Module) Type() string                             { return "module" }
28
29// MakeModule may be used as the implementation of a Starlark built-in
30// function, module(name, **kwargs). It returns a new module with the
31// specified name and members.
32func MakeModule(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
33	var name string
34	if err := starlark.UnpackPositionalArgs(b.Name(), args, nil, 1, &name); err != nil {
35		return nil, err
36	}
37	members := make(starlark.StringDict, len(kwargs))
38	for _, kwarg := range kwargs {
39		k := string(kwarg[0].(starlark.String))
40		members[k] = kwarg[1]
41	}
42	return &Module{name, members}, nil
43}
44