Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow removal of specific attributes inside a builtin module #446

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions modules.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package tengo

import "fmt"

// Importable interface represents importable module instance.
type Importable interface {
// Import should return either an Object or module source code ([]byte).
Expand Down Expand Up @@ -39,9 +41,29 @@ func (m *ModuleMap) AddSourceModule(name string, src []byte) {
m.m[name] = &SourceModule{Src: src}
}

// Remove removes a named module.
func (m *ModuleMap) Remove(name string) {
delete(m.m, name)
// Remove removes a named module. If one or more attrNames are supplied, instead removes those attributes from the named module (will panic if module is not a *BuiltinModule).
func (m *ModuleMap) Remove(name string, attrNames ...string) {
if len(attrNames) > 0 {
mod, ok := m.m[name]
if !ok {
// module doesn't exist, nothing to remove
return
}
builtinMod, ok := mod.(*BuiltinModule)
if !ok {
panic(fmt.Errorf("cannot remove attrs %v from module %q: expected module to be of type *BuiltinModule but is %T", attrNames, name, mod))
}
for _, attrName := range attrNames {
delete(builtinMod.Attrs, attrName)
}
if len(builtinMod.Attrs) == 0 {
// delete module as there are no attrs left
delete(m.m, name)
}
} else {
// delete whole module as no attrNames supplied
delete(m.m, name)
}
}

// Get returns an import module identified by name. It returns if the name is
Expand Down