modules.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package tengo
  2. // Importable interface represents importable module instance.
  3. type Importable interface {
  4. // Import should return either an Object or module source code ([]byte).
  5. Import(moduleName string) (interface{}, error)
  6. }
  7. // ModuleGetter enables implementing dynamic module loading.
  8. type ModuleGetter interface {
  9. Get(name string) Importable
  10. }
  11. // ModuleMap represents a set of named modules. Use NewModuleMap to create a
  12. // new module map.
  13. type ModuleMap struct {
  14. m map[string]Importable
  15. }
  16. // NewModuleMap creates a new module map.
  17. func NewModuleMap() *ModuleMap {
  18. return &ModuleMap{
  19. m: make(map[string]Importable),
  20. }
  21. }
  22. // Add adds an import module.
  23. func (m *ModuleMap) Add(name string, module Importable) {
  24. m.m[name] = module
  25. }
  26. // AddBuiltinModule adds a builtin module.
  27. func (m *ModuleMap) AddBuiltinModule(name string, attrs map[string]Object) {
  28. m.m[name] = &BuiltinModule{Attrs: attrs}
  29. }
  30. // AddSourceModule adds a source module.
  31. func (m *ModuleMap) AddSourceModule(name string, src []byte) {
  32. m.m[name] = &SourceModule{Src: src}
  33. }
  34. // Remove removes a named module.
  35. func (m *ModuleMap) Remove(name string) {
  36. delete(m.m, name)
  37. }
  38. // Get returns an import module identified by name. It returns if the name is
  39. // not found.
  40. func (m *ModuleMap) Get(name string) Importable {
  41. return m.m[name]
  42. }
  43. // GetBuiltinModule returns a builtin module identified by name. It returns
  44. // if the name is not found or the module is not a builtin module.
  45. func (m *ModuleMap) GetBuiltinModule(name string) *BuiltinModule {
  46. mod, _ := m.m[name].(*BuiltinModule)
  47. return mod
  48. }
  49. // GetSourceModule returns a source module identified by name. It returns if
  50. // the name is not found or the module is not a source module.
  51. func (m *ModuleMap) GetSourceModule(name string) *SourceModule {
  52. mod, _ := m.m[name].(*SourceModule)
  53. return mod
  54. }
  55. // Copy creates a copy of the module map.
  56. func (m *ModuleMap) Copy() *ModuleMap {
  57. c := &ModuleMap{
  58. m: make(map[string]Importable),
  59. }
  60. for name, mod := range m.m {
  61. c.m[name] = mod
  62. }
  63. return c
  64. }
  65. // Len returns the number of named modules.
  66. func (m *ModuleMap) Len() int {
  67. return len(m.m)
  68. }
  69. // AddMap adds named modules from another module map.
  70. func (m *ModuleMap) AddMap(o *ModuleMap) {
  71. for name, mod := range o.m {
  72. m.m[name] = mod
  73. }
  74. }
  75. // SourceModule is an importable module that's written in Tengo.
  76. type SourceModule struct {
  77. Src []byte
  78. }
  79. // Import returns a module source code.
  80. func (m *SourceModule) Import(_ string) (interface{}, error) {
  81. return m.Src, nil
  82. }