Skip to content

Commit

Permalink
refactor: add relevant model
Browse files Browse the repository at this point in the history
  • Loading branch information
shivasurya committed Feb 8, 2025
1 parent b20acc6 commit df6dce8
Show file tree
Hide file tree
Showing 11 changed files with 1,091 additions and 127 deletions.
46 changes: 21 additions & 25 deletions sourcecode-parser/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,50 @@ import (
)

type StorageNode struct {
Package []*model.TreeNode
ImportDecl []*model.TreeNode
ClassDecl []*model.TreeNode
MethodDecl []*model.TreeNode
FieldDecl []*model.TreeNode
Variable []*model.TreeNode
BinaryExpr []*model.TreeNode
Annotation []*model.TreeNode
JavaDoc []*model.TreeNode
Comment []*model.TreeNode
}

func (s *StorageNode) AddPackage(node *model.TreeNode) {
Package []*model.Package
ImportDecl []*model.ImportType
Annotation []*model.Annotation
ClassDecl []*model.ClassOrInterface
MethodDecl []*model.Method
MethodCall []*model.MethodCall
FieldDecl []*model.FieldDeclaration
Variable []*model.LocalVariableDecl
BinaryExpr []*model.BinaryExpr
JavaDoc []*model.Javadoc
}

func (s *StorageNode) AddPackage(node *model.Package) {
s.Package = append(s.Package, node)
}

