Skip to content

Commit 69582b4

Browse files
committed
add new pkg/module files and types for go packaging method
1 parent 14daef0 commit 69582b4

File tree

3 files changed

+278
-20
lines changed

3 files changed

+278
-20
lines changed

pkg/module/base.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package module
2+
3+
import "fmt"
4+
5+
// Base provides common module functionality
6+
type Base struct {
7+
id string
8+
name string
9+
description string
10+
repo Repository
11+
commands []Command
12+
}
13+
14+
// NewBase creates a new base module
15+
func NewBase(id, name, description string) *Base {
16+
return &Base{
17+
id: id,
18+
name: name,
19+
description: description,
20+
commands: make([]Command, 0),
21+
}
22+
}
23+
24+
// ID returns the module identifier
25+
func (b *Base) ID() string {
26+
return b.id
27+
}
28+
29+
// Name returns the module name
30+
func (b *Base) Name() string {
31+
return b.name
32+
}
33+
34+
// Description returns the module description
35+
func (b *Base) Description() string {
36+
return b.description
37+
}
38+
39+
// Init initializes the module with a repository
40+
func (b *Base) Init(repo Repository) error {
41+
b.repo = repo
42+
return nil
43+
}
44+
45+
// Commands returns the list of available commands
46+
func (b *Base) Commands() []Command {
47+
baseCommands := []Command{
48+
{
49+
Name: "help",
50+
Description: "Show module help",
51+
},
52+
{
53+
Name: "version",
54+
Description: "Show module version",
55+
},
56+
}
57+
return append(baseCommands, b.commands...)
58+
}
59+
60+
// HandleCommand handles a module command
61+
func (b *Base) HandleCommand(cmd string, args []string) error {
62+
switch cmd {
63+
case "help":
64+
fmt.Printf("Module: %s\n", b.name)
65+
fmt.Printf("Description: %s\n\n", b.description)
66+
fmt.Println("Commands:")
67+
for _, cmd := range b.Commands() {
68+
if cmd.Usage != "" {
69+
fmt.Printf(" %s - %s\n Usage: %s\n", cmd.Name, cmd.Description, cmd.Usage)
70+
} else {
71+
fmt.Printf(" %s - %s\n", cmd.Name, cmd.Description)
72+
}
73+
}
74+
return nil
75+
case "version":
76+
fmt.Printf("%s version 1.0.0\n", b.name)
77+
return nil
78+
default:
79+
return fmt.Errorf("unknown command: %s", cmd)
80+
}
81+
}
82+
83+
// AddCommand adds a command to the module
84+
func (b *Base) AddCommand(cmd Command) {
85+
b.commands = append(b.commands, cmd)
86+
}
87+
88+
// Helper methods for repository operations
89+
90+
// AddNode adds a node to the repository
91+
func (b *Base) AddNode(content []byte, nodeType string, meta map[string]interface{}) (string, error) {
92+
if b.repo == nil {
93+
return "", fmt.Errorf("module not initialized")
94+
}
95+
return b.repo.AddNode(content, nodeType, meta)
96+
}
97+
98+
// GetNode gets a node from the repository
99+
func (b *Base) GetNode(id string) (*Node, error) {
100+
if b.repo == nil {
101+
return nil, fmt.Errorf("module not initialized")
102+
}
103+
return b.repo.GetNode(id)
104+
}
105+
106+
// AddLink adds a link between nodes
107+
func (b *Base) AddLink(source, target, linkType string, meta map[string]interface{}) error {
108+
if b.repo == nil {
109+
return fmt.Errorf("module not initialized")
110+
}
111+
return b.repo.AddLink(source, target, linkType, meta)
112+
}
113+
114+
// GetLinks gets links for a node
115+
func (b *Base) GetLinks(nodeID string) ([]*Link, error) {
116+
if b.repo == nil {
117+
return nil, fmt.Errorf("module not initialized")
118+
}
119+
return b.repo.GetLinks(nodeID)
120+
}

pkg/module/module.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package module
2+
3+
import "time"
4+
5+
// Module defines the interface for Memex modules
6+
type Module interface {
7+
// Identity
8+
ID() string // Unique identifier (e.g., "git", "ast")
9+
Name() string // Human-readable name
10+
Description() string // Module description
11+
12+
// Core functionality
13+
Init(repo Repository) error // Initialize module with repository
14+
Commands() []Command // Available commands
15+
HandleCommand(cmd string, args []string) error // Execute a command
16+
}
17+
18+
// Command represents a module command
19+
type Command struct {
20+
Name string // Command name (e.g., "add", "status")
21+
Description string // Command description
22+
Usage string // Usage example (e.g., "git add <file>")
23+
Args []string // Expected arguments
24+
}
25+
26+
// Node represents a node in the graph
27+
type Node struct {
28+
ID string
29+
Type string
30+
Content []byte
31+
Meta map[string]interface{}
32+
Created time.Time
33+
Modified time.Time
34+
}
35+
36+
// Link represents a relationship between nodes
37+
type Link struct {
38+
Source string
39+
Target string
40+
Type string
41+
Meta map[string]interface{}
42+
Created time.Time
43+
Modified time.Time
44+
}
45+
46+
// Repository defines operations modules can perform
47+
type Repository interface {
48+
// Node operations
49+
AddNode(content []byte, nodeType string, meta map[string]interface{}) (string, error)
50+
GetNode(id string) (*Node, error)
51+
DeleteNode(id string) error
52+
ListNodes() ([]string, error)
53+
GetContent(id string) ([]byte, error)
54+
55+
// Link operations
56+
AddLink(source, target, linkType string, meta map[string]interface{}) error
57+
GetLinks(nodeID string) ([]*Link, error)
58+
DeleteLink(source, target, linkType string) error
59+
60+
// Query operations
61+
QueryNodesByModule(moduleID string) ([]*Node, error)
62+
QueryLinksByModule(moduleID string) ([]*Link, error)
63+
}

