diff --git a/cgraph/cgraph.go b/cgraph/cgraph.go index 6959802..a0826c1 100644 --- a/cgraph/cgraph.go +++ b/cgraph/cgraph.go @@ -528,6 +528,22 @@ func (e *Edge) SetNode(v *Node) { e.wasm.SetNode(v.getWasm()) } +func (e *Edge) Head() (*Node, error) { + n, err := e.wasm.Head(context.Background()) + if err != nil { + return nil, err + } + return toNode(n), nil +} + +func (e *Edge) Tail() (*Node, error) { + n, err := e.wasm.Tail(context.Background()) + if err != nil { + return nil, err + } + return toNode(n), nil +} + func (c *CommonFields) Disc() *Disc { return toDisc(c.wasm.GetDisc()) } diff --git a/graphviz_test.go b/graphviz_test.go index cadedc6..15b634e 100644 --- a/graphviz_test.go +++ b/graphviz_test.go @@ -288,3 +288,65 @@ func TestNodeDegree(t *testing.T) { } } } + +func TestEdgeSourceAndTarget(t *testing.T) { + ctx := context.Background() + graph, err := graphviz.New(ctx) + if err != nil { + t.Fatalf("Error: %+v", err) + } + + g, err := graph.Graph() + if err != nil { + t.Fatalf("Error: %+v", err) + } + + nodeA, err := g.CreateNodeByName("a") + if err != nil { + t.Fatalf("Error: %+v", err) + } + + nodeB, err := g.CreateNodeByName("b") + if err != nil { + t.Fatalf("Error: %+v", err) + } + + edge, err := g.CreateEdgeByName("edge", nodeA, nodeB) + if err != nil { + t.Fatalf("Error: %+v", err) + } + + head, err := edge.Head() + if err != nil { + t.Fatalf("Error: %+v", err) + } + if head == nil { + t.Fatalf("Source is nil") + } + + headName, err := head.Name() + if err != nil { + t.Fatalf("Error: %+v", err) + } + + if headName != "b" { + t.Fatalf("Expected source name to be 'b', got '%s'", headName) + } + + target, err := edge.Tail() + if err != nil { + t.Fatalf("Error: %+v", err) + } + if target == nil { + t.Fatalf("Target is nil") + } + + tailName, err := target.Name() + if err != nil { + t.Fatalf("Error: %+v", err) + } + + if tailName != "a" { + t.Fatalf("Expected target name to be 'a', got '%s'", tailName) + } +} diff --git a/internal/wasm/bind.go b/internal/wasm/bind.go index 216acb1..f67907c 100644 --- a/internal/wasm/bind.go +++ b/internal/wasm/bind.go @@ -15858,6 +15858,26 @@ func (v *Graph) Edge(ctx context.Context, _arg0 *Node, _arg1 *Node, _arg2 string return ret, nil } +func (v *Edge) Head(ctx context.Context) (*Node, error) { + var zero *Node + p, err := mod.callWithRet(ctx, "Edge_head", v.getPtr()) + if err != nil { + return zero, err + } + ret := newNode(p) + return ret, nil +} + +func (v *Edge) Tail(ctx context.Context) (*Node, error) { + var zero *Node + p, err := mod.callWithRet(ctx, "Edge_tail", v.getPtr()) + if err != nil { + return zero, err + } + ret := newNode(p) + return ret, nil +} + func (v *Graph) IdEdge(ctx context.Context, _arg0 *Node, _arg1 *Node, _arg2 uint64, _arg3 int) (*Edge, error) { var zero *Edge arg0, err := mod.toObjectWasmValue(ctx, _arg0) diff --git a/internal/wasm/bind.proto b/internal/wasm/bind.proto index 5f72dab..6c3dc1d 100644 --- a/internal/wasm/bind.proto +++ b/internal/wasm/bind.proto @@ -690,6 +690,26 @@ option (nori.file).export = { pointer: 1 } } + method { + recv: "Edge" + name: "head" + alias: "aghead" + return { + kind: STRUCT + ref: "Node" + pointer: 1 + } + } + method { + recv: "Edge" + name: "tail" + alias: "agtail" + return { + kind: STRUCT + ref: "Node" + pointer: 1 + } + } method { recv: "Graph" name: "idEdge" diff --git a/internal/wasm/build/bind.c b/internal/wasm/build/bind.c index 17aa686..3aaedad 100644 --- a/internal/wasm/build/bind.c +++ b/internal/wasm/build/bind.c @@ -5690,6 +5690,26 @@ void wasm_bridge_Graph_edge(void * _arg0, void * _arg1, void * _arg2, void * _ar *_arg5 = v; } +void wasm_bridge_Edge_head(void * _arg0, void ** _arg1) { + Agedge_t * arg0; + arg0 = (Agedge_t *)_arg0; + Agnode_t * ret = aghead( + arg0 + ); + Agnode_t * v = (Agnode_t *)ret; + *_arg1 = v; +} + +void wasm_bridge_Edge_tail(void * _arg0, void ** _arg1) { + Agedge_t * arg0; + arg0 = (Agedge_t *)_arg0; + Agnode_t * ret = agtail( + arg0 + ); + Agnode_t * v = (Agnode_t *)ret; + *_arg1 = v; +} + void wasm_bridge_Graph_idEdge(void * _arg0, void * _arg1, void * _arg2, unsigned long long int _arg3, int _arg4, void ** _arg5) { Agraph_t * arg0; arg0 = (Agraph_t *)_arg0; diff --git a/internal/wasm/graphviz.wasm b/internal/wasm/graphviz.wasm index 5476b24..8dde238 100755 Binary files a/internal/wasm/graphviz.wasm and b/internal/wasm/graphviz.wasm differ