func (s *StorageNode) AddImportDecl(node *model.TreeNode) {
func (s *StorageNode) AddImportDecl(node *model.ImportType) {
s.ImportDecl = append(s.ImportDecl, node)
}

func (s *StorageNode) AddClassDecl(node *model.TreeNode) {
func (s *StorageNode) AddClassDecl(node *model.ClassOrInterface) {
s.ClassDecl = append(s.ClassDecl, node)
}

func (s *StorageNode) AddMethodDecl(node *model.TreeNode) {
func (s *StorageNode) AddMethodDecl(node *model.Method) {
s.MethodDecl = append(s.MethodDecl, node)
}

func (s *StorageNode) AddFieldDecl(node *model.TreeNode) {
func (s *StorageNode) AddFieldDecl(node *model.FieldDeclaration) {
s.FieldDecl = append(s.FieldDecl, node)
}

func (s *StorageNode) AddVariable(node *model.TreeNode) {
func (s *StorageNode) AddVariable(node *model.LocalVariableDecl) {
s.Variable = append(s.Variable, node)
}

func (s *StorageNode) AddBinaryExpr(node *model.TreeNode) {
func (s *StorageNode) AddBinaryExpr(node *model.BinaryExpr) {
s.BinaryExpr = append(s.BinaryExpr, node)
}

func (s *StorageNode) AddAnnotation(node *model.TreeNode) {
func (s *StorageNode) AddAnnotation(node *model.Annotation) {
s.Annotation = append(s.Annotation, node)
}

func (s *StorageNode) AddJavaDoc(node *model.TreeNode) {
func (s *StorageNode) AddJavaDoc(node *model.Javadoc) {
s.JavaDoc = append(s.JavaDoc, node)
}

func (s *StorageNode) AddComment(node *model.TreeNode) {
s.Comment = append(s.Comment, node)
}
6 changes: 4 additions & 2 deletions sourcecode-parser/graph/construct.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"sync"
"time"

"github.com/shivasurya/code-pathfinder/sourcecode-parser/db"
javalang "github.com/shivasurya/code-pathfinder/sourcecode-parser/graph/java"
utilities "github.com/shivasurya/code-pathfinder/sourcecode-parser/util"

Expand All @@ -18,7 +19,7 @@ import (
sitter "github.com/smacker/go-tree-sitter"
)

func buildQLTreeFromAST(node *sitter.Node, sourceCode []byte, currentContext *model.Node, file string, parentNode *model.TreeNode) {
func buildQLTreeFromAST(node *sitter.Node, sourceCode []byte, currentContext *model.Node, file string, parentNode *model.TreeNode, storageNode db.StorageNode) {
switch node.Type() {
case "block":
blockStmtNode := javalang.ParseBlockStatement(node, sourceCode, file)
Expand Down Expand Up @@ -166,6 +167,7 @@ func Initialize(directory string) []*model.TreeNode {
defer tree.Close()

rootNode := tree.RootNode()
storageNode := db.StorageNode{}
localTree := &model.TreeNode{
Parent: nil,
Node: &model.Node{
Expand All @@ -175,7 +177,7 @@ func Initialize(directory string) []*model.TreeNode {
},
}
statusChan <- fmt.Sprintf("\033[32mWorker %d ....... Building graph and traversing code %s\033[0m", workerID, fileName)
buildQLTreeFromAST(rootNode, sourceCode, nil, file, localTree)
buildQLTreeFromAST(rootNode, sourceCode, nil, file, localTree, storageNode)
treeHolder = append(treeHolder, localTree)
statusChan <- fmt.Sprintf("\033[32mWorker %d ....... Done processing file %s\033[0m", workerID, fileName)

Expand Down
86 changes: 0 additions & 86 deletions sourcecode-parser/graph/construct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,92 +14,6 @@ import (
"github.com/smacker/go-tree-sitter/java"
)

func TestNewCodeGraph(t *testing.T) {
graph := NewCodeGraph()
if graph == nil {
t.Error("NewCodeGraph() returned nil")
}
if graph != nil && graph.Nodes == nil {
t.Error("NewCodeGraph() returned graph with nil Nodes")
}
if graph != nil && graph.Edges == nil {
t.Error("NewCodeGraph() returned graph with nil Edges")
}
if graph != nil && len(graph.Nodes) != 0 {
t.Errorf("NewCodeGraph() returned graph with non-empty Nodes, got %d nodes", len(graph.Nodes))
}
if graph != nil && len(graph.Edges) != 0 {
t.Errorf("NewCodeGraph() returned graph with non-empty Edges, got %d edges", len(graph.Edges))
}
}

func TestAddNode(t *testing.T) {
graph := NewCodeGraph()
node := &model.Node{ID: "test_node"}
graph.AddNode(node)

if len(graph.Nodes) != 1 {
t.Errorf("AddNode() failed to add node, expected 1 node, got %d", len(graph.Nodes))
}
if graph.Nodes["test_node"] != node {
t.Error("AddNode() failed to add node with correct ID")
}
}

func TestAddEdge(t *testing.T) {
graph := NewCodeGraph()
node1 := &model.Node{ID: "node1"}
node2 := &model.Node{ID: "node2"}
graph.AddNode(node1)
graph.AddNode(node2)

graph.AddEdge(node1, node2)

if len(graph.Edges) != 1 {
t.Errorf("AddEdge() failed to add edge, expected 1 edge, got %d", len(graph.Edges))
}
if graph.Edges[0].From != node1 || graph.Edges[0].To != node2 {
t.Error("AddEdge() failed to add edge with correct From and To nodes")
}
if len(node1.OutgoingEdges) != 1 {
t.Errorf("AddEdge() failed to add outgoing edge to From node, expected 1 edge, got %d", len(node1.OutgoingEdges))
}
if node1.OutgoingEdges[0].To != node2 {
t.Error("AddEdge() failed to add correct outgoing edge to From node")
}
}

func TestAddMultipleNodesAndEdges(t *testing.T) {
graph := NewCodeGraph()
node1 := &model.Node{ID: "node1"}
node2 := &model.Node{ID: "node2"}
node3 := &model.Node{ID: "node3"}

graph.AddNode(node1)
graph.AddNode(node2)
graph.AddNode(node3)

graph.AddEdge(node1, node2)
graph.AddEdge(node2, node3)
graph.AddEdge(node1, node3)

if len(graph.Nodes) != 3 {
t.Errorf("Expected 3 nodes, got %d", len(graph.Nodes))
}
if len(graph.Edges) != 3 {
t.Errorf("Expected 3 edges, got %d", len(graph.Edges))
}
if len(node1.OutgoingEdges) != 2 {
t.Errorf("Expected 2 outgoing edges for node1, got %d", len(node1.OutgoingEdges))
}
if len(node2.OutgoingEdges) != 1 {
t.Errorf("Expected 1 outgoing edge for node2, got %d", len(node2.OutgoingEdges))
}
if len(node3.OutgoingEdges) != 0 {
t.Errorf("Expected 0 outgoing edges for node3, got %d", len(node3.OutgoingEdges))
}
}

func TestIsJavaSourceFile(t *testing.T) {
tests := []struct {
name string
Expand Down
2 changes: 1 addition & 1 deletion sourcecode-parser/graph/java/parse_statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func ParseBlockStatement(node *sitter.Node, sourceCode []byte, file string) *mod
}

uniqueBlockID := fmt.Sprintf("block_%d_%d_%s", node.StartPoint().Row+1, node.StartPoint().Column+1, file)
blockStmtNode := &graph.Node{
blockStmtNode := &model.Node{
ID: util.GenerateSha256(uniqueBlockID),
Type: "BlockStmt",
LineNumber: node.StartPoint().Row + 1,
Expand Down
12 changes: 0 additions & 12 deletions sourcecode-parser/model/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,3 @@ func (j *JarFile) GetManifestMainAttributes(key string) (string, bool) {
func (j *JarFile) GetSpecificationVersion() string {
return j.SpecificationVersion
}

type Package struct {
Package string
}

func (p *Package) GetAPrimaryQlClass() string {
return "Package"
}

func (p *Package) GetURL() string {
return p.Package
}
Loading

0 comments on commit df6dce8

Please sign in to comment.