pkg/types/types.go

Lines changed: 95 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package types
22

3-
import "time"
3+
import (
4+
"fmt"
5+
"time"
6+
)
47

58
// Node represents a node in the graph
69
type Node struct {
@@ -71,28 +74,100 @@ type Repository interface {
7174
Close() error
7275
}
7376

74-
// ModuleRepository extends the Repository interface with module management capabilities
75-
type ModuleRepository interface {
76-
Repository
77+
// Common command names
78+
const (
79+
CmdID = "id"
80+
CmdName = "name"
81+
CmdDescription = "description"
82+
CmdHelp = "help"
83+
)
7784

78-
// Module management
79-
GetLoader() ModuleLoader
80-
GetDiscovery() ModuleDiscovery
85+
// BaseModule provides a basic implementation of Module interface
86+
type BaseModule struct {
87+
id string
88+
name string
89+
description string
90+
repo Repository
91+
commands []Command
8192
}
8293

83-
// ModuleLoader defines the interface for module loading
84-
type ModuleLoader interface {
85-
AddPath(path string)
86-
AddDevPath(moduleID, path string)
87-
LoadModule(id string, mod Module) error
88-
UnloadModule(id string) error
89-
UnloadAll() error
90-
IsDevModule(moduleID string) bool
91-
GetDevPath(moduleID string) (string, bool)
94+
// NewBaseModule creates a new base module
95+
func NewBaseModule(id, name, description string) *BaseModule {
96+
return &BaseModule{
97+
id: id,
98+
name: name,
99+
description: description,
100+
commands: make([]Command, 0),
101+
}
92102
}
93103

94-
// ModuleDiscovery defines the interface for module discovery
95-
type ModuleDiscovery interface {
96-
DiscoverModules() error
97-
ValidateModule(mod Module) error
104+
// ID returns the module identifier
105+
func (m *BaseModule) ID() string {
106+
return m.id
107+
}
108+
109+
// Name returns the module name
110+
func (m *BaseModule) Name() string {
111+
return m.name
112+
}
113+
114+
// Description returns the module description
115+
func (m *BaseModule) Description() string {
116+
return m.description
117+
}
118+
119+
// Init initializes the module with a repository
120+
func (m *BaseModule) Init(repo Repository) error {
121+
m.repo = repo
122+
return nil
123+
}
124+
125+
// Commands returns the list of available commands
126+
func (m *BaseModule) Commands() []Command {
127+
baseCommands := []Command{
128+
{
129+
Name: CmdID,
130+
Description: "Get module ID",
131+
},
132+
{
133+
Name: CmdName,
134+
Description: "Get module name",
135+
},
136+
{
137+
Name: CmdDescription,
138+
Description: "Get module description",
139+
},
140+
{
141+
Name: CmdHelp,
142+
Description: "Get command help",
143+
},
144+
}
145+
return append(baseCommands, m.commands...)
146+
}
147+
148+
// AddCommand adds a command to the module
149+
func (m *BaseModule) AddCommand(cmd Command) {
150+
m.commands = append(m.commands, cmd)
151+
}
152+
153+
// Helper functions for repository operations
154+
func (m *BaseModule) AddNode(content []byte, nodeType string, meta map[string]interface{}) (string, error) {
155+
if m.repo == nil {
156+
return "", fmt.Errorf("module not initialized")
157+
}
158+
return m.repo.AddNode(content, nodeType, meta)
159+
}
160+
161+
func (m *BaseModule) GetNode(id string) (*Node, error) {
162+
if m.repo == nil {
163+
return nil, fmt.Errorf("module not initialized")
164+
}
165+
return m.repo.GetNode(id)
166+
}
167+
168+
func (m *BaseModule) AddLink(source, target, linkType string, meta map[string]interface{}) error {
169+
if m.repo == nil {
170+
return fmt.Errorf("module not initialized")
171+
}
172+
return m.repo.AddLink(source, target, linkType, meta)
98173
}

0 commit comments

Comments
 (0)